SHARE
TWEET

Untitled

JJCUBER Feb 17th, 2019 (edited) 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public static class ObjectPool
  6. {
  7.     const int DEFAULT_POOL_SIZE = 10;
  8.     private static Dictionary<GameObject, Pool> pools; // = new Dictionary<GameObject, Pool>();
  9.  
  10.     static void Init(GameObject prefab = null, int qty = DEFAULT_POOL_SIZE)
  11.     {
  12.         if (pools == null)
  13.             pools = new Dictionary<GameObject, Pool>();
  14.         else if (pools.ContainsKey(prefab) == false)
  15.             pools[prefab] = new Pool(prefab, qty);
  16.     }
  17.  
  18.     static public void Preload(GameObject prefab, int qty = 1)
  19.     {
  20.         Init(prefab, qty);
  21.  
  22.         GameObject[] objs = new GameObject[qty];
  23.         for (int i = 0; i < qty; i++)
  24.         {
  25.             objs[i] = Spawn(prefab, Vector3.zero, Quaternion.identity);
  26.             Despawn(objs[i]);
  27.         }
  28.     }
  29.  
  30.     /*public static void MakePool(GameObject prefab, int startAmount)
  31.     {
  32.         pools.Add(prefab, new Pool(prefab, startAmount));
  33.     }*/
  34.  
  35.     public static GameObject Spawn(GameObject prefab)
  36.     {
  37.         return Spawn(prefab, Vector3.zero, Quaternion.identity);
  38.     }
  39.  
  40.     public static GameObject Spawn(GameObject prefab, Vector3 pos)
  41.     {
  42.         return Spawn(prefab, pos, Quaternion.identity);
  43.     }
  44.  
  45.     public static GameObject Spawn(GameObject prefab, Quaternion rot)
  46.     {
  47.         return Spawn(prefab, Vector3.zero, rot);
  48.     }
  49.  
  50.     public static GameObject Spawn(GameObject prefab, Transform parent)
  51.     {
  52.         return Spawn(prefab, Vector3.zero, Quaternion.identity, parent);
  53.     }
  54.  
  55.     public static GameObject Spawn(GameObject prefab, Vector3 pos, Transform parent)
  56.     {
  57.         return Spawn(prefab, pos, Quaternion.identity, parent);
  58.     }
  59.  
  60.     public static GameObject Spawn(GameObject prefab, Quaternion rot, Transform parent)
  61.     {
  62.         return Spawn(prefab, Vector3.zero, rot, parent);
  63.     }
  64.  
  65.     public static GameObject Spawn(GameObject prefab, Vector3 pos, Quaternion rot, Transform parent = null)
  66.     {
  67.         Init(prefab);
  68.         // if(avail)
  69.         return pools[prefab].Spawn(pos, rot, parent);
  70.     }
  71.  
  72.     public static void Despawn(GameObject obj)
  73.     {
  74.         PoolMember pm = obj.GetComponent<PoolMember>();
  75.         if (pm == null)
  76.             Object.Destroy(obj);
  77.         else
  78.             pm.myPool.Despawn(obj);
  79.     }
  80.  
  81.     public static void Despawn(GameObject obj, float delay)
  82.     {
  83.         Wait.instance.Call(obj, delay);
  84.  
  85.         // Wait wait = new Wait();
  86.         // wait.Amount(obj, delay);
  87.     }
  88.  
  89.     private class Pool // : MonoBehaviour
  90.     {
  91.         Stack<GameObject> available;
  92.         GameObject prefab;
  93.         // int startAmount;
  94.  
  95.         public Pool(GameObject prefab, int startAmount)
  96.         {
  97.             this.prefab = prefab;
  98.             // this.startAmount = startAmount;
  99.             available = new Stack<GameObject>(startAmount);
  100.         }
  101.  
  102.         public GameObject Spawn(Vector3 pos, Quaternion rot, Transform parent)
  103.         {
  104.             GameObject obj;
  105.             if (available.Count == 0)
  106.                 obj = Object.Instantiate(prefab, pos, rot);
  107.             else
  108.             {
  109.                 obj = available.Pop();
  110.  
  111.                 if (obj == null)
  112.                     return Spawn(pos, rot, parent);
  113.             }
  114.  
  115.             obj.transform.parent = parent;
  116.             obj.transform.position = pos;
  117.             obj.transform.rotation = rot;
  118.             obj.SetActive(true);
  119.  
  120.             return obj;
  121.         }
  122.  
  123.         public void Despawn(GameObject obj)
  124.         {
  125.             obj.SetActive(false);
  126.  
  127.             available.Push(obj);
  128.         }
  129.     }
  130.  
  131.     private class PoolMember // : MonoBehaviour
  132.     {
  133.         public Pool myPool;
  134.     }
  135.  
  136.     private class Wait : MonoBehaviour
  137.     {
  138.         public static Wait instance;
  139.  
  140.         private void Awake()
  141.         {
  142.             instance = this;
  143.         }
  144.  
  145.         public void Call(GameObject obj, float delay)
  146.         {
  147.             StartCoroutine(DelayDespawn(obj, delay));
  148.         }
  149.  
  150.         /*public void Amount(GameObject obj, float delay)
  151.         {
  152.             StartCoroutine(DelayDespawn(obj, delay));
  153.         }
  154.         /*public Wait(GameObject obj, float delay)
  155.         {
  156.             StartCoroutine(DelayDespawn(obj, delay));
  157.         }*/
  158.     }
  159.  
  160.     private static IEnumerator DelayDespawn(GameObject obj, float delay)
  161.     {
  162.         yield return new WaitForSeconds(delay);
  163.         Despawn(obj);
  164.     }
  165. }
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