From 7bac54441e2c602245d267e3a299d750cca775b8 Mon Sep 17 00:00:00 2001 From: Alfred Reinold Baudisch Date: Sat, 9 Nov 2013 13:08:11 -0200 Subject: [PATCH 1/4] Instantiate a custom amount of objects on startup --- Assets/ObjectPool/Scripts/ObjectPool.cs | 36 +++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/Assets/ObjectPool/Scripts/ObjectPool.cs b/Assets/ObjectPool/Scripts/ObjectPool.cs index ac13935..e5415a8 100644 --- a/Assets/ObjectPool/Scripts/ObjectPool.cs +++ b/Assets/ObjectPool/Scripts/ObjectPool.cs @@ -1,6 +1,7 @@ using UnityEngine; using System.Collections; using System.Collections.Generic; +using System.Linq; public sealed class ObjectPool : MonoBehaviour { @@ -16,9 +17,22 @@ public static void Clear() } public static void CreatePool(T prefab) where T : Component + { + CreatePool(prefab, 0); + } + + public static void CreatePool(T prefab, int initialAmount) where T : Component { if (!instance.objectLookup.ContainsKey(prefab)) instance.objectLookup.Add(prefab, new List()); + + if(initialAmount > 0) + { + for(int i = 0; i < initialAmount; i++) + Spawn(prefab); + + RecycleAll(prefab); + } } public static T Spawn(T prefab, Vector3 position, Quaternion rotation) where T : Component @@ -74,6 +88,14 @@ public static void Recycle(T obj) where T : Component Object.Destroy(obj.gameObject); } + public static void RecycleAll(T obj) where T : Component + { + var active = instance.prefabLookup.Keys.Where(p => p.GetType() == typeof(T)).ToList(); + + for(int i = 0; i < active.Count; i++) + Recycle(active[i]); + } + public static int Count(T prefab) where T : Component { if (instance.objectLookup.ContainsKey(prefab)) @@ -102,7 +124,12 @@ public static void CreatePool(this T prefab) where T : Component { ObjectPool.CreatePool(prefab); } - + + public static void CreatePool(this T prefab, int initialAmount) where T : Component + { + ObjectPool.CreatePool(prefab, initialAmount); + } + public static T Spawn(this T prefab, Vector3 position, Quaternion rotation) where T : Component { return ObjectPool.Spawn(prefab, position, rotation); @@ -115,12 +142,17 @@ public static T Spawn(this T prefab) where T : Component { return ObjectPool.Spawn(prefab, Vector3.zero, Quaternion.identity); } - + public static void Recycle(this T obj) where T : Component { ObjectPool.Recycle(obj); } + public static void RecycleAll(this T obj) where T : Component + { + ObjectPool.RecycleAll(obj); + } + public static int Count(T prefab) where T : Component { return ObjectPool.Count(prefab); From f21d05f59fbf289616153e2cdafaf516fb9183b6 Mon Sep 17 00:00:00 2001 From: Alfred Reinold Baudisch Date: Sat, 9 Nov 2013 13:08:46 -0200 Subject: [PATCH 2/4] Create the pools with 10 instatiated bullets and explosions --- Assets/ObjectPool/Demo/Scripts/Turret.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Assets/ObjectPool/Demo/Scripts/Turret.cs b/Assets/ObjectPool/Demo/Scripts/Turret.cs index b466def..579ed66 100644 --- a/Assets/ObjectPool/Demo/Scripts/Turret.cs +++ b/Assets/ObjectPool/Demo/Scripts/Turret.cs @@ -8,8 +8,8 @@ public class Turret : MonoBehaviour void Start() { - bulletPrefab.CreatePool(); - bulletPrefab.explosionPrefab.CreatePool(); + bulletPrefab.CreatePool(10); + bulletPrefab.explosionPrefab.CreatePool(10); } void Update() From 8dfd186fae13f4bd95c849350357f76858d77863 Mon Sep 17 00:00:00 2001 From: Alfred Reinold Baudisch Date: Sun, 10 Nov 2013 23:57:01 -0200 Subject: [PATCH 3/4] GetAllOfType returns all inactive instances of prefab T --- Assets/ObjectPool/Scripts/ObjectPool.cs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Assets/ObjectPool/Scripts/ObjectPool.cs b/Assets/ObjectPool/Scripts/ObjectPool.cs index e5415a8..d74fc7c 100644 --- a/Assets/ObjectPool/Scripts/ObjectPool.cs +++ b/Assets/ObjectPool/Scripts/ObjectPool.cs @@ -96,6 +96,22 @@ public static void RecycleAll(T obj) where T : Component Recycle(active[i]); } + public static List GetAllOfType() where T : Component + { + var keys = instance.objectLookup.Keys.Where(p => p.GetType() == typeof(T)).ToList(); + + List objects = new List(); + + if(keys.Count > 0) + foreach(var key in keys) + if(instance.objectLookup[key].Count > 0) + foreach(var obj in instance.objectLookup[key]) + objects.Add(obj as T); + + + return objects; + } + public static int Count(T prefab) where T : Component { if (instance.objectLookup.ContainsKey(prefab)) @@ -153,6 +169,11 @@ public static void RecycleAll(this T obj) where T : Component ObjectPool.RecycleAll(obj); } + public static List GetAllOfType(this T prefab) where T : Component + { + return ObjectPool.GetAllOfType(); + } + public static int Count(T prefab) where T : Component { return ObjectPool.Count(prefab); From ef54b60eecbe0d7c2d9c6f8dd4b54f6e07aae85e Mon Sep 17 00:00:00 2001 From: Alfred Reinold Baudisch Date: Sun, 10 Nov 2013 23:58:03 -0200 Subject: [PATCH 4/4] SpawnInactive is used instead of Spawn and RecycleAll when creating pools with a pre-defined amount of objects --- Assets/ObjectPool/Scripts/ObjectPool.cs | 38 ++++++++++++++++++------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/Assets/ObjectPool/Scripts/ObjectPool.cs b/Assets/ObjectPool/Scripts/ObjectPool.cs index d74fc7c..980d451 100644 --- a/Assets/ObjectPool/Scripts/ObjectPool.cs +++ b/Assets/ObjectPool/Scripts/ObjectPool.cs @@ -27,12 +27,8 @@ public static void CreatePool(T prefab, int initialAmount) where T : Componen instance.objectLookup.Add(prefab, new List()); if(initialAmount > 0) - { for(int i = 0; i < initialAmount; i++) - Spawn(prefab); - - RecycleAll(prefab); - } + SpawnInactive(prefab); } public static T Spawn(T prefab, Vector3 position, Quaternion rotation) where T : Component @@ -58,6 +54,7 @@ public static T Spawn(T prefab, Vector3 position, Quaternion rotation) where return (T)obj; } } + obj = (T)Object.Instantiate(prefab, position, rotation); instance.prefabLookup.Add(obj, prefab); return (T)obj; @@ -74,15 +71,28 @@ public static T Spawn(T prefab) where T : Component return Spawn(prefab, Vector3.zero, Quaternion.identity); } + public static T SpawnInactive(T prefab) where T : Component + { + if (!instance.objectLookup.ContainsKey(prefab)) + instance.objectLookup.Add(prefab, new List()); + + var obj = (T)Object.Instantiate(prefab, Vector3.zero, Quaternion.identity); + obj.transform.parent = instance.transform; + obj.gameObject.SetActive(false); + + instance.objectLookup[prefab].Add(obj); + + return (T)obj; + } + public static void Recycle(T obj) where T : Component { - if (instance.prefabLookup.ContainsKey(obj)) + if(instance.prefabLookup.ContainsKey(obj)) { instance.objectLookup[instance.prefabLookup[obj]].Add(obj); instance.prefabLookup.Remove(obj); obj.transform.parent = instance.transform; obj.gameObject.SetActive(false); - } else Object.Destroy(obj.gameObject); @@ -92,8 +102,9 @@ public static void RecycleAll(T obj) where T : Component { var active = instance.prefabLookup.Keys.Where(p => p.GetType() == typeof(T)).ToList(); - for(int i = 0; i < active.Count; i++) - Recycle(active[i]); + if(active.Count > 0) + for(int i = 0; i < active.Count; i++) + Recycle(active[i]); } public static List GetAllOfType() where T : Component @@ -145,7 +156,7 @@ public static void CreatePool(this T prefab, int initialAmount) where T : Com { ObjectPool.CreatePool(prefab, initialAmount); } - + public static T Spawn(this T prefab, Vector3 position, Quaternion rotation) where T : Component { return ObjectPool.Spawn(prefab, position, rotation); @@ -159,6 +170,11 @@ public static T Spawn(this T prefab) where T : Component return ObjectPool.Spawn(prefab, Vector3.zero, Quaternion.identity); } + public static T SpawnInactive(this T prefab) where T : Component + { + return ObjectPool.SpawnInactive(prefab); + } + public static void Recycle(this T obj) where T : Component { ObjectPool.Recycle(obj); @@ -178,4 +194,4 @@ public static int Count(T prefab) where T : Component { return ObjectPool.Count(prefab); } -} +} \ No newline at end of file