├── .gitignore ├── LICENSE ├── QuickPool.unitypackage ├── QuickPool ├── Assets │ ├── QuickPool.meta │ └── QuickPool │ │ ├── Demo.meta │ │ ├── Demo │ │ ├── Prefabs.meta │ │ ├── Prefabs │ │ │ ├── Audio Source.prefab │ │ │ ├── Audio Source.prefab.meta │ │ │ ├── Capsule.prefab │ │ │ ├── Capsule.prefab.meta │ │ │ ├── Cube.prefab │ │ │ ├── Cube.prefab.meta │ │ │ ├── Cylinder.prefab │ │ │ ├── Cylinder.prefab.meta │ │ │ ├── Particle System.prefab │ │ │ ├── Particle System.prefab.meta │ │ │ ├── Sphere.prefab │ │ │ ├── Sphere.prefab.meta │ │ │ ├── sound.wav │ │ │ └── sound.wav.meta │ │ ├── Scenes.meta │ │ ├── Scenes │ │ │ ├── DemoScene1.unity │ │ │ ├── DemoScene1.unity.meta │ │ │ ├── DemoScene2(PoolInCustomScript).unity │ │ │ ├── DemoScene2(PoolInCustomScript).unity.meta │ │ │ ├── DemoScene3(Audio).unity │ │ │ └── DemoScene3(Audio).unity.meta │ │ ├── Scripts.meta │ │ └── Scripts │ │ │ ├── DemoScene.cs │ │ │ ├── DemoScene.cs.meta │ │ │ ├── PoolSpawner.cs │ │ │ └── PoolSpawner.cs.meta │ │ ├── Scripts.meta │ │ └── Scripts │ │ ├── Core.meta │ │ ├── Core │ │ ├── Pool.cs │ │ ├── Pool.cs.meta │ │ ├── PoolsManager.cs │ │ └── PoolsManager.cs.meta │ │ ├── Editor.meta │ │ ├── Editor │ │ ├── DespawnerEditor.cs │ │ ├── DespawnerEditor.cs.meta │ │ ├── ObjectsPoolEditor.cs │ │ ├── ObjectsPoolEditor.cs.meta │ │ ├── PoolDrawer.cs │ │ └── PoolDrawer.cs.meta │ │ ├── Utils.meta │ │ └── Utils │ │ ├── DespawnIn.cs │ │ ├── DespawnIn.cs.meta │ │ ├── Extentions.cs │ │ ├── Extentions.cs.meta │ │ ├── Singleton.cs │ │ ├── Singleton.cs.meta │ │ ├── Utils.cs │ │ └── Utils.cs.meta └── ProjectSettings │ ├── AudioManager.asset │ ├── ClusterInputManager.asset │ ├── DynamicsManager.asset │ ├── EditorBuildSettings.asset │ ├── EditorSettings.asset │ ├── GraphicsSettings.asset │ ├── InputManager.asset │ ├── NavMeshAreas.asset │ ├── NetworkManager.asset │ ├── Physics2DSettings.asset │ ├── ProjectSettings.asset │ ├── ProjectVersion.txt │ ├── QualitySettings.asset │ ├── TagManager.asset │ ├── TimeManager.asset │ ├── UnityAdsSettings.asset │ └── UnityConnectSettings.asset └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | [Ll]ibrary/ 2 | [Tt]emp/ 3 | [Oo]bj/ 4 | [Bb]uild/ 5 | 6 | # Autogenerated VS/MD solution and project files 7 | *.csproj 8 | *.unityproj 9 | *.sln 10 | *.suo 11 | *.tmp 12 | *.user 13 | *.userprefs 14 | *.pidb 15 | *.booproj 16 | 17 | # Unity3D generated meta files 18 | *.pidb.meta 19 | 20 | # Unity3D Generated File On Crash Reports 21 | sysinfo.txt 22 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, 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, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /QuickPool.unitypackage: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/densylkin/QuickPool/376e7a515702c4e83757be486b4b58f946c780b9/QuickPool.unitypackage -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9d33cb8b6f1fa45429fc9fd213fbb9d6 3 | folderAsset: yes 4 | timeCreated: 1452974602 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Demo.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 7cc9111d0dba8144fb8df2dc88458f73 3 | folderAsset: yes 4 | timeCreated: 1442337649 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Demo/Prefabs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 46b56ea7aae7c184884a413bc9c673ea 3 | folderAsset: yes 4 | timeCreated: 1453090693 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Demo/Prefabs/Audio Source.prefab: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/densylkin/QuickPool/376e7a515702c4e83757be486b4b58f946c780b9/QuickPool/Assets/QuickPool/Demo/Prefabs/Audio Source.prefab -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Demo/Prefabs/Audio Source.prefab.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 06b8da9d88d39314389a431e7086af73 3 | timeCreated: 1448362843 4 | licenseType: Free 5 | NativeFormatImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Demo/Prefabs/Capsule.prefab: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/densylkin/QuickPool/376e7a515702c4e83757be486b4b58f946c780b9/QuickPool/Assets/QuickPool/Demo/Prefabs/Capsule.prefab -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Demo/Prefabs/Capsule.prefab.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: bdd5cf0434e729b4fb516de0b1998fbb 3 | timeCreated: 1442338343 4 | licenseType: Free 5 | NativeFormatImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Demo/Prefabs/Cube.prefab: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/densylkin/QuickPool/376e7a515702c4e83757be486b4b58f946c780b9/QuickPool/Assets/QuickPool/Demo/Prefabs/Cube.prefab -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Demo/Prefabs/Cube.prefab.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 459c5cd02625bc34b8cef630cdc17673 3 | timeCreated: 1442338346 4 | licenseType: Free 5 | NativeFormatImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Demo/Prefabs/Cylinder.prefab: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/densylkin/QuickPool/376e7a515702c4e83757be486b4b58f946c780b9/QuickPool/Assets/QuickPool/Demo/Prefabs/Cylinder.prefab -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Demo/Prefabs/Cylinder.prefab.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: a5302a7b049b330428071d278060f90b 3 | timeCreated: 1442338342 4 | licenseType: Free 5 | NativeFormatImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Demo/Prefabs/Particle System.prefab: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/densylkin/QuickPool/376e7a515702c4e83757be486b4b58f946c780b9/QuickPool/Assets/QuickPool/Demo/Prefabs/Particle System.prefab -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Demo/Prefabs/Particle System.prefab.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9eebdbdaa2527ea4bb2a48ce27d2b12c 3 | timeCreated: 1448063434 4 | licenseType: Free 5 | NativeFormatImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Demo/Prefabs/Sphere.prefab: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/densylkin/QuickPool/376e7a515702c4e83757be486b4b58f946c780b9/QuickPool/Assets/QuickPool/Demo/Prefabs/Sphere.prefab -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Demo/Prefabs/Sphere.prefab.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: cafb1c0b27af3fd48972882b5f11e847 3 | timeCreated: 1442338348 4 | licenseType: Free 5 | NativeFormatImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Demo/Prefabs/sound.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/densylkin/QuickPool/376e7a515702c4e83757be486b4b58f946c780b9/QuickPool/Assets/QuickPool/Demo/Prefabs/sound.wav -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Demo/Prefabs/sound.wav.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ddb0a62665ca17f4fbafa316973dd9d8 3 | timeCreated: 1453090808 4 | licenseType: Free 5 | AudioImporter: 6 | serializedVersion: 6 7 | defaultSettings: 8 | loadType: 0 9 | sampleRateSetting: 0 10 | sampleRateOverride: 44100 11 | compressionFormat: 1 12 | quality: 1 13 | conversionMode: 0 14 | platformSettingOverrides: {} 15 | forceToMono: 0 16 | normalize: 1 17 | preloadAudioData: 1 18 | loadInBackground: 0 19 | 3D: 1 20 | userData: 21 | assetBundleName: 22 | assetBundleVariant: 23 | -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Demo/Scenes.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: b340c5109f8c21744a74b3b8b1ad5c2f 3 | folderAsset: yes 4 | timeCreated: 1453090671 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Demo/Scenes/DemoScene1.unity: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/densylkin/QuickPool/376e7a515702c4e83757be486b4b58f946c780b9/QuickPool/Assets/QuickPool/Demo/Scenes/DemoScene1.unity -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Demo/Scenes/DemoScene1.unity.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 080782ef7936b9740b0cc266e8dcf000 3 | timeCreated: 1442337664 4 | licenseType: Free 5 | DefaultImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Demo/Scenes/DemoScene2(PoolInCustomScript).unity: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/densylkin/QuickPool/376e7a515702c4e83757be486b4b58f946c780b9/QuickPool/Assets/QuickPool/Demo/Scenes/DemoScene2(PoolInCustomScript).unity -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Demo/Scenes/DemoScene2(PoolInCustomScript).unity.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: a7feec67e04257b4ba7d96449340ac34 3 | timeCreated: 1453085523 4 | licenseType: Free 5 | DefaultImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Demo/Scenes/DemoScene3(Audio).unity: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/densylkin/QuickPool/376e7a515702c4e83757be486b4b58f946c780b9/QuickPool/Assets/QuickPool/Demo/Scenes/DemoScene3(Audio).unity -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Demo/Scenes/DemoScene3(Audio).unity.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 0c49dbb468592ed42afa86589c545905 3 | timeCreated: 1453108675 4 | licenseType: Free 5 | DefaultImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Demo/Scripts.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 241501935bd024c419b4c7e60e7f7e8e 3 | folderAsset: yes 4 | timeCreated: 1453090680 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Demo/Scripts/DemoScene.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using QuickPool; 4 | 5 | [RequireComponent(typeof(PoolsManager))] 6 | public class DemoScene : MonoBehaviour 7 | { 8 | private PoolsManager poolObj; 9 | public GameObject[] prefabs; 10 | 11 | private void Start() 12 | { 13 | poolObj = PoolsManager.Instance; 14 | StartCoroutine(RandomSpawning()); 15 | } 16 | 17 | private void OnGUI() 18 | { 19 | GUILayout.BeginVertical(GUI.skin.box, GUILayout.Width(200)); 20 | foreach(var pool in poolObj.pools) 21 | { 22 | GUILayout.Label("Spawned: " + pool.spawnedCount + "/" + pool.totalCount); 23 | } 24 | if (GUILayout.Button("Despawn all")) 25 | PoolsManager.DespawnAll(); 26 | GUILayout.EndVertical(); 27 | } 28 | 29 | private IEnumerator RandomSpawning() 30 | { 31 | while(true) 32 | { 33 | for (int i = 0; i < prefabs.Length; i++ ) 34 | { 35 | Vector3 randomPos = Random.insideUnitSphere * 10; 36 | prefabs[i].Spawn(randomPos, Quaternion.identity); 37 | yield return new WaitForSeconds(0.1f); 38 | } 39 | yield return new WaitForSeconds(0.2f); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Demo/Scripts/DemoScene.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9205b9ce4d1e1774580fc19b4652c209 3 | timeCreated: 1442337754 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Demo/Scripts/PoolSpawner.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using QuickPool; 4 | 5 | public class PoolSpawner : MonoBehaviour 6 | { 7 | public Pool pool = new Pool() 8 | { 9 | size = 100, 10 | allowGrowth = true 11 | }; 12 | public float delay = 0.1f; 13 | 14 | private void Start() 15 | { 16 | PoolsManager.RegisterPool(pool); 17 | pool.Initialize(); 18 | StartCoroutine(Spawn()); 19 | } 20 | 21 | IEnumerator Spawn() 22 | { 23 | while (true) 24 | { 25 | pool.Spawn(Random.insideUnitSphere * 25, Quaternion.identity); 26 | yield return new WaitForSeconds(delay); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Demo/Scripts/PoolSpawner.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 84135ca9ada26904282c119d8eac39ad 3 | timeCreated: 1453107974 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Scripts.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 0d0adb950fa26b94bb396588c16245bd 3 | folderAsset: yes 4 | timeCreated: 1431896907 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Scripts/Core.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 45ba79cde19897542800828342af165d 3 | folderAsset: yes 4 | timeCreated: 1453004534 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Scripts/Core/Pool.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace QuickPool 6 | { 7 | 8 | public enum DespawnMode 9 | { 10 | Deactivate, 11 | Move 12 | } 13 | 14 | public enum PoolType 15 | { 16 | GameObject, 17 | AudioSource, 18 | ParticleSystem 19 | } 20 | 21 | [System.Flags] 22 | public enum ComponentHandlingType 23 | { 24 | playOnSpawn = (1 << 0), 25 | stopOnDespawn = (1 << 1) 26 | } 27 | 28 | [System.Serializable] 29 | public class Pool 30 | { 31 | #region PublicFields 32 | 33 | public GameObject prefab = null; 34 | public GameObject Prefab 35 | { 36 | get { return prefab; } 37 | set 38 | { 39 | if (Application.isEditor) 40 | { 41 | prefab = value; 42 | if (prefab != null && m_Root != null) 43 | m_Root.name = poolName + "_Root"; 44 | } 45 | } 46 | } 47 | 48 | public string id; 49 | 50 | public DespawnMode mode = DespawnMode.Deactivate; 51 | public Vector3 despawnPos = new Vector3(-100, -100, -100); 52 | public List despawned = new List(); 53 | public List spawned = new List(); 54 | public Transform m_Root; 55 | 56 | public bool allowGrowth = false; 57 | public bool debugMessages = true; 58 | public bool useCustomRoot = false; 59 | 60 | public ComponentHandlingType audioSourceHandling; 61 | public ComponentHandlingType particleSystemHandling; 62 | 63 | public int size; 64 | public bool playOnSpawn; 65 | public bool stopOnDespawn; 66 | #endregion 67 | 68 | #region Properties 69 | 70 | public int spawnedCount { get { return spawned.Count; } } 71 | public int totalCount { get { return spawned.Count + despawned.Count; } } 72 | public int leftCount { get { return despawned.Count; } } 73 | public string poolName { get { return prefab == null ? "None" : prefab.name; } } 74 | public bool Empty { get { return despawned.Count == 0; } } 75 | public bool Audio; 76 | public bool Particles; 77 | public bool GO { get { return !Audio && !Particles; } } 78 | 79 | public PoolType Type 80 | { 81 | get 82 | { 83 | if (prefab.GetComponent() != null) 84 | return PoolType.AudioSource; 85 | else if (prefab.GetComponent() != null) 86 | return PoolType.ParticleSystem; 87 | else 88 | return PoolType.GameObject; 89 | } 90 | } 91 | 92 | #endregion 93 | 94 | #if UNITY_EDITOR 95 | public bool foldout; 96 | #endif 97 | 98 | public Pool() { } 99 | 100 | /// 101 | /// Creates new pool 102 | /// 103 | /// 104 | public Pool(GameObject _prefab) 105 | { 106 | if(_prefab != null) 107 | this.Prefab = _prefab; 108 | size = 1; 109 | Audio = Prefab.GetComponent() != null; 110 | Particles = Prefab.GetComponent() != null; 111 | } 112 | 113 | /// 114 | /// Preinstantiates all objects 115 | /// 116 | public void Initialize() 117 | { 118 | if (prefab == null) 119 | return; 120 | 121 | if (m_Root == null) 122 | m_Root = Utils.CreateRoot(prefab.name).transform; 123 | 124 | for (int i = 0; i < size; i++) 125 | { 126 | if (totalCount > size) 127 | break; 128 | 129 | GameObject obj = GameObject.Instantiate(prefab, m_Root.position, m_Root.rotation) as GameObject; 130 | #if UNITY_EDITOR 131 | UnityEditor.Undo.RegisterCreatedObjectUndo(obj, "instantiated_obj"); 132 | #endif 133 | despawned.Add(obj); 134 | obj.transform.SetParent(m_Root); 135 | if (mode == DespawnMode.Deactivate) 136 | obj.SetActive(false); 137 | else 138 | obj.transform.position = despawnPos; 139 | 140 | obj.name = poolName; 141 | } 142 | 143 | //if (ObjectsPool.Instance.debugMessages && debugMessages) 144 | // Debug.Log("Pool " + poolName + " spawned"); 145 | } 146 | 147 | /// 148 | /// Adds new object to pool if pool is empty 149 | /// 150 | public void AddewObject() 151 | { 152 | if (!Empty || !allowGrowth) 153 | return; 154 | 155 | GameObject obj = GameObject.Instantiate(prefab, m_Root.position, m_Root.rotation) as GameObject; 156 | despawned.Add(obj); 157 | obj.transform.SetParent(m_Root); 158 | obj.SetActive(false); 159 | obj.name = prefab.name; 160 | 161 | if (PoolsManager.Instance.debugMessages && debugMessages) 162 | Debug.Log("New object of " + poolName + "added"); 163 | } 164 | 165 | /// 166 | /// 167 | /// 168 | /// 169 | /// 170 | /// 171 | /// 172 | public T Spawn(Vector3 pos, Quaternion rot) where T : Component 173 | { 174 | return Spawn(pos, rot).GetComponent(); 175 | } 176 | 177 | /// 178 | /// Used to spawn objects 179 | /// 180 | /// 181 | /// 182 | /// 183 | public GameObject Spawn(Vector3 pos, Quaternion rot) 184 | { 185 | var obj = Pop(); 186 | 187 | if (obj == null) 188 | { 189 | if (PoolsManager.Instance.debugMessages) 190 | Debug.Log("No such object left"); 191 | return null; 192 | } 193 | 194 | var objt = obj.transform; 195 | objt.position = pos; 196 | objt.rotation = rot; 197 | 198 | if (PoolsManager.Instance.spawnDespawnMessages) 199 | obj.SendMessage("OnSpawn", SendMessageOptions.DontRequireReceiver); 200 | 201 | if((audioSourceHandling & ComponentHandlingType.playOnSpawn) == ComponentHandlingType.playOnSpawn) 202 | if (Audio) 203 | obj.GetComponent().Play(); 204 | 205 | if((particleSystemHandling & ComponentHandlingType.playOnSpawn) == ComponentHandlingType.playOnSpawn) 206 | if (Particles) 207 | obj.GetComponent().Play(); 208 | 209 | return obj; 210 | } 211 | 212 | /// 213 | /// Used to despawn message 214 | /// 215 | /// 216 | public void Despawn(GameObject target) 217 | { 218 | if (!spawned.Contains(target)) 219 | return; 220 | 221 | Push(target); 222 | 223 | if (PoolsManager.Instance.spawnDespawnMessages) 224 | target.SendMessage("OnDespawn", SendMessageOptions.DontRequireReceiver); 225 | 226 | if ((audioSourceHandling & ComponentHandlingType.stopOnDespawn) == ComponentHandlingType.stopOnDespawn) 227 | if (Audio) 228 | target.GetComponent().Play(); 229 | 230 | if ((particleSystemHandling & ComponentHandlingType.stopOnDespawn) == ComponentHandlingType.stopOnDespawn) 231 | if (Particles) 232 | target.GetComponent().Play(); 233 | } 234 | 235 | /// 236 | /// Gets an item from pool 237 | /// 238 | /// 239 | public GameObject Pop() 240 | { 241 | if (Empty) 242 | { 243 | if (allowGrowth) 244 | { 245 | AddewObject(); 246 | size++; 247 | } 248 | else 249 | return null; 250 | } 251 | 252 | GameObject obj = despawned[0]; 253 | 254 | if (obj == null) 255 | return null; 256 | 257 | despawned.Remove(obj); 258 | spawned.Add(obj); 259 | if (mode == DespawnMode.Deactivate) 260 | obj.SetActive(true); 261 | obj.transform.SetParent(null, false); 262 | 263 | return obj; 264 | } 265 | 266 | /// 267 | /// Send item to pool 268 | /// 269 | /// 270 | public void Push(GameObject obj) 271 | { 272 | if (despawned.Contains(obj) || !spawned.Contains(obj)) 273 | return; 274 | 275 | spawned.Remove(obj); 276 | despawned.Add(obj); 277 | if (mode == DespawnMode.Deactivate) 278 | obj.SetActive(false); 279 | else 280 | obj.transform.position = despawnPos; 281 | obj.transform.SetParent(m_Root, false); 282 | } 283 | 284 | /// 285 | /// Clears and deletes pool 286 | /// 287 | public void ClearAndDestroy() 288 | { 289 | for (int i = 0; i < despawned.Count; i++) 290 | Object.DestroyImmediate(despawned[i]); 291 | 292 | despawned.Clear(); 293 | } 294 | 295 | /// 296 | /// Despawns all spawned objects 297 | /// 298 | public void DespawnAll() 299 | { 300 | for (int i = 0; i < spawned.Count; i++) 301 | Push(spawned[i]); 302 | } 303 | } 304 | } -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Scripts/Core/Pool.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 55f20a0953bd3c34aac513f00eb04837 3 | timeCreated: 1445993435 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Scripts/Core/PoolsManager.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | 6 | namespace QuickPool 7 | { 8 | public class PoolsManager : MonoBehaviour 9 | { 10 | private static PoolsManager instance; 11 | public static PoolsManager Instance 12 | { 13 | get 14 | { 15 | if (instance == null) 16 | instance = FindObjectOfType(); 17 | if (instance == null) 18 | instance = new GameObject("Pool manager").AddComponent(); 19 | 20 | return instance; 21 | } 22 | } 23 | 24 | public List pools = new List(); 25 | public bool debugMessages = true; 26 | public bool spawnDespawnMessages = true; 27 | 28 | public Pool this[string name] 29 | { 30 | get { return pools.Find(pool => pool.poolName == name); } 31 | 32 | } 33 | 34 | public List Prefabs 35 | { 36 | get 37 | { 38 | return pools.Select(pool => pool.Prefab).ToList(); 39 | } 40 | } 41 | 42 | /// 43 | /// Initialization 44 | /// 45 | private void Start() 46 | { 47 | //pools.ForEach(pool => pool.PreInstantiate()); 48 | for (int i = 0; i < pools.Count; i++) 49 | pools[i].Initialize(); 50 | } 51 | 52 | public static Pool CreatePool(GameObject prefabs) 53 | { 54 | var pool = new Pool(prefabs); 55 | RegisterPool(pool); 56 | return pool; 57 | } 58 | 59 | /// 60 | /// Spawns an object from specified pool 61 | /// 62 | /// Pool name 63 | /// Target position 64 | /// Target rotation 65 | /// 66 | public static GameObject Spawn(string name, Vector3 pos, Quaternion rot) 67 | { 68 | Pool targetPool = PoolsManager.Instance[name]; 69 | 70 | if (targetPool == null) 71 | return null; 72 | 73 | GameObject obj = targetPool.Pop(); 74 | 75 | if (obj == null) 76 | { 77 | if (PoolsManager.Instance.debugMessages) 78 | Debug.Log("No such object left"); 79 | return null; 80 | } 81 | 82 | obj.transform.position = pos; 83 | obj.transform.rotation = rot; 84 | 85 | if (PoolsManager.Instance.spawnDespawnMessages) 86 | obj.SendMessage("OnSpawn", SendMessageOptions.DontRequireReceiver); 87 | 88 | return obj; 89 | } 90 | 91 | /// 92 | /// Spawns an object from specified pool 93 | /// 94 | /// Pool name 95 | /// Target position 96 | /// Target rotation 97 | /// 98 | public static GameObject Spawn(GameObject prefab, Vector3 pos, Quaternion rot) 99 | { 100 | Pool targetPool = PoolsManager.Instance.pools.Where(pool => pool.Prefab == prefab).FirstOrDefault(); 101 | 102 | if (targetPool == null) 103 | return null; 104 | 105 | GameObject obj = targetPool.Pop(); 106 | 107 | if (obj == null) 108 | return null; 109 | 110 | obj.transform.position = pos; 111 | obj.transform.rotation = rot; 112 | 113 | if (PoolsManager.Instance.spawnDespawnMessages) 114 | obj.SendMessage("OnSpawn", SendMessageOptions.DontRequireReceiver); 115 | return obj; 116 | } 117 | 118 | /// 119 | /// Hides object 120 | /// 121 | /// Target 122 | public static void Despawn(GameObject target) 123 | { 124 | if (PoolsManager.Instance.spawnDespawnMessages) 125 | target.SendMessage("OnDespawn", SendMessageOptions.DontRequireReceiver); 126 | 127 | Pool targetPool = PoolsManager.Instance.pools.Where(pool => pool.spawned.Contains(target)).FirstOrDefault(); 128 | 129 | targetPool.Despawn(target); 130 | } 131 | 132 | public static void DespawnAll() 133 | { 134 | for (int i = 0; i < PoolsManager.Instance.pools.Count; i++) 135 | PoolsManager.Instance.pools[i].DespawnAll(); 136 | } 137 | 138 | public static void RegisterPool(Pool target) 139 | { 140 | if (!Instance.pools.Contains(target)) 141 | Instance.pools.Add(target); 142 | } 143 | 144 | public static void RemovePool(string name) 145 | { 146 | var pool = Instance[name]; 147 | if (pool != null) 148 | Instance.pools.Remove(pool); 149 | } 150 | } 151 | } -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Scripts/Core/PoolsManager.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 92d2439b4e9b94044b8a0b5109cc13dd 3 | timeCreated: 1431896944 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Scripts/Editor.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 5f7abdc33ae96ff49ae9ab298037ce75 3 | folderAsset: yes 4 | timeCreated: 1431896913 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Scripts/Editor/DespawnerEditor.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public class DespawnerEditor : MonoBehaviour 5 | { 6 | 7 | } 8 | -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Scripts/Editor/DespawnerEditor.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 8d0a5010417935049b1f32a57cbba29b 3 | timeCreated: 1453063128 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Scripts/Editor/ObjectsPoolEditor.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using UnityEditor; 4 | using System.Linq; 5 | using System.Collections.Generic; 6 | 7 | namespace QuickPool 8 | { 9 | [CustomEditor(typeof(PoolsManager))] 10 | public class ObjectsPoolEditor : Editor 11 | { 12 | private PoolsManager poolManager { get { return (PoolsManager)target; } } 13 | 14 | private GUIStyle background; 15 | private GUIStyle poolBackground; 16 | private GUIStyle dropBox; 17 | //private Texture2D particlesIcon; 18 | 19 | private string searchStr = ""; 20 | 21 | private void OnEnable() 22 | { 23 | background = new GUIStyle(); 24 | poolBackground = new GUIStyle(); 25 | dropBox = new GUIStyle(); 26 | 27 | background.normal.background = MakeTex(new Color(0.5f, 0.5f, 0.5f, 0.5f)); 28 | poolBackground.normal.background = MakeTex(new Color(0.3f, 0.3f, 0.3f, 0.5f)); 29 | dropBox.normal.background = MakeTex(new Color(1, 1, 1, 0.5f)); 30 | 31 | poolBackground.margin = new RectOffset(2, 2, 2, 2); 32 | dropBox.margin = new RectOffset(4, 4, 4, 4); 33 | 34 | dropBox.alignment = TextAnchor.MiddleCenter; 35 | 36 | dropBox.fontSize = 14; 37 | 38 | dropBox.normal.textColor = Color.black; 39 | 40 | //particlesIcon = Resources.Load("particles"); 41 | } 42 | 43 | public override void OnInspectorGUI() 44 | { 45 | Undo.RecordObject(poolManager, "poolmanager"); 46 | 47 | PoolManagerSettings(); 48 | Toolbar(); 49 | 50 | EditorUtility.SetDirty(poolManager); 51 | } 52 | 53 | private void PoolManagerSettings() 54 | { 55 | GUILayout.BeginHorizontal(EditorStyles.helpBox); 56 | 57 | poolManager.debugMessages = EditorGUILayout.Toggle("Show debug ", poolManager.debugMessages); 58 | poolManager.spawnDespawnMessages = EditorGUILayout.Toggle("Send messages ", poolManager.spawnDespawnMessages); 59 | 60 | GUILayout.EndHorizontal(); 61 | } 62 | 63 | private void Toolbar() 64 | { 65 | GUILayout.Space(10f); 66 | DropArea(); 67 | GUILayout.BeginHorizontal(GUI.skin.FindStyle("Toolbar"), GUILayout.ExpandWidth(true)); 68 | GUILayout.Label("Pools (" + poolManager.pools.Count + ")"); 69 | SearchField(); 70 | 71 | if (GUILayout.Button("Expand All", EditorStyles.toolbarButton, GUILayout.Width(65))) 72 | poolManager.pools.ForEach(pool => pool.foldout = true); 73 | 74 | if (GUILayout.Button("Collapse All", EditorStyles.toolbarButton, GUILayout.Width(71))) 75 | poolManager.pools.ForEach(pool => pool.foldout = false); 76 | 77 | GUILayout.EndHorizontal(); 78 | GUILayout.BeginVertical(); 79 | 80 | var results = searchStr == "" ? poolManager.pools : poolManager.pools.Where(pool => pool.Prefab.name.Contains(searchStr)).ToList(); 81 | 82 | for (int i = 0; i < results.Count; i++) 83 | { 84 | Pool pool = results[i]; 85 | GUILayout.BeginHorizontal(); 86 | GUILayout.BeginVertical(); 87 | PoolArea(pool); 88 | GUILayout.EndVertical(); 89 | GUILayout.EndHorizontal(); 90 | } 91 | GUILayout.EndVertical(); 92 | } 93 | 94 | private void PoolArea(Pool pool) 95 | { 96 | GUILayout.BeginVertical(poolBackground); 97 | GUILayout.BeginHorizontal(EditorStyles.toolbar); 98 | GUILayout.Space(10f); 99 | 100 | pool.foldout = EditorGUILayout.Foldout(pool.foldout, pool.poolName); 101 | GUILayout.FlexibleSpace(); 102 | 103 | if (Application.isPlaying) 104 | GUILayout.Label("Spawned: " + pool.spawnedCount + "/" + pool.totalCount); 105 | 106 | if (GUILayout.Button("Clear", EditorStyles.toolbarButton, GUILayout.Width(40))) 107 | pool.ClearAndDestroy(); 108 | 109 | if (GUILayout.Button("Preinstantiate", EditorStyles.toolbarButton, GUILayout.Width(80))) 110 | pool.Initialize(); 111 | 112 | if (GUILayout.Button("X", EditorStyles.toolbarButton, GUILayout.Width(15))) 113 | { 114 | pool.ClearAndDestroy(); 115 | if (pool.m_Root != null) 116 | GameObject.DestroyImmediate(pool.m_Root.gameObject); 117 | poolManager.pools.Remove(pool); 118 | } 119 | 120 | GUILayout.EndHorizontal(); 121 | 122 | if (pool.foldout) 123 | { 124 | 125 | 126 | pool.Prefab = EditorGUILayout.ObjectField("Prefab: ", pool.Prefab, typeof(GameObject), false) as GameObject; 127 | pool.size = EditorGUILayout.IntField("Pool size: ", pool.size); 128 | pool.allowGrowth = EditorGUILayout.Toggle("Allow more: ", pool.allowGrowth); 129 | 130 | pool.audioSourceHandling = (ComponentHandlingType)EditorGUILayout.EnumMaskField("Audio source: ", pool.audioSourceHandling); 131 | pool.particleSystemHandling = (ComponentHandlingType)EditorGUILayout.EnumMaskField("Particles: ", pool.particleSystemHandling); 132 | 133 | pool.mode = (DespawnMode)EditorGUILayout.EnumPopup("Despawn mode", pool.mode); 134 | if (pool.mode == DespawnMode.Move) 135 | pool.despawnPos = EditorGUILayout.Vector3Field("Despawn position: ", pool.despawnPos); 136 | } 137 | 138 | GUILayout.EndVertical(); 139 | } 140 | 141 | private void DropArea() 142 | { 143 | GUILayout.Box("Drop prefabs here", dropBox, GUILayout.ExpandWidth(true), GUILayout.Height(35)); 144 | 145 | EventType eventType = Event.current.type; 146 | bool isAccepted = false; 147 | 148 | if (eventType == EventType.DragUpdated || eventType == EventType.DragPerform) 149 | { 150 | DragAndDrop.visualMode = DragAndDropVisualMode.Copy; 151 | 152 | if (eventType == EventType.DragPerform) 153 | { 154 | DragAndDrop.AcceptDrag(); 155 | isAccepted = true; 156 | } 157 | Event.current.Use(); 158 | } 159 | 160 | if (isAccepted) 161 | { 162 | var pools = DragAndDrop.objectReferences 163 | .Where(obj => obj.GetType() == typeof(GameObject)) 164 | .Cast() 165 | .Where(obj => PrefabUtility.GetPrefabType(obj) == PrefabType.Prefab) 166 | .Except(poolManager.Prefabs) 167 | .Select(obj => new Pool(obj)); 168 | 169 | poolManager.pools.AddRange(pools); 170 | } 171 | } 172 | 173 | private void SearchField() 174 | { 175 | 176 | searchStr = GUILayout.TextField(searchStr, GUI.skin.FindStyle("ToolbarSeachTextField"), GUILayout.ExpandWidth(true), GUILayout.MinWidth(150)); 177 | if (GUILayout.Button("", GUI.skin.FindStyle("ToolbarSeachCancelButton"))) 178 | { 179 | // Remove focus if cleared 180 | searchStr = ""; 181 | GUI.FocusControl(null); 182 | } 183 | } 184 | 185 | private Texture2D MakeTex(Color col) 186 | { 187 | Color[] pix = new Color[1 * 1]; 188 | 189 | for (int i = 0; i < pix.Length; i++) 190 | pix[i] = col; 191 | 192 | Texture2D result = new Texture2D(1, 1, TextureFormat.ARGB32, false); 193 | result.hideFlags = HideFlags.HideAndDontSave; 194 | result.SetPixels(pix); 195 | result.Apply(); 196 | 197 | return result; 198 | } 199 | } 200 | } -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Scripts/Editor/ObjectsPoolEditor.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 69e5ebbb21be1b7409aa6f2cff16c178 3 | timeCreated: 1436049510 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Scripts/Editor/PoolDrawer.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using UnityEditor; 3 | using System.Collections; 4 | using System.Reflection; 5 | 6 | namespace QuickPool 7 | { 8 | [CustomPropertyDrawer(typeof(Pool))] 9 | public class PoolDrawer : PropertyDrawer 10 | { 11 | private float lineHeight { get { return EditorGUIUtility.singleLineHeight; } } 12 | 13 | private DespawnMode despawnMode; 14 | bool useCustomRoot; 15 | 16 | public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) 17 | { 18 | SerializedProperty prefabProperty = property.FindPropertyRelative("prefab"); 19 | SerializedProperty sizeProperty = property.FindPropertyRelative("size"); 20 | SerializedProperty allowGrowthProperty = property.FindPropertyRelative("allowGrowth"); 21 | SerializedProperty useCustomRootProperty = property.FindPropertyRelative("useCustomRoot"); 22 | SerializedProperty rootProperty = property.FindPropertyRelative("m_Root"); 23 | SerializedProperty despawnModeProperty = property.FindPropertyRelative("mode"); 24 | SerializedProperty despawnPosProperty = property.FindPropertyRelative("despawnPos"); 25 | SerializedProperty audioSourceHandlingProperty = property.FindPropertyRelative("audioSourceHandling"); 26 | SerializedProperty particleSystemHandlingProperty = property.FindPropertyRelative("particleSystemHandling"); 27 | 28 | GameObject prefab = (GameObject)prefabProperty.objectReferenceValue; 29 | string prefabName = prefab != null ? prefab.name : "None"; 30 | despawnMode = (DespawnMode)despawnModeProperty.enumValueIndex; 31 | useCustomRoot = useCustomRootProperty.boolValue; 32 | 33 | //base.OnGUI(position, property, label); 34 | DrawHeader(position, prefabName, property); 35 | GUI.Box(position, "", GUI.skin.box); 36 | position.y += 5; 37 | if (property.isExpanded) 38 | { 39 | DrawField(position, prefabProperty, 1); 40 | DrawField(position, sizeProperty, 2); 41 | DrawField(position, allowGrowthProperty, 3); 42 | DrawEnumMaskField(position, audioSourceHandlingProperty, 4, "Audio Source"); 43 | DrawEnumMaskField(position, particleSystemHandlingProperty, 5, "Particle system"); 44 | DrawField(position, useCustomRootProperty, 6); 45 | if (useCustomRoot) 46 | DrawField(position, rootProperty, 7); 47 | DrawField(position, despawnModeProperty, useCustomRootProperty.boolValue ? 8 : 7); 48 | if (despawnMode == DespawnMode.Move) 49 | DrawField(position, despawnPosProperty, useCustomRootProperty.boolValue ? 9 : 8); 50 | } 51 | } 52 | 53 | public override float GetPropertyHeight(SerializedProperty property, GUIContent label) 54 | { 55 | 56 | var pool = GetPoolObject(property); 57 | 58 | float height = lineHeight; 59 | if(property.isExpanded) 60 | { 61 | height += lineHeight * 7; 62 | if (pool.useCustomRoot) 63 | height += lineHeight; 64 | if (pool.mode == DespawnMode.Move) 65 | height += lineHeight; 66 | } 67 | return height + 2; 68 | } 69 | 70 | private void DrawHeader(Rect position, string poolName, SerializedProperty property) 71 | { 72 | var headerPosition = new Rect(position.x, position.y, position.width, 20); 73 | GUI.Box(headerPosition, "", EditorStyles.toolbar); 74 | 75 | var headerLabel = new Rect(headerPosition.x + 15, headerPosition.y, headerPosition.width - 140, headerPosition.height); 76 | property.isExpanded = EditorGUI.Foldout(headerLabel, property.isExpanded, poolName); 77 | 78 | var clearBtnRect = new Rect(headerLabel.x + headerLabel.width, headerLabel.y, 40, headerLabel.height); 79 | 80 | if (GUI.Button(clearBtnRect, "Clear", EditorStyles.toolbarButton)) 81 | GetPoolObject(property).ClearAndDestroy(); 82 | 83 | var preinstantiateBtnRect = new Rect(headerLabel.x + headerLabel.width + 40, headerLabel.y, 80, headerLabel.height); 84 | 85 | if (GUI.Button(preinstantiateBtnRect, "Preinstantiate", EditorStyles.toolbarButton)) 86 | GetPoolObject(property).Initialize(); 87 | 88 | } 89 | 90 | private void DrawField(Rect position, SerializedProperty property, int index) 91 | { 92 | var fieldRect = new Rect(position.x + 5, position.y + lineHeight * index, position.width - 5, lineHeight); 93 | EditorGUI.PropertyField(fieldRect, property); 94 | } 95 | 96 | private void DrawEnumMaskField(Rect position, SerializedProperty property, int index, string text) 97 | { 98 | var fieldRect = new Rect(position.x + 5, position.y + lineHeight * index, position.width - 5, lineHeight); 99 | property.intValue = Utils.DrawBitMaskField(fieldRect, property.intValue, typeof(ComponentHandlingType), new GUIContent(text)); 100 | } 101 | 102 | 103 | private Pool GetPoolObject(SerializedProperty property) 104 | { 105 | var obj = property.serializedObject.targetObject; 106 | var filedName = fieldInfo.Name; 107 | var type = obj.GetType(); 108 | 109 | return (Pool)type.GetField(filedName).GetValue(obj); 110 | } 111 | } 112 | 113 | public class PropertyField 114 | { 115 | 116 | public void Draw() 117 | { 118 | 119 | } 120 | } 121 | } -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Scripts/Editor/PoolDrawer.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9dd935e09f28e3744bad71a28a1b9693 3 | timeCreated: 1452987014 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Scripts/Utils.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 64a2069d23285064283cab5150ff1b68 3 | folderAsset: yes 4 | timeCreated: 1453004520 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Scripts/Utils/DespawnIn.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | namespace QuickPool 5 | { 6 | public class DespawnIn : MonoBehaviour 7 | { 8 | public float time; 9 | 10 | public void OnSpawn() 11 | { 12 | StartCoroutine("Destroy"); 13 | } 14 | 15 | private IEnumerator Destroy() 16 | { 17 | yield return new WaitForSeconds(time); 18 | gameObject.Despawn(); 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Scripts/Utils/DespawnIn.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: b093e9f1573506a4687a816e23f05183 3 | timeCreated: 1436393164 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Scripts/Utils/Extentions.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | namespace QuickPool 5 | { 6 | public static class Extentions 7 | { 8 | public static GameObject Spawn(this GameObject prefabToSpawn, Vector3 pos, Quaternion rot) 9 | { 10 | return PoolsManager.Spawn(prefabToSpawn, pos, rot); 11 | } 12 | 13 | public static T Spawn(this GameObject prefabToSpawn, Vector3 pos, Quaternion rot) where T : Component 14 | { 15 | return PoolsManager.Spawn(prefabToSpawn, pos, rot).GetComponent(); 16 | } 17 | 18 | public static void Despawn(this GameObject objToDespawn) 19 | { 20 | PoolsManager.Despawn(objToDespawn); 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Scripts/Utils/Extentions.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d11f97709086b2b4ba97a94927da2bb1 3 | timeCreated: 1446014865 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Scripts/Utils/Singleton.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public class Singleton : MonoBehaviour where T : MonoBehaviour 5 | { 6 | protected static T instance; 7 | 8 | public static T Instance 9 | { 10 | get 11 | { 12 | if (instance == null) 13 | { 14 | instance = FindObjectOfType(); 15 | 16 | if (instance == null) 17 | { 18 | Debug.LogError("An instance of " + typeof(T) + " is needed in the scene, but there is none."); 19 | } 20 | } 21 | 22 | return instance; 23 | } 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Scripts/Utils/Singleton.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 0aa62cd719118bb4b93bfee4ad71ca47 3 | timeCreated: 1442337630 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Scripts/Utils/Utils.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | #if UNITY_EDITOR 3 | using UnityEditor; 4 | #endif 5 | using System.Collections; 6 | using System.Linq; 7 | 8 | namespace QuickPool 9 | { 10 | public static class Utils 11 | { 12 | public static GameObject CreateRoot(string name) 13 | { 14 | var poolsRoot = GameObject.Find("Instances"); 15 | 16 | if (poolsRoot == null) 17 | poolsRoot = new GameObject("Instances"); 18 | 19 | var root = new GameObject(name + "_Root"); 20 | root.transform.SetParent(poolsRoot.transform); 21 | return root; 22 | 23 | } 24 | #if UNITY_EDITOR 25 | public static int DrawBitMaskField(Rect aPosition, int aMask, System.Type aType, GUIContent aLabel) 26 | { 27 | var itemNames = System.Enum.GetNames(aType); 28 | var itemValues = System.Enum.GetValues(aType) as int[]; 29 | 30 | int val = aMask; 31 | int maskVal = 0; 32 | for (int i = 0; i < itemValues.Length; i++) 33 | { 34 | if (itemValues[i] != 0) 35 | { 36 | if ((val & itemValues[i]) == itemValues[i]) 37 | maskVal |= 1 << i; 38 | } 39 | else if (val == 0) 40 | maskVal |= 1 << i; 41 | } 42 | int newMaskVal = EditorGUI.MaskField(aPosition, aLabel, maskVal, itemNames); 43 | int changes = maskVal ^ newMaskVal; 44 | 45 | for (int i = 0; i < itemValues.Length; i++) 46 | { 47 | if ((changes & (1 << i)) != 0) 48 | { 49 | if ((newMaskVal & (1 << i)) != 0) 50 | { 51 | if (itemValues[i] == 0) 52 | { 53 | val = 0; 54 | break; 55 | } 56 | else 57 | val |= itemValues[i]; 58 | } 59 | else 60 | { 61 | val &= ~itemValues[i]; 62 | } 63 | } 64 | } 65 | return val; 66 | } 67 | #endif 68 | } 69 | 70 | } -------------------------------------------------------------------------------- /QuickPool/Assets/QuickPool/Scripts/Utils/Utils.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 34e50ca0d0f3e5f41841dd34c7357915 3 | timeCreated: 1453002879 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /QuickPool/ProjectSettings/AudioManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/densylkin/QuickPool/376e7a515702c4e83757be486b4b58f946c780b9/QuickPool/ProjectSettings/AudioManager.asset -------------------------------------------------------------------------------- /QuickPool/ProjectSettings/ClusterInputManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/densylkin/QuickPool/376e7a515702c4e83757be486b4b58f946c780b9/QuickPool/ProjectSettings/ClusterInputManager.asset -------------------------------------------------------------------------------- /QuickPool/ProjectSettings/DynamicsManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/densylkin/QuickPool/376e7a515702c4e83757be486b4b58f946c780b9/QuickPool/ProjectSettings/DynamicsManager.asset -------------------------------------------------------------------------------- /QuickPool/ProjectSettings/EditorBuildSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/densylkin/QuickPool/376e7a515702c4e83757be486b4b58f946c780b9/QuickPool/ProjectSettings/EditorBuildSettings.asset -------------------------------------------------------------------------------- /QuickPool/ProjectSettings/EditorSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/densylkin/QuickPool/376e7a515702c4e83757be486b4b58f946c780b9/QuickPool/ProjectSettings/EditorSettings.asset -------------------------------------------------------------------------------- /QuickPool/ProjectSettings/GraphicsSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/densylkin/QuickPool/376e7a515702c4e83757be486b4b58f946c780b9/QuickPool/ProjectSettings/GraphicsSettings.asset -------------------------------------------------------------------------------- /QuickPool/ProjectSettings/InputManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/densylkin/QuickPool/376e7a515702c4e83757be486b4b58f946c780b9/QuickPool/ProjectSettings/InputManager.asset -------------------------------------------------------------------------------- /QuickPool/ProjectSettings/NavMeshAreas.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/densylkin/QuickPool/376e7a515702c4e83757be486b4b58f946c780b9/QuickPool/ProjectSettings/NavMeshAreas.asset -------------------------------------------------------------------------------- /QuickPool/ProjectSettings/NetworkManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/densylkin/QuickPool/376e7a515702c4e83757be486b4b58f946c780b9/QuickPool/ProjectSettings/NetworkManager.asset -------------------------------------------------------------------------------- /QuickPool/ProjectSettings/Physics2DSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/densylkin/QuickPool/376e7a515702c4e83757be486b4b58f946c780b9/QuickPool/ProjectSettings/Physics2DSettings.asset -------------------------------------------------------------------------------- /QuickPool/ProjectSettings/ProjectSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/densylkin/QuickPool/376e7a515702c4e83757be486b4b58f946c780b9/QuickPool/ProjectSettings/ProjectSettings.asset -------------------------------------------------------------------------------- /QuickPool/ProjectSettings/ProjectVersion.txt: -------------------------------------------------------------------------------- 1 | m_EditorVersion: 5.3.1f1 2 | m_StandardAssetsVersion: 0 3 | -------------------------------------------------------------------------------- /QuickPool/ProjectSettings/QualitySettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/densylkin/QuickPool/376e7a515702c4e83757be486b4b58f946c780b9/QuickPool/ProjectSettings/QualitySettings.asset -------------------------------------------------------------------------------- /QuickPool/ProjectSettings/TagManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/densylkin/QuickPool/376e7a515702c4e83757be486b4b58f946c780b9/QuickPool/ProjectSettings/TagManager.asset -------------------------------------------------------------------------------- /QuickPool/ProjectSettings/TimeManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/densylkin/QuickPool/376e7a515702c4e83757be486b4b58f946c780b9/QuickPool/ProjectSettings/TimeManager.asset -------------------------------------------------------------------------------- /QuickPool/ProjectSettings/UnityAdsSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/densylkin/QuickPool/376e7a515702c4e83757be486b4b58f946c780b9/QuickPool/ProjectSettings/UnityAdsSettings.asset -------------------------------------------------------------------------------- /QuickPool/ProjectSettings/UnityConnectSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/densylkin/QuickPool/376e7a515702c4e83757be486b4b58f946c780b9/QuickPool/ProjectSettings/UnityConnectSettings.asset -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Unity-QuickPool 2 | [Asset store](http://u3d.as/jEu) 3 | Usage 4 | ----- 5 | You can use quickpool in two ways: 6 | - Create PoolManager and configure your pools with editor 7 | 8 | ![Imgur](http://i.imgur.com/mMy5Pr5.png) 9 | 10 | - Or create a Pool field (in this case you will need to register your pool somewhere in Start with PoolManager.RegisterPool()) 11 | 12 | ![Imgur](http://i.imgur.com/N6jDhrr.png) 13 | 14 | Include QuickPool namespace 15 | 16 | ```C# 17 | using QuickPool; 18 | ``` 19 | 20 | You can use extention methods 21 | 22 | ```C# 23 | var instance = somePrefab.Spawn(Vector3.zero, Quaternion.identity); 24 | var component = somePrefab.Spawn(Vector3.zero, Quaternion.identity); 25 | obj.Despawn(); 26 | ``` 27 | 28 | You also can get concrete pool from PoolManager or use that one you have created 29 | 30 | ```C# 31 | public Pool pool = new Pool() 32 | { 33 | size = 100, 34 | allowGrowth = true 35 | }; 36 | 37 | // OR 38 | 39 | var pool = PoolManager.Instance["pool name"]; 40 | ``` 41 | ```C# 42 | private void Start() 43 | { 44 | PoolsManager.RegisterPool(pool); //register pool if you want to use extention method Despawn 45 | pool.Initialize(); 46 | 47 | var instance = pool.Spawn(Vector3.zero, Quaternion.identity); 48 | var conponent = pool.Spawn(Vector3.zero, Quaternion.identity); 49 | pool.Despawn(instance); 50 | } 51 | ``` 52 | Despawn all spawned objects 53 | 54 | ```C# 55 | PoolsManager.DespawnAll(); 56 | // OR 57 | pool.DespawnAll(); 58 | ``` 59 | --------------------------------------------------------------------------------