Hydrogen Framework  1.3.1
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events
Hydrogen.Core.ObjectPoolCollection Class Reference

The actual pool used by the pooling system, multiple instances of these will be created per unique Prefab or GameObject. More...

Public Member Functions

 ObjectPoolCollection (int preload, bool spawnMore, bool slowMessage, bool handleParticles, bool trackSpawned, bool cullExtras, float cullInterval)
 
void AddToPool ()
 
void Initialize (GameObject prefab, Transform parent, int poolID)
 
void CullUpdate ()
 
void Despawn (GameObject gameObject)
 
void Despawn (GameObject gameObject, bool onSpawn)
 
void DespawnImmediate (GameObject gameObject)
 
bool RemoveFromPool (GameObject gameObject, bool destroyObject)
 
GameObject Spawn ()
 Spawn an object from the pool at origin. More...
 
GameObject Spawn (Vector3 location, Quaternion rotation)
 Spawn an object from the pool at the specified world location and rotation. More...
 

Public Attributes

bool CullExtras
 Should extra objects be culled when not in use? More...
 
float CullInterval
 How often should we look at culling extra objects. More...
 
bool DespawnPoolLocation = true
 Should despawned object be returned to it's pool's origin position? More...
 
bool ManageParticles = true
 Should particle systems be appropriately handled when despawning? More...
 
GameObject Prefab
 Reference to the Prefab or GameObject used by this Object Pool. More...
 
int PreloadAmount
 The number of objects to preload in an Object Pool. More...
 
bool SendMessage
 Should Unity's SendMessage be used. (OnSpawned, WaitToDespawn, OnDespawned) More...
 
bool SpawnMore
 Should additional objects be spawned as needed? More...
 
bool TrackObjects
 Should objects be tracked when they are spawned? More...
 

Properties

bool HasObjectPoolItem [get]
 
bool HasRigidbody [get]
 
bool HasParticleSystem [get]
 
bool HasLegacyParticleAnimator [get]
 
bool HasLegacyParticleEmitter [get]
 
GameObject[] SpawnedObjects [get]
 
bool Initialized [get]
 

Detailed Description

The actual pool used by the pooling system, multiple instances of these will be created per unique Prefab or GameObject.

Definition at line 37 of file ObjectPoolCollection.cs.

Constructor & Destructor Documentation

Hydrogen.Core.ObjectPoolCollection.ObjectPoolCollection ( int  preload,
bool  spawnMore,
bool  slowMessage,
bool  handleParticles,
bool  trackSpawned,
bool  cullExtras,
float  cullInterval 
)

Definition at line 161 of file ObjectPoolCollection.cs.

162  {
163  ManageParticles = handleParticles;
164  PreloadAmount = preload;
165  SpawnMore = spawnMore;
166  SendMessage = slowMessage;
167  TrackObjects = trackSpawned;
168  CullExtras = cullExtras;
169  CullInterval = cullInterval;
170  _cullTimer = cullInterval;
171  }
bool SendMessage
Should Unity's SendMessage be used. (OnSpawned, WaitToDespawn, OnDespawned)
bool ManageParticles
Should particle systems be appropriately handled when despawning?
bool CullExtras
Should extra objects be culled when not in use?
bool SpawnMore
Should additional objects be spawned as needed?
bool TrackObjects
Should objects be tracked when they are spawned?
int PreloadAmount
The number of objects to preload in an Object Pool.
float CullInterval
How often should we look at culling extra objects.

Member Function Documentation

void Hydrogen.Core.ObjectPoolCollection.AddToPool ( )

Definition at line 173 of file ObjectPoolCollection.cs.

174  {
175  var newObject = Object.Instantiate (Prefab) as GameObject;
176  newObject.name = Prefab.name;
177 
178 
179  // Check if there is our helper item on the gameObject
180  if (newObject.GetComponent<ObjectPoolItemBase> ()) {
181  newObject.GetComponent<ObjectPoolItemBase> ().ParentPool = this;
182  newObject.GetComponent<ObjectPoolItemBase> ().PoolID = _poolID;
183  }
184 
185  Array.Add<GameObject> (ref _pooledObjects, newObject, false);
186 
187  Despawn (newObject, true);
188  }
GameObject Prefab
Reference to the Prefab or GameObject used by this Object Pool.
void Despawn(GameObject gameObject)
void Hydrogen.Core.ObjectPoolCollection.CullUpdate ( )

Definition at line 216 of file ObjectPoolCollection.cs.

217  {
218  if (!TrackObjects || !_hasObjectPoolItem)
219  return;
220 
221  _cullTimer -= Time.deltaTime;
222 
223  if (_cullTimer <= 0) {
224  for (int x = 0; x < SpawnedObjects.Length; x++) {
225  if (SpawnedObjects [x].GetComponent<ObjectPoolItemBase> ().IsInactive ()) {
226  Despawn (SpawnedObjects [x]);
227  }
228  }
229  // Establish Timer Again
230  _cullTimer = CullInterval;
231  }
232  }
void Despawn(GameObject gameObject)
bool TrackObjects
Should objects be tracked when they are spawned?
float CullInterval
How often should we look at culling extra objects.
void Hydrogen.Core.ObjectPoolCollection.Despawn ( GameObject  gameObject)

Definition at line 234 of file ObjectPoolCollection.cs.

235  {
236  Despawn (gameObject, false);
237  }
void Despawn(GameObject gameObject)
void Hydrogen.Core.ObjectPoolCollection.Despawn ( GameObject  gameObject,
bool  onSpawn 
)

Definition at line 239 of file ObjectPoolCollection.cs.

240  {
241  if (!ManageParticles)
242  gameObject.transform.parent = _parentTransform;
243 
244  // Has our handler
245  if (_hasObjectPoolItem) {
246  if (ManageParticles) {
247  gameObject.GetComponent<ObjectPoolItemBase> ().DespawnSafely ();
248  return;
249  }
250  gameObject.GetComponent<ObjectPoolItemBase> ().OnDespawned ();
251 
252  }
253  // Use slow method
254  else if (SendMessage) {
255  if (ManageParticles) {
256  gameObject.SendMessage ("WaitToDespawn", SendMessageOptions.DontRequireReceiver);
257  return;
258  }
259  gameObject.SendMessage ("OnDespawned", SendMessageOptions.DontRequireReceiver);
260  }
261  // Fail safe
262  else {
263  // This stops things from keeping their velocity from previous
264  if (_hasRigidbody) {
265  gameObject.rigidbody.velocity = Vector3.zero;
266  gameObject.rigidbody.angularVelocity = Vector3.zero;
267  }
268 
269  gameObject.SetActive (false);
270 
271  }
272 
273  if (DespawnPoolLocation && _parentTransform != null) {
274  gameObject.transform.position = _parentTransform.position;
275  }
276 
277  if (!onSpawn) {
278  if (TrackObjects)
279  Array.Remove<GameObject> (ref _spawnedObjects, gameObject);
280  Array.Add<GameObject> (ref _pooledObjects, gameObject, false);
281  }
282  }
bool SendMessage
Should Unity&#39;s SendMessage be used. (OnSpawned, WaitToDespawn, OnDespawned)
bool ManageParticles
Should particle systems be appropriately handled when despawning?
bool TrackObjects
Should objects be tracked when they are spawned?
bool DespawnPoolLocation
Should despawned object be returned to it&#39;s pool&#39;s origin position?
void Hydrogen.Core.ObjectPoolCollection.DespawnImmediate ( GameObject  gameObject)

Definition at line 284 of file ObjectPoolCollection.cs.

285  {
286  gameObject.transform.parent = _parentTransform;
287  if (TrackObjects)
288  Array.Remove<GameObject> (ref _spawnedObjects, gameObject);
289  Array.Add<GameObject> (ref _pooledObjects, gameObject, false);
290  }
bool TrackObjects
Should objects be tracked when they are spawned?
void Hydrogen.Core.ObjectPoolCollection.Initialize ( GameObject  prefab,
Transform  parent,
int  poolID 
)

Definition at line 190 of file ObjectPoolCollection.cs.

191  {
192  Prefab = prefab;
193 
194  _parentTransform = parent;
195  _poolID = poolID;
196  _pooledObjects = new GameObject[0];
197 
198  _hasObjectPoolItem |= Prefab.GetComponent<ObjectPoolItemBase> ();
199 
200  _hasRigidbody |= prefab.GetComponent<Rigidbody> ();
201 
202  if (ManageParticles) {
203  _hasLegacyParticleEmitter |= prefab.GetComponent<ParticleEmitter> ();
204  _hasLegacyParticleAnimator |= prefab.GetComponent<ParticleAnimator> ();
205  _hasParticleSystem |= prefab.GetComponent<ParticleSystem> ();
206  }
207 
208  ManageParticles &= _hasLegacyParticleAnimator || _hasLegacyParticleEmitter || _hasParticleSystem;
209 
210  for (int x = 0; x < PreloadAmount; x++) {
211  AddToPool ();
212  }
213  _initialized = true;
214  }
GameObject Prefab
Reference to the Prefab or GameObject used by this Object Pool.
bool ManageParticles
Should particle systems be appropriately handled when despawning?
int PreloadAmount
The number of objects to preload in an Object Pool.
bool Hydrogen.Core.ObjectPoolCollection.RemoveFromPool ( GameObject  gameObject,
bool  destroyObject 
)

Definition at line 292 of file ObjectPoolCollection.cs.

293  {
294  if (destroyObject) {
295 
296  Object.DestroyImmediate (gameObject);
297  return Array.Remove<GameObject> (ref _pooledObjects, gameObject);
298  }
299  return Array.Remove<GameObject> (ref _pooledObjects, gameObject);
300 
301  }
GameObject Hydrogen.Core.ObjectPoolCollection.Spawn ( )

Spawn an object from the pool at origin.

Definition at line 306 of file ObjectPoolCollection.cs.

307  {
308  return Spawn (Vector3.zero, Quaternion.identity);
309  }
GameObject Spawn()
Spawn an object from the pool at origin.
GameObject Hydrogen.Core.ObjectPoolCollection.Spawn ( Vector3  location,
Quaternion  rotation 
)

Spawn an object from the pool at the specified world location and rotation.

Parameters
locationSpawn Location
rotationSpawn Rotation

Definition at line 316 of file ObjectPoolCollection.cs.

317  {
318  if (_pooledObjects != null && _pooledObjects.Length > 0) {
319  GameObject spawnedObject = _pooledObjects [0];
320 
321  if (spawnedObject == null) {
322  UnityEngine.Debug.LogError (
323  "[h] A GameObject (" + Prefab.name + ") has been destroyed," +
324  "but is still referenced by the Object Pool. If you must destroy a GameObject manually," +
325  "please make sure to remove it from the pool as well:" +
326  "hObjectPool.Instance.Remove(GameObject);");
327 
328  return null;
329  }
330 
331  Array.RemoveAt<GameObject> (ref _pooledObjects, 0);
332 
333  // Move and rotate before we call on spawned
334  spawnedObject.transform.position = location;
335  spawnedObject.transform.rotation = rotation;
336 
337  if (ManageParticles) {
338  spawnedObject.GetComponent<ParticleAnimator> ().autodestruct &= !_hasLegacyParticleAnimator;
339  spawnedObject.GetComponent<ParticleEmitter> ().emit |= _hasLegacyParticleEmitter;
340  }
341 
342  if (_hasObjectPoolItem) {
343  spawnedObject.GetComponent<ObjectPoolItemBase> ().OnSpawned ();
344  } else if (SendMessage) {
345  spawnedObject.SendMessage ("OnSpawned", SendMessageOptions.DontRequireReceiver);
346  } else {
347  spawnedObject.SetActive (true);
348  }
349 
350  if (TrackObjects) {
351  // No already spawned objects
352  if (_spawnedObjects == null)
353  _spawnedObjects = new GameObject[0];
354 
355  Array.Add<GameObject> (ref _spawnedObjects, spawnedObject, false);
356  }
357 
358  return spawnedObject;
359  }
360  if (SpawnMore) {
361  AddToPool ();
362  return Spawn (location, rotation);
363  }
364  return null;
365  }
bool SendMessage
Should Unity&#39;s SendMessage be used. (OnSpawned, WaitToDespawn, OnDespawned)
bool ManageParticles
Should particle systems be appropriately handled when despawning?
bool SpawnMore
Should additional objects be spawned as needed?
GameObject Spawn()
Spawn an object from the pool at origin.
bool TrackObjects
Should objects be tracked when they are spawned?

Member Data Documentation

bool Hydrogen.Core.ObjectPoolCollection.CullExtras

Should extra objects be culled when not in use?

Definition at line 42 of file ObjectPoolCollection.cs.

float Hydrogen.Core.ObjectPoolCollection.CullInterval

How often should we look at culling extra objects.

Definition at line 46 of file ObjectPoolCollection.cs.

bool Hydrogen.Core.ObjectPoolCollection.DespawnPoolLocation = true

Should despawned object be returned to it's pool's origin position?

Definition at line 50 of file ObjectPoolCollection.cs.

bool Hydrogen.Core.ObjectPoolCollection.ManageParticles = true

Should particle systems be appropriately handled when despawning?

Definition at line 54 of file ObjectPoolCollection.cs.

GameObject Hydrogen.Core.ObjectPoolCollection.Prefab

Reference to the Prefab or GameObject used by this Object Pool.

Definition at line 58 of file ObjectPoolCollection.cs.

int Hydrogen.Core.ObjectPoolCollection.PreloadAmount

The number of objects to preload in an Object Pool.

Definition at line 62 of file ObjectPoolCollection.cs.

bool Hydrogen.Core.ObjectPoolCollection.SendMessage

Should Unity's SendMessage be used. (OnSpawned, WaitToDespawn, OnDespawned)

Definition at line 66 of file ObjectPoolCollection.cs.

bool Hydrogen.Core.ObjectPoolCollection.SpawnMore

Should additional objects be spawned as needed?

Definition at line 70 of file ObjectPoolCollection.cs.

bool Hydrogen.Core.ObjectPoolCollection.TrackObjects

Should objects be tracked when they are spawned?

Useful for when you need to keep track of what objects are in use.

Definition at line 77 of file ObjectPoolCollection.cs.

Property Documentation

bool Hydrogen.Core.ObjectPoolCollection.HasLegacyParticleAnimator
get

Definition at line 141 of file ObjectPoolCollection.cs.

bool Hydrogen.Core.ObjectPoolCollection.HasLegacyParticleEmitter
get

Definition at line 147 of file ObjectPoolCollection.cs.

bool Hydrogen.Core.ObjectPoolCollection.HasObjectPoolItem
get

Definition at line 123 of file ObjectPoolCollection.cs.

bool Hydrogen.Core.ObjectPoolCollection.HasParticleSystem
get

Definition at line 135 of file ObjectPoolCollection.cs.

bool Hydrogen.Core.ObjectPoolCollection.HasRigidbody
get

Definition at line 129 of file ObjectPoolCollection.cs.

bool Hydrogen.Core.ObjectPoolCollection.Initialized
get

Definition at line 155 of file ObjectPoolCollection.cs.

GameObject [] Hydrogen.Core.ObjectPoolCollection.SpawnedObjects
get

Definition at line 153 of file ObjectPoolCollection.cs.