From 51f97081f950a481c6f9ded565a66230ee6c3639 Mon Sep 17 00:00:00 2001 From: UnityPatterns Date: Wed, 12 Nov 2014 16:08:26 -0800 Subject: [PATCH] Added DestroyPooled/DestroyAll Added two additional functions, one for destroying all pooled objects (thus removing them from the pool), and another which will do that and additionally destroy all spawned instances of a prefab as well. --- Assets/ObjectPool/Scripts/ObjectPool.cs | 43 +++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/Assets/ObjectPool/Scripts/ObjectPool.cs b/Assets/ObjectPool/Scripts/ObjectPool.cs index b1fbbac..fba6c17 100755 --- a/Assets/ObjectPool/Scripts/ObjectPool.cs +++ b/Assets/ObjectPool/Scripts/ObjectPool.cs @@ -292,6 +292,31 @@ public static List GetSpawned(T prefab, List list, bool appendList) whe return list; } + public static void DestroyPooled(GameObject prefab) + { + List pooled; + if (instance.pooledObjects.TryGetValue(prefab, out pooled)) + { + for (int i = 0; i < pooled.Count; ++i) + GameObject.Destroy(pooled[i]); + pooled.Clear(); + } + } + public static void DestroyPooled(T prefab) where T : Component + { + DestroyPooled(prefab.gameObject); + } + + public static void DestroyAll(GameObject prefab) + { + RecycleAll(prefab); + DestroyPooled(prefab); + } + public static void DestroyAll(T prefab) where T : Component + { + DestroyAll(prefab.gameObject); + } + public static ObjectPool instance { get @@ -466,4 +491,22 @@ public static List GetPooled(this T prefab) where T : Component { return ObjectPool.GetPooled(prefab, null, false); } + + public static void DestroyPooled(this GameObject prefab) + { + ObjectPool.DestroyPooled(prefab); + } + public static void DestroyPooled(this T prefab) where T : Component + { + ObjectPool.DestroyPooled(prefab.gameObject); + } + + public static void DestroyAll(this GameObject prefab) + { + ObjectPool.DestroyAll(prefab); + } + public static void DestroyAll(this T prefab) where T : Component + { + ObjectPool.DestroyAll(prefab.gameObject); + } }