Hydrogen Framework  1.3.1
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events
hObjectPoolItem.cs
1 #region Copyright Notice & License Information
2 //
3 // hObjectPoolItem.cs
4 //
5 // Author:
6 // Matthew Davey <matthew.davey@dotbunny.com>
7 //
8 // Copyright (c) 2013 dotBunny Inc. (http://www.dotbunny.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining a copy
11 // of this software and associated documentation files (the "Software"), to deal
12 // in the Software without restriction, including without limitation the rights
13 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 // copies of the Software, and to permit persons to whom the Software is
15 // furnished to do so, subject to the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be included in
18 // all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 // THE SOFTWARE.
27 #endregion
28 
29 using System.Collections;
30 using UnityEngine;
31 
32 /// <summary>
33 /// A drop in implementation of the Hydrogen.Core.ObjectPoolItem. This is one possible way of setting up an
34 /// ObjectPoolItem to handle spawning and despawning appropriately.
35 /// </summary>
36 /// <remarks>
37 /// Learn from it, make your own, as long as you extend from the base class
38 /// you still get the performance benefits.
39 /// </remarks>
40 [AddComponentMenu ("Hydrogen/Object Pool Item")]
41 public sealed class hObjectPoolItem : Hydrogen.Core.ObjectPoolItemBase
42 {
43  /// <summary>
44  /// Despawn gameObject after this number of seconds.
45  /// </summary>
46  /// <remarks>In seconds, use 0 to disable.</remarks>
47  public float LifeTime;
48 
49  /// <summary>
50  /// Despawn the gameObject safely after all particles have done their thing.
51  /// </summary>
52  /// <remarks>If you are going to utilize this make sure that your function contains:
53  /// hObjectPool.Instance.objectPools[poolID].DespawnImmediate(gameObject);</remarks>
54  public override void DespawnSafely ()
55  {
56  StartCoroutine (WaitForParticles ());
57  }
58 
59  /// <summary>
60  /// Is the object idle, and therefore can be despawned organically?
61  /// </summary>
62  /// <returns>true</returns>
63  /// <c>false</c>
64  /// <remarks>This will only work on tracked spawned objects.</remarks>
65  public override bool IsInactive ()
66  {
67  // A simple rigidbody check, otherwise no bueno
68  return ParentPool.HasRigidbody && gameObject.rigidbody.IsSleeping ();
69  }
70 
71  /// <summary>
72  /// Raised when the object is 'despawned' back into the pool.
73  /// </summary>
74  /// <remarks>It does not set "active", you must handle that yourself.</remarks>
75  public override void OnDespawned ()
76  {
77  // If our object has a rigidbody (cached check), make sure to zero its velocity.
78  if (ParentPool.HasRigidbody)
79  gameObject.rigidbody.velocity = Vector3.zero;
80 
81  // Disable the gameObject
82  gameObject.SetActive (false);
83  }
84 
85  /// <summary>
86  /// Raised when the object is 'spawned' from the pool.
87  /// </summary>
88  /// <remarks>It does not set "active", you must handle that yourself.</remarks>
89  public override void OnSpawned ()
90  {
91  // Make sure our object is active please and thank you
92  gameObject.SetActive (true);
93 
94  // If there is a LifeTime greater then 0, we set a timer to despawn
95  if (LifeTime > 0)
96  StartCoroutine (DespawnTimer ());
97  }
98 
99  /// <summary>
100  /// Coroutine for despawning our gameObject after the timer value.
101  /// </summary>
102  IEnumerator DespawnTimer ()
103  {
104  yield return new WaitForSeconds (LifeTime);
105  hObjectPool.Instance.Despawn (gameObject, PoolID);
106  }
107 
108  /// <summary>
109  /// Coroutine to wait for particles to be finished, then despawn gameObject.
110  /// </summary>
111  IEnumerator WaitForParticles ()
112  {
113  if (particleEmitter != null) {
114  yield return null;
115  yield return new WaitForEndOfFrame ();
116 
117  while (particleEmitter.particleCount > 0) {
118  yield return null;
119  }
120  particleEmitter.emit = false;
121  } else if (particleSystem != null) {
122  yield return new WaitForSeconds (particleSystem.startDelay + 0.25f);
123  while (particleSystem.IsAlive (true)) {
124  if (!particleSystem.gameObject.activeSelf) {
125  particleSystem.Clear (true);
126  yield break;
127  }
128  yield return null;
129  }
130  }
131 
132  // Disable the gameObject
133  gameObject.SetActive (false);
134 
135  // Immediately get rid of the object as we don't want any artifacts showing up
136  hObjectPool.Instance.ObjectPools [PoolID].DespawnImmediate (gameObject);
137  }
138 }
float LifeTime
Despawn gameObject after this number of seconds.
override void DespawnSafely()
Despawn the gameObject safely after all particles have done their thing.
override bool IsInactive()
Is the object idle, and therefore can be despawned organically?
override void OnDespawned()
Raised when the object is &#39;despawned&#39; back into the pool.
A drop in implementation of the Hydrogen.Core.ObjectPoolItem. This is one possible way of setting up ...
override void OnSpawned()
Raised when the object is &#39;spawned&#39; from the pool.