├── .gitignore ├── Assets ├── ObjectPool.meta └── ObjectPool │ ├── Demo.meta │ ├── Demo │ ├── Materials.meta │ ├── Materials │ │ ├── Grass.mat │ │ ├── Grass.mat.meta │ │ ├── Red.mat │ │ ├── Red.mat.meta │ │ ├── Stone.mat │ │ └── Stone.mat.meta │ ├── Prefabs.meta │ ├── Prefabs │ │ ├── Bullet.prefab │ │ ├── Bullet.prefab.meta │ │ ├── Explosion.prefab │ │ └── Explosion.prefab.meta │ ├── Scenes.meta │ ├── Scenes │ │ ├── ObjectPoolDemo.unity │ │ └── ObjectPoolDemo.unity.meta │ ├── Scripts.meta │ ├── Scripts │ │ ├── Bullet.cs │ │ ├── Bullet.cs.meta │ │ ├── Explosion.cs │ │ ├── Explosion.cs.meta │ │ ├── Turret.cs │ │ └── Turret.cs.meta │ ├── Textures.meta │ └── Textures │ │ ├── Grass.png │ │ ├── Grass.png.meta │ │ ├── Stone.png │ │ └── Stone.png.meta │ ├── Scripts.meta │ └── Scripts │ ├── ObjectPool.cs │ └── ObjectPool.cs.meta ├── LICENSE ├── ObjectPool.userprefs ├── ProjectSettings ├── AudioManager.asset ├── DynamicsManager.asset ├── EditorBuildSettings.asset ├── EditorSettings.asset ├── GraphicsSettings.asset ├── InputManager.asset ├── NavMeshLayers.asset ├── NetworkManager.asset ├── Physics2DSettings.asset ├── ProjectSettings.asset ├── QualitySettings.asset ├── TagManager.asset └── TimeManager.asset └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | [Ll]ibrary/ 2 | [Tt]emp/ 3 | [Oo]bj/ 4 | 5 | # Autogenerated VS/MD solution and project files 6 | *.csproj 7 | *.unityproj 8 | *.sln 9 | -------------------------------------------------------------------------------- /Assets/ObjectPool.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 0f86af003aaa27845baf6d9a803e2a8d 3 | folderAsset: yes 4 | DefaultImporter: 5 | userData: 6 | -------------------------------------------------------------------------------- /Assets/ObjectPool/Demo.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 7aedc71b2127c3c4da9bd2ebf00796e0 3 | folderAsset: yes 4 | DefaultImporter: 5 | userData: 6 | -------------------------------------------------------------------------------- /Assets/ObjectPool/Demo/Materials.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: cdd8fea9ef4e1914a9a5ff316c0dd20a 3 | folderAsset: yes 4 | DefaultImporter: 5 | userData: 6 | -------------------------------------------------------------------------------- /Assets/ObjectPool/Demo/Materials/Grass.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/ObjectPool/51f97081f950a481c6f9ded565a66230ee6c3639/Assets/ObjectPool/Demo/Materials/Grass.mat -------------------------------------------------------------------------------- /Assets/ObjectPool/Demo/Materials/Grass.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 026708813f2426d4886bf43c1c07e1b6 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/ObjectPool/Demo/Materials/Red.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/ObjectPool/51f97081f950a481c6f9ded565a66230ee6c3639/Assets/ObjectPool/Demo/Materials/Red.mat -------------------------------------------------------------------------------- /Assets/ObjectPool/Demo/Materials/Red.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 124a78f42d3f8f649a66b1b90de8de6f 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/ObjectPool/Demo/Materials/Stone.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/ObjectPool/51f97081f950a481c6f9ded565a66230ee6c3639/Assets/ObjectPool/Demo/Materials/Stone.mat -------------------------------------------------------------------------------- /Assets/ObjectPool/Demo/Materials/Stone.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 78ae3d72b30d3a9449b2ee29c7aa43a3 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/ObjectPool/Demo/Prefabs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c299c02548a7fe74d9eed1380b260376 3 | folderAsset: yes 4 | DefaultImporter: 5 | userData: 6 | -------------------------------------------------------------------------------- /Assets/ObjectPool/Demo/Prefabs/Bullet.prefab: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/ObjectPool/51f97081f950a481c6f9ded565a66230ee6c3639/Assets/ObjectPool/Demo/Prefabs/Bullet.prefab -------------------------------------------------------------------------------- /Assets/ObjectPool/Demo/Prefabs/Bullet.prefab.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 6cf85383246094749bdbfab71c166cfe 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/ObjectPool/Demo/Prefabs/Explosion.prefab: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/ObjectPool/51f97081f950a481c6f9ded565a66230ee6c3639/Assets/ObjectPool/Demo/Prefabs/Explosion.prefab -------------------------------------------------------------------------------- /Assets/ObjectPool/Demo/Prefabs/Explosion.prefab.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d9a4643a6bef4c14ba13ca08a3e727c4 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/ObjectPool/Demo/Scenes.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 1690c69bee0bbb447aed507096681bea 3 | folderAsset: yes 4 | DefaultImporter: 5 | userData: 6 | -------------------------------------------------------------------------------- /Assets/ObjectPool/Demo/Scenes/ObjectPoolDemo.unity: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/ObjectPool/51f97081f950a481c6f9ded565a66230ee6c3639/Assets/ObjectPool/Demo/Scenes/ObjectPoolDemo.unity -------------------------------------------------------------------------------- /Assets/ObjectPool/Demo/Scenes/ObjectPoolDemo.unity.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 4b75350a61e92c94c841bba2a8da3f33 3 | DefaultImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/ObjectPool/Demo/Scripts.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: f1fafb1db2eae3e41b32ce97a2d5e893 3 | folderAsset: yes 4 | DefaultImporter: 5 | userData: 6 | -------------------------------------------------------------------------------- /Assets/ObjectPool/Demo/Scripts/Bullet.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public class Bullet : MonoBehaviour 5 | { 6 | public Explosion explosionPrefab; 7 | public float shootDistance; 8 | public float shootSpeed; 9 | 10 | void OnEnable() 11 | { 12 | StartCoroutine(Shoot()); 13 | } 14 | 15 | void OnDisable() 16 | { 17 | StopAllCoroutines(); 18 | } 19 | 20 | IEnumerator Shoot() 21 | { 22 | float travelledDistance = 0; 23 | while (travelledDistance < shootDistance) 24 | { 25 | travelledDistance += shootSpeed * Time.deltaTime; 26 | transform.position += transform.forward * (shootSpeed * Time.deltaTime); 27 | yield return 0; 28 | } 29 | 30 | //Spawn a pooled explosion prefab 31 | explosionPrefab.Spawn(transform.position); 32 | 33 | //Recycle this pooled bullet instance 34 | gameObject.Recycle(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Assets/ObjectPool/Demo/Scripts/Bullet.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 106322755cb46e04f8fca4f90fce9283 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/ObjectPool/Demo/Scripts/Explosion.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public class Explosion : MonoBehaviour 5 | { 6 | public AnimationCurve animationCurve; 7 | public float duration; 8 | 9 | void OnEnable() 10 | { 11 | StartCoroutine(Shrink()); 12 | } 13 | 14 | IEnumerator Shrink() 15 | { 16 | transform.localScale = Vector3.one; 17 | float elapsed = 0; 18 | while (elapsed < duration) 19 | { 20 | float scale = 1 - animationCurve.Evaluate(elapsed / duration); 21 | transform.localScale = new Vector3(scale, scale, scale); 22 | elapsed += Time.deltaTime; 23 | yield return 0; 24 | } 25 | 26 | //Recycle this pooled explosion instance 27 | gameObject.Recycle(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Assets/ObjectPool/Demo/Scripts/Explosion.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 25c0a52a53668b34ebbf75af5e93f5fe 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/ObjectPool/Demo/Scripts/Turret.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public class Turret : MonoBehaviour 5 | { 6 | public Bullet bulletPrefab; 7 | public Transform gun; 8 | 9 | void Update() 10 | { 11 | var plane = new Plane(Vector3.up, transform.position); 12 | var ray = Camera.main.ScreenPointToRay(Input.mousePosition); 13 | float hit; 14 | if (plane.Raycast(ray, out hit)) 15 | { 16 | var aimDirection = Vector3.Normalize(ray.GetPoint(hit) - transform.position); 17 | var targetRotation = Quaternion.LookRotation(aimDirection); 18 | transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, 360 * Time.deltaTime); 19 | 20 | if (Input.GetMouseButtonDown(0)) 21 | bulletPrefab.Spawn(gun.position, gun.rotation); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Assets/ObjectPool/Demo/Scripts/Turret.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 8fc999d750aaca3468580792b3f83985 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/ObjectPool/Demo/Textures.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: e83b3d8218e7b3d459adecdeb69b5ef6 3 | folderAsset: yes 4 | DefaultImporter: 5 | userData: 6 | -------------------------------------------------------------------------------- /Assets/ObjectPool/Demo/Textures/Grass.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/ObjectPool/51f97081f950a481c6f9ded565a66230ee6c3639/Assets/ObjectPool/Demo/Textures/Grass.png -------------------------------------------------------------------------------- /Assets/ObjectPool/Demo/Textures/Grass.png.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: da711322f7fd361479640b562fc29e71 3 | TextureImporter: 4 | serializedVersion: 2 5 | mipmaps: 6 | mipMapMode: 0 7 | enableMipMap: 1 8 | linearTexture: 0 9 | correctGamma: 0 10 | fadeOut: 0 11 | borderMipMap: 0 12 | mipMapFadeDistanceStart: 1 13 | mipMapFadeDistanceEnd: 3 14 | bumpmap: 15 | convertToNormalMap: 0 16 | externalNormalMap: 0 17 | heightScale: .25 18 | normalMapFilter: 0 19 | isReadable: 0 20 | grayScaleToAlpha: 0 21 | generateCubemap: 0 22 | seamlessCubemap: 0 23 | textureFormat: -3 24 | maxTextureSize: 256 25 | textureSettings: 26 | filterMode: -1 27 | aniso: -1 28 | mipBias: -1 29 | wrapMode: -1 30 | nPOTScale: 1 31 | lightmap: 0 32 | compressionQuality: 50 33 | spriteMode: 0 34 | spriteExtrude: 1 35 | alignment: 0 36 | spritePivot: {x: .5, y: .5} 37 | spritePixelsToUnits: 100 38 | alphaIsTransparency: 0 39 | textureType: -1 40 | buildTargetSettings: [] 41 | spriteSheet: 42 | sprites: [] 43 | spriteAtlasHint: 44 | userData: 45 | -------------------------------------------------------------------------------- /Assets/ObjectPool/Demo/Textures/Stone.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/ObjectPool/51f97081f950a481c6f9ded565a66230ee6c3639/Assets/ObjectPool/Demo/Textures/Stone.png -------------------------------------------------------------------------------- /Assets/ObjectPool/Demo/Textures/Stone.png.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 4170921eb8fa2744f96eaa4d2cf85954 3 | TextureImporter: 4 | serializedVersion: 2 5 | mipmaps: 6 | mipMapMode: 0 7 | enableMipMap: 1 8 | linearTexture: 0 9 | correctGamma: 0 10 | fadeOut: 0 11 | borderMipMap: 0 12 | mipMapFadeDistanceStart: 1 13 | mipMapFadeDistanceEnd: 3 14 | bumpmap: 15 | convertToNormalMap: 0 16 | externalNormalMap: 0 17 | heightScale: .25 18 | normalMapFilter: 0 19 | isReadable: 0 20 | grayScaleToAlpha: 0 21 | generateCubemap: 0 22 | seamlessCubemap: 0 23 | textureFormat: -3 24 | maxTextureSize: 256 25 | textureSettings: 26 | filterMode: -1 27 | aniso: -1 28 | mipBias: -1 29 | wrapMode: -1 30 | nPOTScale: 1 31 | lightmap: 0 32 | compressionQuality: 50 33 | spriteMode: 0 34 | spriteExtrude: 1 35 | alignment: 0 36 | spritePivot: {x: .5, y: .5} 37 | spritePixelsToUnits: 100 38 | alphaIsTransparency: 0 39 | textureType: -1 40 | buildTargetSettings: [] 41 | spriteSheet: 42 | sprites: [] 43 | spriteAtlasHint: 44 | userData: 45 | -------------------------------------------------------------------------------- /Assets/ObjectPool/Scripts.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 76511ef137d17024498d4622554afb7a 3 | folderAsset: yes 4 | DefaultImporter: 5 | userData: 6 | -------------------------------------------------------------------------------- /Assets/ObjectPool/Scripts/ObjectPool.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | 5 | public sealed class ObjectPool : MonoBehaviour 6 | { 7 | public enum StartupPoolMode { Awake, Start, CallManually }; 8 | 9 | [System.Serializable] 10 | public class StartupPool 11 | { 12 | public int size; 13 | public GameObject prefab; 14 | } 15 | 16 | static ObjectPool _instance; 17 | static List tempList = new List(); 18 | 19 | Dictionary> pooledObjects = new Dictionary>(); 20 | Dictionary spawnedObjects = new Dictionary(); 21 | 22 | public StartupPoolMode startupPoolMode; 23 | public StartupPool[] startupPools; 24 | 25 | bool startupPoolsCreated; 26 | 27 | void Awake() 28 | { 29 | _instance = this; 30 | if (startupPoolMode == StartupPoolMode.Awake) 31 | CreateStartupPools(); 32 | } 33 | 34 | void Start() 35 | { 36 | if (startupPoolMode == StartupPoolMode.Start) 37 | CreateStartupPools(); 38 | } 39 | 40 | public static void CreateStartupPools() 41 | { 42 | if (!instance.startupPoolsCreated) 43 | { 44 | instance.startupPoolsCreated = true; 45 | var pools = instance.startupPools; 46 | if (pools != null && pools.Length > 0) 47 | for (int i = 0; i < pools.Length; ++i) 48 | CreatePool(pools[i].prefab, pools[i].size); 49 | } 50 | } 51 | 52 | public static void CreatePool(T prefab, int initialPoolSize) where T : Component 53 | { 54 | CreatePool(prefab.gameObject, initialPoolSize); 55 | } 56 | public static void CreatePool(GameObject prefab, int initialPoolSize) 57 | { 58 | if (prefab != null && !instance.pooledObjects.ContainsKey(prefab)) 59 | { 60 | var list = new List(); 61 | instance.pooledObjects.Add(prefab, list); 62 | 63 | if (initialPoolSize > 0) 64 | { 65 | bool active = prefab.activeSelf; 66 | prefab.SetActive(false); 67 | Transform parent = instance.transform; 68 | while (list.Count < initialPoolSize) 69 | { 70 | var obj = (GameObject)Object.Instantiate(prefab); 71 | obj.transform.parent = parent; 72 | list.Add(obj); 73 | } 74 | prefab.SetActive(active); 75 | } 76 | } 77 | } 78 | 79 | public static T Spawn(T prefab, Transform parent, Vector3 position, Quaternion rotation) where T : Component 80 | { 81 | return Spawn(prefab.gameObject, parent, position, rotation).GetComponent(); 82 | } 83 | public static T Spawn(T prefab, Vector3 position, Quaternion rotation) where T : Component 84 | { 85 | return Spawn(prefab.gameObject, null, position, rotation).GetComponent(); 86 | } 87 | public static T Spawn(T prefab, Transform parent, Vector3 position) where T : Component 88 | { 89 | return Spawn(prefab.gameObject, parent, position, Quaternion.identity).GetComponent(); 90 | } 91 | public static T Spawn(T prefab, Vector3 position) where T : Component 92 | { 93 | return Spawn(prefab.gameObject, null, position, Quaternion.identity).GetComponent(); 94 | } 95 | public static T Spawn(T prefab, Transform parent) where T : Component 96 | { 97 | return Spawn(prefab.gameObject, parent, Vector3.zero, Quaternion.identity).GetComponent(); 98 | } 99 | public static T Spawn(T prefab) where T : Component 100 | { 101 | return Spawn(prefab.gameObject, null, Vector3.zero, Quaternion.identity).GetComponent(); 102 | } 103 | public static GameObject Spawn(GameObject prefab, Transform parent, Vector3 position, Quaternion rotation) 104 | { 105 | List list; 106 | Transform trans; 107 | GameObject obj; 108 | if (instance.pooledObjects.TryGetValue(prefab, out list)) 109 | { 110 | obj = null; 111 | if (list.Count > 0) 112 | { 113 | while (obj == null && list.Count > 0) 114 | { 115 | obj = list[0]; 116 | list.RemoveAt(0); 117 | } 118 | if (obj != null) 119 | { 120 | trans = obj.transform; 121 | trans.parent = parent; 122 | trans.localPosition = position; 123 | trans.localRotation = rotation; 124 | obj.SetActive(true); 125 | instance.spawnedObjects.Add(obj, prefab); 126 | return obj; 127 | } 128 | } 129 | obj = (GameObject)Object.Instantiate(prefab); 130 | trans = obj.transform; 131 | trans.parent = parent; 132 | trans.localPosition = position; 133 | trans.localRotation = rotation; 134 | instance.spawnedObjects.Add(obj, prefab); 135 | return obj; 136 | } 137 | else 138 | { 139 | obj = (GameObject)Object.Instantiate(prefab); 140 | trans = obj.GetComponent(); 141 | trans.parent = parent; 142 | trans.localPosition = position; 143 | trans.localRotation = rotation; 144 | return obj; 145 | } 146 | } 147 | public static GameObject Spawn(GameObject prefab, Transform parent, Vector3 position) 148 | { 149 | return Spawn(prefab, parent, position, Quaternion.identity); 150 | } 151 | public static GameObject Spawn(GameObject prefab, Vector3 position, Quaternion rotation) 152 | { 153 | return Spawn(prefab, null, position, rotation); 154 | } 155 | public static GameObject Spawn(GameObject prefab, Transform parent) 156 | { 157 | return Spawn(prefab, parent, Vector3.zero, Quaternion.identity); 158 | } 159 | public static GameObject Spawn(GameObject prefab, Vector3 position) 160 | { 161 | return Spawn(prefab, null, position, Quaternion.identity); 162 | } 163 | public static GameObject Spawn(GameObject prefab) 164 | { 165 | return Spawn(prefab, null, Vector3.zero, Quaternion.identity); 166 | } 167 | 168 | public static void Recycle(T obj) where T : Component 169 | { 170 | Recycle(obj.gameObject); 171 | } 172 | public static void Recycle(GameObject obj) 173 | { 174 | GameObject prefab; 175 | if (instance.spawnedObjects.TryGetValue(obj, out prefab)) 176 | Recycle(obj, prefab); 177 | else 178 | Object.Destroy(obj); 179 | } 180 | static void Recycle(GameObject obj, GameObject prefab) 181 | { 182 | instance.pooledObjects[prefab].Add(obj); 183 | instance.spawnedObjects.Remove(obj); 184 | obj.transform.parent = instance.transform; 185 | obj.SetActive(false); 186 | } 187 | 188 | public static void RecycleAll(T prefab) where T : Component 189 | { 190 | RecycleAll(prefab.gameObject); 191 | } 192 | public static void RecycleAll(GameObject prefab) 193 | { 194 | foreach (var item in instance.spawnedObjects) 195 | if (item.Value == prefab) 196 | tempList.Add(item.Key); 197 | for (int i = 0; i < tempList.Count; ++i) 198 | Recycle(tempList[i]); 199 | tempList.Clear(); 200 | } 201 | public static void RecycleAll() 202 | { 203 | tempList.AddRange(instance.spawnedObjects.Keys); 204 | for (int i = 0; i < tempList.Count; ++i) 205 | Recycle(tempList[i]); 206 | tempList.Clear(); 207 | } 208 | 209 | public static bool IsSpawned(GameObject obj) 210 | { 211 | return instance.spawnedObjects.ContainsKey(obj); 212 | } 213 | 214 | public static int CountPooled(T prefab) where T : Component 215 | { 216 | return CountPooled(prefab.gameObject); 217 | } 218 | public static int CountPooled(GameObject prefab) 219 | { 220 | List list; 221 | if (instance.pooledObjects.TryGetValue(prefab, out list)) 222 | return list.Count; 223 | return 0; 224 | } 225 | 226 | public static int CountSpawned(T prefab) where T : Component 227 | { 228 | return CountSpawned(prefab.gameObject); 229 | } 230 | public static int CountSpawned(GameObject prefab) 231 | { 232 | int count = 0 ; 233 | foreach (var instancePrefab in instance.spawnedObjects.Values) 234 | if (prefab == instancePrefab) 235 | ++count; 236 | return count; 237 | } 238 | 239 | public static int CountAllPooled() 240 | { 241 | int count = 0; 242 | foreach (var list in instance.pooledObjects.Values) 243 | count += list.Count; 244 | return count; 245 | } 246 | 247 | public static List GetPooled(GameObject prefab, List list, bool appendList) 248 | { 249 | if (list == null) 250 | list = new List(); 251 | if (!appendList) 252 | list.Clear(); 253 | List pooled; 254 | if (instance.pooledObjects.TryGetValue(prefab, out pooled)) 255 | list.AddRange(pooled); 256 | return list; 257 | } 258 | public static List GetPooled(T prefab, List list, bool appendList) where T : Component 259 | { 260 | if (list == null) 261 | list = new List(); 262 | if (!appendList) 263 | list.Clear(); 264 | List pooled; 265 | if (instance.pooledObjects.TryGetValue(prefab.gameObject, out pooled)) 266 | for (int i = 0; i < pooled.Count; ++i) 267 | list.Add(pooled[i].GetComponent()); 268 | return list; 269 | } 270 | 271 | public static List GetSpawned(GameObject prefab, List list, bool appendList) 272 | { 273 | if (list == null) 274 | list = new List(); 275 | if (!appendList) 276 | list.Clear(); 277 | foreach (var item in instance.spawnedObjects) 278 | if (item.Value == prefab) 279 | list.Add(item.Key); 280 | return list; 281 | } 282 | public static List GetSpawned(T prefab, List list, bool appendList) where T : Component 283 | { 284 | if (list == null) 285 | list = new List(); 286 | if (!appendList) 287 | list.Clear(); 288 | var prefabObj = prefab.gameObject; 289 | foreach (var item in instance.spawnedObjects) 290 | if (item.Value == prefabObj) 291 | list.Add(item.Key.GetComponent()); 292 | return list; 293 | } 294 | 295 | public static void DestroyPooled(GameObject prefab) 296 | { 297 | List pooled; 298 | if (instance.pooledObjects.TryGetValue(prefab, out pooled)) 299 | { 300 | for (int i = 0; i < pooled.Count; ++i) 301 | GameObject.Destroy(pooled[i]); 302 | pooled.Clear(); 303 | } 304 | } 305 | public static void DestroyPooled(T prefab) where T : Component 306 | { 307 | DestroyPooled(prefab.gameObject); 308 | } 309 | 310 | public static void DestroyAll(GameObject prefab) 311 | { 312 | RecycleAll(prefab); 313 | DestroyPooled(prefab); 314 | } 315 | public static void DestroyAll(T prefab) where T : Component 316 | { 317 | DestroyAll(prefab.gameObject); 318 | } 319 | 320 | public static ObjectPool instance 321 | { 322 | get 323 | { 324 | if (_instance != null) 325 | return _instance; 326 | 327 | _instance = Object.FindObjectOfType(); 328 | if (_instance != null) 329 | return _instance; 330 | 331 | var obj = new GameObject("ObjectPool"); 332 | obj.transform.localPosition = Vector3.zero; 333 | obj.transform.localRotation = Quaternion.identity; 334 | obj.transform.localScale = Vector3.one; 335 | _instance = obj.AddComponent(); 336 | return _instance; 337 | } 338 | } 339 | } 340 | 341 | public static class ObjectPoolExtensions 342 | { 343 | public static void CreatePool(this T prefab) where T : Component 344 | { 345 | ObjectPool.CreatePool(prefab, 0); 346 | } 347 | public static void CreatePool(this T prefab, int initialPoolSize) where T : Component 348 | { 349 | ObjectPool.CreatePool(prefab, initialPoolSize); 350 | } 351 | public static void CreatePool(this GameObject prefab) 352 | { 353 | ObjectPool.CreatePool(prefab, 0); 354 | } 355 | public static void CreatePool(this GameObject prefab, int initialPoolSize) 356 | { 357 | ObjectPool.CreatePool(prefab, initialPoolSize); 358 | } 359 | 360 | public static T Spawn(this T prefab, Transform parent, Vector3 position, Quaternion rotation) where T : Component 361 | { 362 | return ObjectPool.Spawn(prefab, parent, position, rotation); 363 | } 364 | public static T Spawn(this T prefab, Vector3 position, Quaternion rotation) where T : Component 365 | { 366 | return ObjectPool.Spawn(prefab, null, position, rotation); 367 | } 368 | public static T Spawn(this T prefab, Transform parent, Vector3 position) where T : Component 369 | { 370 | return ObjectPool.Spawn(prefab, parent, position, Quaternion.identity); 371 | } 372 | public static T Spawn(this T prefab, Vector3 position) where T : Component 373 | { 374 | return ObjectPool.Spawn(prefab, null, position, Quaternion.identity); 375 | } 376 | public static T Spawn(this T prefab, Transform parent) where T : Component 377 | { 378 | return ObjectPool.Spawn(prefab, parent, Vector3.zero, Quaternion.identity); 379 | } 380 | public static T Spawn(this T prefab) where T : Component 381 | { 382 | return ObjectPool.Spawn(prefab, null, Vector3.zero, Quaternion.identity); 383 | } 384 | public static GameObject Spawn(this GameObject prefab, Transform parent, Vector3 position, Quaternion rotation) 385 | { 386 | return ObjectPool.Spawn(prefab, parent, position, rotation); 387 | } 388 | public static GameObject Spawn(this GameObject prefab, Vector3 position, Quaternion rotation) 389 | { 390 | return ObjectPool.Spawn(prefab, null, position, rotation); 391 | } 392 | public static GameObject Spawn(this GameObject prefab, Transform parent, Vector3 position) 393 | { 394 | return ObjectPool.Spawn(prefab, parent, position, Quaternion.identity); 395 | } 396 | public static GameObject Spawn(this GameObject prefab, Vector3 position) 397 | { 398 | return ObjectPool.Spawn(prefab, null, position, Quaternion.identity); 399 | } 400 | public static GameObject Spawn(this GameObject prefab, Transform parent) 401 | { 402 | return ObjectPool.Spawn(prefab, parent, Vector3.zero, Quaternion.identity); 403 | } 404 | public static GameObject Spawn(this GameObject prefab) 405 | { 406 | return ObjectPool.Spawn(prefab, null, Vector3.zero, Quaternion.identity); 407 | } 408 | 409 | public static void Recycle(this T obj) where T : Component 410 | { 411 | ObjectPool.Recycle(obj); 412 | } 413 | public static void Recycle(this GameObject obj) 414 | { 415 | ObjectPool.Recycle(obj); 416 | } 417 | 418 | public static void RecycleAll(this T prefab) where T : Component 419 | { 420 | ObjectPool.RecycleAll(prefab); 421 | } 422 | public static void RecycleAll(this GameObject prefab) 423 | { 424 | ObjectPool.RecycleAll(prefab); 425 | } 426 | 427 | public static int CountPooled(this T prefab) where T : Component 428 | { 429 | return ObjectPool.CountPooled(prefab); 430 | } 431 | public static int CountPooled(this GameObject prefab) 432 | { 433 | return ObjectPool.CountPooled(prefab); 434 | } 435 | 436 | public static int CountSpawned(this T prefab) where T : Component 437 | { 438 | return ObjectPool.CountSpawned(prefab); 439 | } 440 | public static int CountSpawned(this GameObject prefab) 441 | { 442 | return ObjectPool.CountSpawned(prefab); 443 | } 444 | 445 | public static List GetSpawned(this GameObject prefab, List list, bool appendList) 446 | { 447 | return ObjectPool.GetSpawned(prefab, list, appendList); 448 | } 449 | public static List GetSpawned(this GameObject prefab, List list) 450 | { 451 | return ObjectPool.GetSpawned(prefab, list, false); 452 | } 453 | public static List GetSpawned(this GameObject prefab) 454 | { 455 | return ObjectPool.GetSpawned(prefab, null, false); 456 | } 457 | public static List GetSpawned(this T prefab, List list, bool appendList) where T : Component 458 | { 459 | return ObjectPool.GetSpawned(prefab, list, appendList); 460 | } 461 | public static List GetSpawned(this T prefab, List list) where T : Component 462 | { 463 | return ObjectPool.GetSpawned(prefab, list, false); 464 | } 465 | public static List GetSpawned(this T prefab) where T : Component 466 | { 467 | return ObjectPool.GetSpawned(prefab, null, false); 468 | } 469 | 470 | public static List GetPooled(this GameObject prefab, List list, bool appendList) 471 | { 472 | return ObjectPool.GetPooled(prefab, list, appendList); 473 | } 474 | public static List GetPooled(this GameObject prefab, List list) 475 | { 476 | return ObjectPool.GetPooled(prefab, list, false); 477 | } 478 | public static List GetPooled(this GameObject prefab) 479 | { 480 | return ObjectPool.GetPooled(prefab, null, false); 481 | } 482 | public static List GetPooled(this T prefab, List list, bool appendList) where T : Component 483 | { 484 | return ObjectPool.GetPooled(prefab, list, appendList); 485 | } 486 | public static List GetPooled(this T prefab, List list) where T : Component 487 | { 488 | return ObjectPool.GetPooled(prefab, list, false); 489 | } 490 | public static List GetPooled(this T prefab) where T : Component 491 | { 492 | return ObjectPool.GetPooled(prefab, null, false); 493 | } 494 | 495 | public static void DestroyPooled(this GameObject prefab) 496 | { 497 | ObjectPool.DestroyPooled(prefab); 498 | } 499 | public static void DestroyPooled(this T prefab) where T : Component 500 | { 501 | ObjectPool.DestroyPooled(prefab.gameObject); 502 | } 503 | 504 | public static void DestroyAll(this GameObject prefab) 505 | { 506 | ObjectPool.DestroyAll(prefab); 507 | } 508 | public static void DestroyAll(this T prefab) where T : Component 509 | { 510 | ObjectPool.DestroyAll(prefab.gameObject); 511 | } 512 | } 513 | -------------------------------------------------------------------------------- /Assets/ObjectPool/Scripts/ObjectPool.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 96049f6daef2a4bac8f2c95bfc6a5b7a 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 UnityPatterns 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /ObjectPool.userprefs: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /ProjectSettings/AudioManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/ObjectPool/51f97081f950a481c6f9ded565a66230ee6c3639/ProjectSettings/AudioManager.asset -------------------------------------------------------------------------------- /ProjectSettings/DynamicsManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/ObjectPool/51f97081f950a481c6f9ded565a66230ee6c3639/ProjectSettings/DynamicsManager.asset -------------------------------------------------------------------------------- /ProjectSettings/EditorBuildSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/ObjectPool/51f97081f950a481c6f9ded565a66230ee6c3639/ProjectSettings/EditorBuildSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/EditorSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/ObjectPool/51f97081f950a481c6f9ded565a66230ee6c3639/ProjectSettings/EditorSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/GraphicsSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/ObjectPool/51f97081f950a481c6f9ded565a66230ee6c3639/ProjectSettings/GraphicsSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/InputManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/ObjectPool/51f97081f950a481c6f9ded565a66230ee6c3639/ProjectSettings/InputManager.asset -------------------------------------------------------------------------------- /ProjectSettings/NavMeshLayers.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/ObjectPool/51f97081f950a481c6f9ded565a66230ee6c3639/ProjectSettings/NavMeshLayers.asset -------------------------------------------------------------------------------- /ProjectSettings/NetworkManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/ObjectPool/51f97081f950a481c6f9ded565a66230ee6c3639/ProjectSettings/NetworkManager.asset -------------------------------------------------------------------------------- /ProjectSettings/Physics2DSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/ObjectPool/51f97081f950a481c6f9ded565a66230ee6c3639/ProjectSettings/Physics2DSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/ProjectSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/ObjectPool/51f97081f950a481c6f9ded565a66230ee6c3639/ProjectSettings/ProjectSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/QualitySettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/ObjectPool/51f97081f950a481c6f9ded565a66230ee6c3639/ProjectSettings/QualitySettings.asset -------------------------------------------------------------------------------- /ProjectSettings/TagManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/ObjectPool/51f97081f950a481c6f9ded565a66230ee6c3639/ProjectSettings/TagManager.asset -------------------------------------------------------------------------------- /ProjectSettings/TimeManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/ObjectPool/51f97081f950a481c6f9ded565a66230ee6c3639/ProjectSettings/TimeManager.asset -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ObjectPool 2 | ========== 3 | 4 | Instead of creating and destroying new objects all the time, this script reduces garbage by pooling instances, allowing you to seemingly create hundreds of new objects while only actually using a recycled few. 5 | --------------------------------------------------------------------------------