少年修仙传客户端代码仓库
hch
2025-06-12 204ef05a831c9484e2abc561d27ecbff7c797453
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
 
public class GameObjectPool
{
 
    private List<GameObject> m_FreeList;
    private List<GameObject> m_ActiveList;
    private GameObject m_Prefab;
 
    public int nameHashCode;
    public string name;
 
    public GameObjectPool(GameObject prefab)
    {
        name = prefab.name;
        nameHashCode = name.GetHashCode();
 
        m_Prefab = prefab;
        m_FreeList = new List<GameObject>();
        m_ActiveList = new List<GameObject>();
    }
 
    public GameObject Request()
    {
        GameObject _gameObject = null;
        if (m_FreeList.Count == 0)
        {
            _gameObject = Object.Instantiate(m_Prefab);
            _gameObject.name = name;
        }
        else
        {
            _gameObject = m_FreeList[0];
            m_FreeList.RemoveAt(0);
        }
        m_ActiveList.Add(_gameObject);
        return _gameObject;
    }
 
    public void Release(GameObject gameObject)
    {
        if (m_ActiveList.Contains(gameObject))
        {
            m_ActiveList.Remove(gameObject);
        }
        else
        {
            DebugEx.LogWarningFormat("所回收的go对象 {0} 并不是从池里取得的...", gameObject.name);
        }
        m_FreeList.Add(gameObject);
       
    }
 
    public void Clear()
    {
        foreach (var _item in m_FreeList)
        {
            Object.Destroy(_item);
        }
        foreach (var _item in m_ActiveList)
        {
            Object.Destroy(_item);
        }
        m_FreeList.Clear();
        m_ActiveList.Clear();
    }
 
    public void Destroy()
    {
 
        Clear();
 
        m_Prefab = null;
 
        m_FreeList = null;
        m_ActiveList = null;
    }
 
#if UNITY_EDITOR
    public void ForeachActive(System.Action<GameObject> method)
    {
        for (int i = m_ActiveList.Count - 1; i >= 0; --i)
        {
            method(m_ActiveList[i]);
        }
    }
 
    public void ForeachFree(System.Action<GameObject> method)
    {
        for (int i = m_FreeList.Count - 1; i >= 0; --i)
        {
            method(m_FreeList[i]);
        }
    }
#endif
}