Pro_Unit

Pool_Upgraded

Jan 14th, 2019
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.38 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Linq;
  3.  
  4. using UnityEngine;
  5. namespace GameCore
  6. {
  7.     public class Pool : MonoBehaviour
  8.     {
  9.         private static Pool _instance = null;
  10.         public static Pool instance { get { if (_instance == null) { _instance = GameObject.FindObjectOfType<Pool> (); } return _instance; } }
  11.  
  12.         public PoolSettingsData data;
  13.         List<PoolSetteings> Settings { get { return data.Settings; } }
  14.  
  15.         Dictionary<string, SpawnedObject> _dictSettings = null;
  16.         Dictionary<string, SpawnedObject> dictSettings
  17.         {
  18.             get { return _dictSettings ?? (_dictSettings = Settings.ToDictionary (s => s.Key, s => s.Prefab)); }
  19.         }
  20.  
  21.         [SerializeField] Dictionary<string, List<SpawnedObject>> PoolDict = new Dictionary<string, List<SpawnedObject>> ();
  22.         [SerializeField] Dictionary<string, Transform> Parents = new Dictionary<string, Transform> ();
  23.         private void Awake ()
  24.         {
  25.             InitParents ();
  26.             InitStartCount ();
  27.         }
  28.  
  29.         private void InitStartCount ()
  30.         {
  31.             foreach (var item in Settings)
  32.             {
  33.                 for (int i = 0; i < item.startCount; i++)
  34.                 {
  35.                     Extend (item.Key);
  36.                 }
  37.             }
  38.         }
  39.  
  40.         private void InitParents ()
  41.         {
  42.             foreach (var setting in Settings)
  43.             {
  44.                 InitParent (setting);
  45.             }
  46.         }
  47.  
  48.         private void InitParent (PoolSetteings setting)
  49.         {
  50.             var Key = setting.Key;
  51.             var parent = new GameObject (Key).transform;
  52.             parent.SetParent (transform);
  53.             parent.name = Key;
  54.             Parents[Key] = parent;
  55.         }
  56.         public void Pop (PoolData data)
  57.         {
  58.             Pop (data.Key, data.parent);
  59.         }
  60.  
  61.         public SpawnedObject Pop (string Key, Transform Parent = null)
  62.         {
  63.             SpawnedObject pObj = null;
  64.  
  65.             dictSettings.TryGetValue (Key, out pObj);
  66.  
  67.             if (pObj == null)
  68.             {
  69.                 return null;
  70.             }
  71.  
  72.             if (!PoolDict.ContainsKey (Key))
  73.             {
  74.                 PoolDict[Key] = new List<SpawnedObject> ();
  75.             }
  76.  
  77.             var NewObj = PoolDict[Key].Find (po => !po.isActiveAndEnabled);
  78.             if (!NewObj)
  79.             {
  80.                 NewObj = Extend (Key);
  81.             }
  82.  
  83.             if (!NewObj)
  84.             {
  85.                 return null;
  86.             }
  87.  
  88.             NewObj.gameObject.SetActive (true);
  89.  
  90.             if (Parent == null) Parent = Parents[Key];
  91.  
  92.             NewObj.SetParent (Parent);
  93.             NewObj.ApplyOffset ();
  94.  
  95.             NewObj.OnSpawn.Invoke ();
  96.             return NewObj;
  97.         }
  98.         public T Pop<T> (string Key) where T : Component
  99.         {
  100.             return Pop (Key).Get<T> ();
  101.         }
  102.  
  103.         public void Push (GameObject go, bool force = false)
  104.         {
  105.             var spawnedObj = go.GetComponent<SpawnedObject> ();
  106.             if (!spawnedObj && !force)
  107.             {
  108.                 return;
  109.             }
  110.  
  111.             if (force && !spawnedObj)
  112.             {
  113.                 spawnedObj = go.AddComponent<SpawnedObject> ();
  114.                 PoolSetteings setting = new PoolSetteings () { Key = spawnedObj.name, Prefab = spawnedObj };
  115.                 Settings.Add (setting);
  116.                 InitParent (setting);
  117.                 Extend (setting.Key);
  118.             }
  119.             foreach (var ListItems in PoolDict.Values)
  120.             {
  121.                 foreach (var item in ListItems)
  122.                 {
  123.                     if (item.gameObject.Equals (go))
  124.                     {
  125.                         go.SetActive (false);
  126.                         spawnedObj.OnDeSpawn.Invoke ();
  127.                     }
  128.                 }
  129.             }
  130.         }
  131.  
  132.         private SpawnedObject Extend (string Key)
  133.         {
  134.             var pObj = Settings.Find (ps => ps.Key == Key);
  135.             if (pObj == null)
  136.             {
  137.                 return null;
  138.             }
  139.  
  140.             var newObj = Instantiate (pObj.Prefab);
  141.  
  142.             newObj.name = string.Format ("{0}{1}", Key, newObj.GetInstanceID ());
  143.  
  144.             newObj.transform.SetParent (Parents[Key]);
  145.  
  146.             if (!PoolDict.ContainsKey (Key))
  147.             {
  148.                 PoolDict[Key] = new List<SpawnedObject> ();
  149.             }
  150.  
  151.             PoolDict[Key].Add (newObj);
  152.  
  153.             newObj.gameObject.SetActive (false);
  154.  
  155.             return newObj;
  156.         }
  157.  
  158. #if UNITY_EDITOR
  159.         [ContextMenu ("Add to Pool selected objects")]
  160.         void AddToPoolSelected ()
  161.         {
  162.             var gos = UnityEditor.Selection.gameObjects;
  163.             var spwns = gos.ToList ()
  164.                 .FindAll (go => go.GetComponent<SpawnedObject> ())
  165.                 .Select (go => go.GetComponent<SpawnedObject> ());
  166.             foreach (var spwn in spwns)
  167.             {
  168.                 Settings.Add (new PoolSetteings () { Key = spwn.name, Prefab = spwn, startCount = 10 });
  169.             }
  170.         }
  171. #endif
  172.     }
  173.  
  174.     [System.Serializable]
  175.     public class PoolSetteings
  176.     {
  177.         public string Key;
  178.         public SpawnedObject Prefab;
  179.         public int startCount;
  180.     }
  181.  
  182.     [System.Serializable]
  183.     public class PoolData
  184.     {
  185.         public string Key;
  186.         public Transform parent;
  187.     }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment