daily pastebin goal
15%
SHARE
TWEET

Pool

Pro_Unit Dec 13th, 2018 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections.Generic;
  2. using System.Linq;
  3.  
  4. using UnityEngine;
  5.  
  6.     public class Pool : MonoBehaviour
  7.     {
  8.         private static Pool _instance = null;
  9.         public static Pool instance { get { if (_instance == null) _instance = GameObject.FindObjectOfType<Pool> (); return _instance; } }
  10.  
  11.         public List<PoolSetteings> Settings;
  12.         [SerializeField] Dictionary<string, List<SpawnedObject>> PoolDict = new Dictionary<string, List<SpawnedObject>> ();
  13.         [SerializeField] Dictionary<string, Transform> Parents = new Dictionary<string, Transform> ();
  14.         private void Awake ()
  15.         {
  16.             InitParents ();
  17.             InitStartCount ();
  18.         }
  19.  
  20.         private void InitStartCount ()
  21.         {
  22.             foreach (var item in Settings)
  23.             {
  24.                 for (int i = 0; i < item.startCount; i++)
  25.                 {
  26.                     Extend (item.Key);
  27.                 }
  28.             }
  29.         }
  30.  
  31.         private void InitParents ()
  32.         {
  33.             foreach (var setting in Settings)
  34.             {
  35.                 InitParent (setting);
  36.             }
  37.         }
  38.  
  39.         private void InitParent (PoolSetteings setting)
  40.         {
  41.             var Key = setting.Key;
  42.             var parent = new GameObject (Key).transform;
  43.             parent.SetParent (transform);
  44.             parent.name = Key;
  45.             Parents[Key] = parent;
  46.         }
  47.  
  48.         public SpawnedObject Pop (string Key)
  49.         {
  50.             var pObj = Settings.Find (ps => ps.Key == Key);
  51.             if (pObj == null) return null;
  52.  
  53.             if (!PoolDict.ContainsKey (Key))
  54.                 PoolDict[Key] = new List<SpawnedObject> ();
  55.  
  56.             var NewObj = PoolDict[Key].Find (po => !po.isActiveAndEnabled);
  57.             if (!NewObj) NewObj = Extend (Key);
  58.             if (!NewObj) return null;
  59.  
  60.             NewObj.gameObject.SetActive (true);
  61.  
  62.             NewObj.OnSpawn.Invoke ();
  63.             return NewObj;
  64.         }
  65.         public T Pop<T> (string Key) where T : Component
  66.         {
  67.             return Pop (Key).Get<T> ();
  68.         }
  69.  
  70.         public void Push (GameObject go, bool force = false)
  71.         {
  72.             var spawnedObj = go.GetComponent<SpawnedObject> ();
  73.             if (!spawnedObj && !force) return;
  74.             if (force && !spawnedObj)
  75.             {
  76.                 spawnedObj = go.AddComponent<SpawnedObject> ();
  77.                 PoolSetteings setting = new PoolSetteings () { Key = spawnedObj.name, Prefab = spawnedObj };
  78.                 Settings.Add (setting);
  79.                 InitParent (setting);
  80.                 Extend (setting.Key);
  81.             }
  82.             foreach (var ListItems in PoolDict.Values)
  83.             {
  84.                 foreach (var item in ListItems)
  85.                 {
  86.                     if (item.gameObject.Equals (go))
  87.                     {
  88.                         go.SetActive (false);
  89.                         spawnedObj.OnDeSpawn.Invoke ();
  90.                     }
  91.                 }
  92.             }
  93.         }
  94.  
  95.         private SpawnedObject Extend (string Key)
  96.         {
  97.             var pObj = Settings.Find (ps => ps.Key == Key);
  98.             if (pObj == null) return null;
  99.  
  100.             var newObj = Instantiate (pObj.Prefab);
  101.  
  102.             newObj.name = string.Format ("{0}{1}", Key, newObj.GetInstanceID ());
  103.  
  104.             newObj.transform.SetParent (Parents[Key]);
  105.  
  106.             if (!PoolDict.ContainsKey (Key))
  107.                 PoolDict[Key] = new List<SpawnedObject> ();
  108.             PoolDict[Key].Add (newObj);
  109.  
  110.             newObj.gameObject.SetActive (false);
  111.  
  112.             return newObj;
  113.         }
  114.  
  115. #if UNITY_EDITOR
  116.         [ContextMenu("Add to Pool selected objects")]
  117.         void AddToPoolSelected ()
  118.         {
  119.             var gos = UnityEditor.Selection.gameObjects;
  120.             var spwns = gos.ToList ()
  121.                 .FindAll (go => go.GetComponent<SpawnedObject> ())
  122.                 .Select (go => go.GetComponent<SpawnedObject> ());
  123.             foreach (var spwn in spwns)
  124.             {
  125.                 Settings.Add (new PoolSetteings () { Key = spwn.name, Prefab = spwn, startCount = 10 });
  126.             }
  127.         }
  128. #endif
  129.     }
  130.  
  131.     [System.Serializable]
  132.     public class PoolSetteings
  133.     {
  134.         public string Key;
  135.         public SpawnedObject Prefab;
  136.         public int startCount;
  137.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top