├── LICENSE.md ├── PoolManager.cs └── PoolObject.cs /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Sebastian 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 | -------------------------------------------------------------------------------- /PoolManager.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | 5 | public class PoolManager : MonoBehaviour { 6 | 7 | Dictionary> poolDictionary = new Dictionary> (); 8 | 9 | static PoolManager _instance; 10 | 11 | public static PoolManager instance { 12 | get { 13 | if (_instance == null) { 14 | _instance = FindObjectOfType (); 15 | } 16 | return _instance; 17 | } 18 | } 19 | 20 | public void CreatePool(GameObject prefab, int poolSize) { 21 | int poolKey = prefab.GetInstanceID (); 22 | 23 | if (!poolDictionary.ContainsKey (poolKey)) { 24 | poolDictionary.Add (poolKey, new Queue ()); 25 | 26 | GameObject poolHolder = new GameObject (prefab.name + " pool"); 27 | poolHolder.transform.parent = transform; 28 | 29 | for (int i = 0; i < poolSize; i++) { 30 | ObjectInstance newObject = new ObjectInstance(Instantiate (prefab) as GameObject); 31 | poolDictionary [poolKey].Enqueue (newObject); 32 | newObject.SetParent (poolHolder.transform); 33 | } 34 | } 35 | } 36 | 37 | public void ReuseObject(GameObject prefab, Vector3 position, Quaternion rotation) { 38 | int poolKey = prefab.GetInstanceID (); 39 | 40 | if (poolDictionary.ContainsKey (poolKey)) { 41 | ObjectInstance objectToReuse = poolDictionary [poolKey].Dequeue (); 42 | poolDictionary [poolKey].Enqueue (objectToReuse); 43 | 44 | objectToReuse.Reuse (position, rotation); 45 | } 46 | } 47 | 48 | public class ObjectInstance { 49 | 50 | GameObject gameObject; 51 | Transform transform; 52 | 53 | bool hasPoolObjectComponent; 54 | PoolObject poolObjectScript; 55 | 56 | public ObjectInstance(GameObject objectInstance) { 57 | gameObject = objectInstance; 58 | transform = gameObject.transform; 59 | gameObject.SetActive(false); 60 | 61 | if (gameObject.GetComponent()) { 62 | hasPoolObjectComponent = true; 63 | poolObjectScript = gameObject.GetComponent(); 64 | } 65 | } 66 | 67 | public void Reuse(Vector3 position, Quaternion rotation) { 68 | gameObject.SetActive (true); 69 | transform.position = position; 70 | transform.rotation = rotation; 71 | 72 | if (hasPoolObjectComponent) { 73 | poolObjectScript.OnObjectReuse (); 74 | } 75 | } 76 | 77 | public void SetParent(Transform parent) { 78 | transform.parent = parent; 79 | } 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /PoolObject.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public class PoolObject : MonoBehaviour { 5 | 6 | public virtual void OnObjectReuse() { 7 | 8 | } 9 | 10 | protected void Destroy() { 11 | gameObject.SetActive (false); 12 | } 13 | } 14 | --------------------------------------------------------------------------------