|
5 | 5 |
|
6 | 6 | [System.Serializable]
|
7 | 7 | public class ObjectPool {
|
8 |
| - private static uint lastInstanceId = 0; |
9 |
| - |
10 |
| - public int minCapacity; |
11 |
| - public int maxCapacity; |
12 |
| - public GameObject prefab; |
13 |
| - public bool returnNullOnUnderflow; |
14 |
| - public Func<GameObject, GameObject> initializer = (prefab) => GameObject.Instantiate (prefab); |
15 |
| - public string label = "<Unlabeled object pool #" + ++ObjectPool.lastInstanceId + ">"; |
16 |
| - |
17 |
| - private GameObject[] pool; |
18 |
| - private bool ready = false; |
19 |
| - private int queueStart = 0; |
20 |
| - private int queueEnd = 0; |
21 |
| - private int objectsInPool = 0; |
22 |
| - |
23 |
| - public GameObject GetObject() { |
24 |
| - if (!ready) { |
25 |
| - Init (); |
26 |
| - } |
27 |
| - |
28 |
| - if (objectsInPool == 0) { |
29 |
| - if (pool.LongLength < maxCapacity) { |
30 |
| - Grow (); |
31 |
| - } else if (returnNullOnUnderflow) { |
32 |
| - return null; |
33 |
| - } else { |
34 |
| - throw new OutOfMemoryException( |
35 |
| - "The maximum capacity (" + maxCapacity + ") of the object pool '" + label + "' has been exceeded" |
36 |
| - ); |
37 |
| - } |
38 |
| - } |
39 |
| - |
40 |
| - GameObject objectInstance = pool[queueStart]; |
41 |
| - objectInstance.SetActive (true); |
42 |
| - queueStart = (queueStart + 1) % pool.Length; |
43 |
| - objectsInPool--; |
44 |
| - return objectInstance; |
45 |
| - } |
46 |
| - |
47 |
| - public void ReturnObject(GameObject usedObject) { |
48 |
| - if (!ready) { |
49 |
| - Init (); |
50 |
| - } |
51 |
| - |
52 |
| - usedObject.SetActive (false); |
53 |
| - pool [queueEnd] = usedObject; |
54 |
| - queueEnd = (queueEnd + 1) % pool.Length; |
55 |
| - objectsInPool++; |
56 |
| - } |
57 |
| - |
58 |
| - public void Init() { |
59 |
| - if (ready) { |
60 |
| - return; |
61 |
| - } |
62 |
| - ready = true; |
63 |
| - |
64 |
| - if (prefab == null) { |
65 |
| - throw new ArgumentNullException ("The prefab game object cannot be null"); |
66 |
| - } |
67 |
| - if (initializer == null) { |
68 |
| - throw new ArgumentNullException ("The game object instance initializer function cannot be null"); |
69 |
| - } |
70 |
| - if (minCapacity <= 0) { |
71 |
| - throw new ArgumentOutOfRangeException ("The minimum capacity must be a positive integer"); |
72 |
| - } |
73 |
| - if (maxCapacity < minCapacity) { |
74 |
| - throw new ArgumentOutOfRangeException ( |
75 |
| - "The maximum capacity (" + maxCapacity + ") cannot be lower than the minimum capacity (" + minCapacity + ")" |
76 |
| - ); |
77 |
| - } |
78 |
| - |
79 |
| - pool = new GameObject[minCapacity]; |
80 |
| - for (int i = 0; i < minCapacity; i++) { |
81 |
| - AddObjectInstance (); |
82 |
| - } |
83 |
| - } |
84 |
| - |
85 |
| - private void Grow() { |
86 |
| - Debug.LogWarning ( |
87 |
| - "The currenct capacity (" + pool.LongLength + ") of the pool '" + label + |
88 |
| - "' has been exceeded, increasing capacity" |
89 |
| - ); |
90 |
| - |
91 |
| - int newCapacity; |
92 |
| - if (pool.Length > maxCapacity / 2) { |
93 |
| - newCapacity = maxCapacity; |
94 |
| - } else { |
95 |
| - newCapacity = pool.Length * 2; |
96 |
| - } |
97 |
| - int capacityIncrease = newCapacity - pool.Length; |
98 |
| - pool = new GameObject[newCapacity]; |
99 |
| - |
100 |
| - for (int i = 0; i < capacityIncrease; i++) { |
101 |
| - AddObjectInstance (); |
102 |
| - } |
103 |
| - } |
104 |
| - |
105 |
| - private void AddObjectInstance() { |
106 |
| - GameObject objectInstance = initializer (prefab); |
107 |
| - PooledObjectController controller = objectInstance.GetComponent<PooledObjectController> (); |
108 |
| - controller.SetParentPool (this); |
109 |
| - ReturnObject (objectInstance); |
110 |
| - } |
| 8 | + private static uint lastInstanceId = 0; |
| 9 | + |
| 10 | + public int minCapacity; |
| 11 | + public int maxCapacity; |
| 12 | + public GameObject prefab; |
| 13 | + public bool returnNullOnUnderflow; |
| 14 | + public Func<GameObject, GameObject> initializer = (prefab) => GameObject.Instantiate (prefab); |
| 15 | + public string label = "<Unlabeled object pool #" + ++ObjectPool.lastInstanceId + ">"; |
| 16 | + |
| 17 | + private GameObject[] pool; |
| 18 | + private bool ready = false; |
| 19 | + private int queueStart = 0; |
| 20 | + private int queueEnd = 0; |
| 21 | + private int objectsInPool = 0; |
| 22 | + |
| 23 | + public static bool IsPooled(GameObject gameObject) { |
| 24 | + return gameObject.GetComponent<PooledObjectController> (); |
| 25 | + } |
| 26 | + |
| 27 | + public static void ReturnObjectToPool(GameObject usedObject) { |
| 28 | + usedObject.GetComponent<PooledObjectController> ().ReturnToPool (); |
| 29 | + } |
| 30 | + |
| 31 | + public GameObject GetObject() { |
| 32 | + if (!ready) { |
| 33 | + Init (); |
| 34 | + } |
| 35 | + |
| 36 | + if (objectsInPool == 0) { |
| 37 | + if (pool.LongLength < maxCapacity) { |
| 38 | + Grow (); |
| 39 | + } else if (returnNullOnUnderflow) { |
| 40 | + return null; |
| 41 | + } else { |
| 42 | + throw new OutOfMemoryException( |
| 43 | + "The maximum capacity (" + maxCapacity + ") of the object pool '" + label + "' has been exceeded" |
| 44 | + ); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + GameObject objectInstance = pool[queueStart]; |
| 49 | + objectInstance.SetActive (true); |
| 50 | + queueStart = (queueStart + 1) % pool.Length; |
| 51 | + objectsInPool--; |
| 52 | + return objectInstance; |
| 53 | + } |
| 54 | + |
| 55 | + public void ReturnObject(GameObject usedObject) { |
| 56 | + if (!ready) { |
| 57 | + Init (); |
| 58 | + } |
| 59 | + |
| 60 | + usedObject.SetActive (false); |
| 61 | + pool [queueEnd] = usedObject; |
| 62 | + queueEnd = (queueEnd + 1) % pool.Length; |
| 63 | + objectsInPool++; |
| 64 | + } |
| 65 | + |
| 66 | + public void Init() { |
| 67 | + if (ready) { |
| 68 | + return; |
| 69 | + } |
| 70 | + ready = true; |
| 71 | + |
| 72 | + if (prefab == null) { |
| 73 | + throw new ArgumentNullException ("The prefab game object cannot be null"); |
| 74 | + } |
| 75 | + if (initializer == null) { |
| 76 | + throw new ArgumentNullException ("The game object instance initializer function cannot be null"); |
| 77 | + } |
| 78 | + if (minCapacity <= 0) { |
| 79 | + throw new ArgumentOutOfRangeException ("The minimum capacity must be a positive integer"); |
| 80 | + } |
| 81 | + if (maxCapacity < minCapacity) { |
| 82 | + throw new ArgumentOutOfRangeException ( |
| 83 | + "The maximum capacity (" + maxCapacity + ") cannot be lower than the minimum capacity (" + minCapacity + ")" |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + pool = new GameObject[minCapacity]; |
| 88 | + for (int i = 0; i < minCapacity; i++) { |
| 89 | + AddObjectInstance (); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private void Grow() { |
| 94 | + Debug.LogWarning ( |
| 95 | + "The currenct capacity (" + pool.LongLength + ") of the pool '" + label + |
| 96 | + "' has been exceeded, increasing capacity" |
| 97 | + ); |
| 98 | + |
| 99 | + int newCapacity; |
| 100 | + if (pool.Length > maxCapacity / 2) { |
| 101 | + newCapacity = maxCapacity; |
| 102 | + } else { |
| 103 | + newCapacity = pool.Length * 2; |
| 104 | + } |
| 105 | + int capacityIncrease = newCapacity - pool.Length; |
| 106 | + pool = new GameObject[newCapacity]; |
| 107 | + |
| 108 | + for (int i = 0; i < capacityIncrease; i++) { |
| 109 | + AddObjectInstance (); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private void AddObjectInstance() { |
| 114 | + GameObject objectInstance = initializer (prefab); |
| 115 | + PooledObjectController controller = objectInstance.GetComponent<PooledObjectController> (); |
| 116 | + controller.parentPool = this; |
| 117 | + ReturnObject (objectInstance); |
| 118 | + } |
111 | 119 | }
|
0 commit comments