-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPool.cs
More file actions
157 lines (131 loc) · 4.7 KB
/
Pool.cs
File metadata and controls
157 lines (131 loc) · 4.7 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using UnityEngine;
using System.Collections.Generic;
/// <summary>
/// Fast implementation of Pool object using array as a stack
/// </summary>
public class Pool
{
#region variables
private int size = 2; // initial size of the pool
private GameObject m_poolObject; // Prefab that will be instatitated
private Transform m_objectsHolder; // Prefab that will be instatitated
private GameObject[] m_poolStack;
private int m_activeCount; // number of objects currently on scene
private int m_inactiveCount; // number of objects in the pool ready for spawning
#endregion
#region getters/setters
/// <summary>
/// Number of active spawned GameObjects
/// </summary>
public int ActiveCount
{
get { return m_activeCount; }
}
/// <summary>
/// Number of inactive GameObjects in pool stack
/// </summary>
public int InactiveCount
{
get { return m_inactiveCount; }
}
#endregion
#region methods
/// <summary>
/// Creates a new Pool for holding game objects.
/// </summary>
/// <param name="pooledObject">GameObject prefab to spawn</param>
/// <param name="parentTransform">Transform parent for spawned objects. If the value is left as null,
/// the spawned objects will be attached to scene root.</param>
public Pool(GameObject pooledObject, Transform parentTransform = null)
{
m_poolStack = new GameObject[size];
m_objectsHolder = parentTransform;
m_poolObject = pooledObject;
}
/// <summary>
/// Spawns the object. Note that the Awake() and Start() will be called only once. OnDisable() and OnEnable() will be called insted,
/// since the method calls SetActive(true) on spawned Object;
/// </summary>
/// <returns>Instance from prefab</returns>
public T Spawn<T>() where T : Component
{
return Spawn<T>(Vector3.zero);
}
/// <summary>
/// Spawns the object with desired position. Note that the Awake() and Start() will be called only once. OnDisable() and OnEnable() will be called insted,
/// since the method calls SetActive(true) on spawned Object;
/// </summary>
/// <param name="position">Position where the object shoul be spawned</param>
/// <returns>Instance from prefab</returns>
public T Spawn<T>(Vector3 position) where T : Component
{
return Spawn<T>(position, Quaternion.identity);
}
/// <summary>
/// Spawns the object with desired position and rotation. Note that the Awake() and Start() will be called only once. OnDisable() and OnEnable() will be called insted,
/// since the method calls SetActive(true) on spawned Object;
/// </summary>
/// <param name="position">Position where the object shoul be spawned</param>
/// <param name="rotation">Rotation of the spawned object</param>
/// <returns></returns>
public T Spawn<T>(Vector3 position, Quaternion rotation) where T : Component
{
GameObject go; // object to return
if (m_inactiveCount == 0)
{
if (m_objectsHolder == null)
{
go = GameObject.Instantiate(m_poolObject, position, rotation);
}
else
{
go = GameObject.Instantiate(m_poolObject, position, rotation, m_objectsHolder);
}
PoolObject po = go.GetComponent<PoolObject>();
if(po == null) po = go.AddComponent<PoolObject>();
po.Init(this);
}
else
{
go = m_poolStack[--m_inactiveCount];
}
// set position and rotation
go.transform.position = position;
go.transform.rotation = rotation;
go.gameObject.SetActive(true);
// increment active count
m_activeCount++;
return go.GetComponent<T>();
}
/// <summary>
/// Returns the GameObject back to pool
/// </summary>
/// <param name="go"></param>
public void ReturnToPool(GameObject go)
{
if (m_inactiveCount == m_poolStack.Length)
{
// Double the pools size if it's full.
EnlargeStack(size * 2);
}
go.SetActive(false);
m_activeCount--; // decrement active count
m_poolStack[m_inactiveCount++] = go;
}
/// <summary>
/// Enlarges the pool stack by provided size
/// </summary>
/// <param name="newSize"></param>
private void EnlargeStack(int newSize)
{
if (newSize <= size) return; // if new size is same or less size
GameObject[] tmp = new GameObject[newSize];
for (int i = 0; i < size; i++)
{
tmp[i] = m_poolStack[i];
}
m_poolStack = tmp;
size = newSize;
}
#endregion
}