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() diff --git a/Assets/ObjectPool/Scripts/ObjectPool.cs b/Assets/ObjectPool/Scripts/ObjectPool.cs index ac13935..980d451 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,18 @@ 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++) + SpawnInactive(prefab); } public static T Spawn(T prefab, Vector3 position, Quaternion rotation) where T : Component @@ -44,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; @@ -60,20 +71,58 @@ 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); } + public static void RecycleAll(T obj) where T : Component + { + var active = instance.prefabLookup.Keys.Where(p => p.GetType() == typeof(T)).ToList(); + + if(active.Count > 0) + for(int i = 0; i < active.Count; i++) + 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)) @@ -102,6 +151,11 @@ 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 { @@ -115,14 +169,29 @@ 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); } + 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); } -} +} \ No newline at end of file