UnityEssentials
Small but useful tools and features for Unity
PoolEssentials.cs
Go to the documentation of this file.
1 using System.Collections.Generic;
2 
3 namespace UnityEngine
4 {
8  [System.Serializable]
9  public class PoolEssentials
10  {
14  private List<GameObject> referencedObjects = new List<GameObject>();
18  [Tooltip("The prefabs of the GameObjects to be spawned for the pool.")]
19  public GameObject[] baseObjects = new GameObject[1];
23  public int activeIndex { get; private set; }
28  public int nextBaseObjectIndex { get; private set; }
33  [Tooltip("Should the selection of the next base object to instantiate be random? If false, the selection will be made in order looping through the baseObjects array.")]
34  public bool randomInstantiationSequence = false;
38  [Tooltip("The size of the pool. How many referenced objects it han have.")]
39  public int size = 10;
48 
52  public PoolEssentials() {
53  activeIndex = 0;
55  referencedObjects = new List<GameObject>();
56  size = 10;
57  }
58 
67  public PoolEssentials(GameObject[] baseObjects, int size, bool instantiateAllAtCreation = false, bool randomInstantiationSequence = false, int intantiationRandomizationSeed = -1)
68  {
69  this.baseObjects = baseObjects;
70  this.size = size;
71  referencedObjects = new List<GameObject>();
72  activeIndex = 0;
73 
74  this.randomInstantiationSequence = randomInstantiationSequence;
75  if (intantiationRandomizationSeed != -1)
76  this.randomEssentialsInstantiation = new RandomEssentials(intantiationRandomizationSeed);
77 
78  if (instantiateAllAtCreation)
79  for (int i = 0; i < size; i++)
81  }
82 
89  public PoolEssentials(GameObject baseObject, int size, bool instantiateAllAtCreation = false)
90  {
91  this.baseObjects = new GameObject[] {baseObject};
92  this.size = size;
93  referencedObjects = new List<GameObject>();
94  activeIndex = 0;
95 
96  if (instantiateAllAtCreation)
97  for (int i = 0; i < size; i++)
99  }
100 
111  public PoolEssentials(GameObject[] baseObjects, int size, Vector3 instantiationPosition, Quaternion instantiationRotation, bool instantiateAllAtCreation = false, bool randomInstantiationSequence = false, int intantiationRandomizationSeed = -1) : this(baseObjects, size, instantiateAllAtCreation, randomInstantiationSequence, intantiationRandomizationSeed)
112  {
113  defaultPositionAndRotation = new DefaultPositionAndRotation(instantiationPosition, instantiationRotation);
114  }
115 
124  public GameObject Spawn(Vector3 position, Quaternion rotation, Vector3 scale, Transform parent = null)
125  {
128 
129  if ((referencedObjects.Count <= activeIndex) || (referencedObjects[activeIndex] == null))
130  {
131  activeIndex = activeIndex.GetLooped(size); //index = index+1<referencedObjects.Length? index+1 : 0;
132  Debug.LogError(
133  "An error occured trying to spawn the GameObject using the class Pool\nBe sure that your pools are properly configured.");
134  return null;
135  }
136  else
137  {
138  referencedObjects[activeIndex].transform.SetProperties(position, rotation, scale);
139  if (parent != null)
140  referencedObjects[activeIndex].transform.parent = parent;
141  referencedObjects[activeIndex].SetActive(true);
142 
143  GameObject goToReturn = referencedObjects[activeIndex];
144 
145 
146  activeIndex = activeIndex.GetLooped(size); //index = index+1<referencedObjects.Length? index+1 : 0;
147 
148  return goToReturn;
149  }
150  }
151 
157  public void Load(int quantity, Transform parent = null)
158  {
159  int remainingQuantity = Mathf.Min(size - referencedObjects.Count, quantity);
160  for (int i = 0; i < size; i++)
161  {
162  if (InstantiateNewAt(i, parent) != null)
163  remainingQuantity--;
164  if (remainingQuantity <= 0)
165  break;
166  }
167  }
168 
176  private GameObject InstantiateNewAt(int index, Transform parent = null)
177  {
178  if (referencedObjects.Count >= size || (index < referencedObjects.Count && referencedObjects[index] != null))
179  return null;
180 
181  if (index != referencedObjects.Count)
182  Debug.LogWarning($"Trying to instantiate an object in the pool '{this}' but the expected index of the object should be {referencedObjects.Count} and it is {index}.");
183 
184  GameObject nextToInstantiate = GetNextBaseObjectToInstantiate(true);
185 
186  if(nextToInstantiate == null)
187  return null;
188 
189  GameObject go = Object.Instantiate(nextToInstantiate, defaultPositionAndRotation.instantiationPosition, defaultPositionAndRotation.instantiationRotation, parent);
190  go.SetActive(false);
191  referencedObjects.Add(go);
192  return go;
193  }
194 
200  private GameObject GetNextBaseObjectToInstantiate(bool register)
201  {
203  {
204  nextBaseObjectIndex = register
207  }
208  else
209  {
210  nextBaseObjectIndex = register
211  ? nextBaseObjectIndex.GetLooped(baseObjects.Length)
213  }
215  }
216 
222  public GameObject Disable(GameObject gameObject)
223  {
224  if (referencedObjects.Contains(gameObject))
225  {
226  gameObject.SetActive(false);
227  return gameObject;
228  }
229  else
230  {
231  Debug.LogWarning("Trying to disable an object ("+gameObject.name+") from a pool that doesn't belong to.");
232  return null;
233  }
234  }
235 
241  public GameObject Disable(int gameObjectIndexInPool)
242  {
243  return Disable(referencedObjects[gameObjectIndexInPool]);
244  }
245 
249  [System.Serializable]
251  {
255  public Vector3 instantiationPosition = Vector3.zero;
259  public Quaternion instantiationRotation = Quaternion.identity;
260 
262 
264  {
265  this.instantiationPosition = instantiationPosition;
266  this.instantiationRotation = instantiationRotation;
267  }
268  }
269  }
270 
271 
272 
273 }
Allows GameObjects pooling by reusing pre-instantiated GameObjects
GameObject Disable(int gameObjectIndexInPool)
Disables the gameObject in the given index
GameObject InstantiateNewAt(int index, Transform parent=null)
Instantiates an object for the pool.
GameObject Spawn(Vector3 position, Quaternion rotation, Vector3 scale, Transform parent=null)
Activates an object from the pool.
bool randomInstantiationSequence
Should the selection of the next base object to instantiate be random? If false, the selection will b...
GameObject Disable(GameObject gameObject)
Disables the given gameObject if it belongs to the pool.
GameObject[] baseObjects
The prefabs of the GameObjects to be spawned for the pool.
DefaultPositionAndRotation defaultPositionAndRotation
The default position and rotation where the objects are first instantiated
GameObject GetNextBaseObjectToInstantiate(bool register)
Returns the next BaseObject to be spawned
PoolEssentials(GameObject[] baseObjects, int size, bool instantiateAllAtCreation=false, bool randomInstantiationSequence=false, int intantiationRandomizationSeed=-1)
Creates a Pool instance.
PoolEssentials(GameObject[] baseObjects, int size, Vector3 instantiationPosition, Quaternion instantiationRotation, bool instantiateAllAtCreation=false, bool randomInstantiationSequence=false, int intantiationRandomizationSeed=-1)
Creates a Pool instance.
PoolEssentials(GameObject baseObject, int size, bool instantiateAllAtCreation=false)
Creates a Pool instance.
int activeIndex
The index of the next GameObject to be activated/spawned.
List< GameObject > referencedObjects
A reference to the instantiated GameObjects linked to the pool.
RandomEssentials randomEssentialsInstantiation
The randomness generator object used to choose the next base object to instantiate.
int size
The size of the pool. How many referenced objects it han have.
int nextBaseObjectIndex
The index of the base object that is going to be instantiated next.
PoolEssentials()
Creates a Pool instance.
void Load(int quantity, Transform parent=null)
Loads an objet to be later activated/used.
Class to handle the default position and rotation where the objects are first instantiated
DefaultPositionAndRotation(Vector3 instantiationPosition, Quaternion instantiationRotation)
Quaternion instantiationRotation
The default rotation of the new instantiated objects
Vector3 instantiationPosition
The default position where the objects will be instantiated
An easier to use and a feature-rich class to generate pseudo-random results.