Skip to content

Commit 4fa691b

Browse files
authored
switched to spaces, added helper static and extension methods
1 parent bb23e02 commit 4fa691b

File tree

3 files changed

+127
-115
lines changed

3 files changed

+127
-115
lines changed

ObjectPool.cs

Lines changed: 111 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -5,107 +5,115 @@
55

66
[System.Serializable]
77
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+
}
111119
}

PooledObjectController.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,9 @@
44
using UnityEngine;
55

66
public class PooledObjectController : MonoBehaviour {
7-
private ObjectPool parentPool;
7+
[HideInInspector] public ObjectPool parentPool;
88

9-
public void SetParentPool(ObjectPool newParentPool) {
10-
if (parentPool != null) {
11-
throw new InvalidOperationException ("Cannot reconfigure the parent pool reference once it has been set");
12-
}
13-
14-
parentPool = newParentPool;
15-
}
16-
17-
public void ReturnToPool() {
18-
parentPool.ReturnObject (this.gameObject);
19-
}
9+
public void ReturnToPool() {
10+
parentPool.ReturnObject (this.gameObject);
11+
}
2012
}

PooledObjectExtensions.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using UnityEngine;
3+
4+
public static class PooledObjectExtensions {
5+
public static bool IsPooled(this GameObject gameObject) {
6+
return ObjectPool.IsPooled (gameObject);
7+
}
8+
9+
public static void ReturnToPool(this GameObject gameObject) {
10+
ObjectPool.ReturnObjectToPool (gameObject);
11+
}
12+
}

0 commit comments

Comments
 (0)