daily pastebin goal
56%
SHARE
TWEET

ObjectPooling

Aslan85 Oct 16th, 2016 62 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 sealed class ObjectPool : MonoBehaviour
  6. {
  7.     public enum StartupPoolMode { Awake, Start, CallManually };
  8.  
  9.     [System.Serializable]
  10.     public class StartupPool
  11.     {
  12.         public int size;
  13.         public GameObject prefab;
  14.     }
  15.  
  16.     static ObjectPool _instance;
  17.     static List<GameObject> tempList = new List<GameObject>();
  18.    
  19.     Dictionary<GameObject, List<GameObject>> pooledObjects = new Dictionary<GameObject, List<GameObject>>();
  20.     Dictionary<GameObject, GameObject> spawnedObjects = new Dictionary<GameObject, GameObject>();
  21.    
  22.     public StartupPoolMode startupPoolMode;
  23.     public StartupPool[] startupPools;
  24.  
  25.     bool startupPoolsCreated;
  26.  
  27.     void Awake()
  28.     {
  29.         _instance = this;
  30.         if (startupPoolMode == StartupPoolMode.Awake)
  31.             CreateStartupPools();
  32.     }
  33.  
  34.     void Start()
  35.     {
  36.         if (startupPoolMode == StartupPoolMode.Start)
  37.             CreateStartupPools();
  38.     }
  39.  
  40.     public static void CreateStartupPools()
  41.     {
  42.         if (!instance.startupPoolsCreated)
  43.         {
  44.             instance.startupPoolsCreated = true;
  45.             var pools = instance.startupPools;
  46.             if (pools != null && pools.Length > 0)
  47.                 for (int i = 0; i < pools.Length; ++i)
  48.                     CreatePool(pools[i].prefab, pools[i].size);
  49.         }
  50.     }
  51.  
  52.     public static void CreatePool<T>(T prefab, int initialPoolSize) where T : Component
  53.     {
  54.         CreatePool(prefab.gameObject, initialPoolSize);
  55.     }
  56.     public static void CreatePool(GameObject prefab, int initialPoolSize)
  57.     {
  58.         if (prefab != null && !instance.pooledObjects.ContainsKey(prefab))
  59.         {
  60.             var list = new List<GameObject>();
  61.             instance.pooledObjects.Add(prefab, list);
  62.  
  63.             if (initialPoolSize > 0)
  64.             {
  65.                 bool active = prefab.activeSelf;
  66.                 prefab.SetActive(false);
  67.                 Transform parent = instance.transform;
  68.                 while (list.Count < initialPoolSize)
  69.                 {
  70.                     var obj = (GameObject)Object.Instantiate(prefab);
  71.                     obj.transform.parent = parent;
  72.                     list.Add(obj);
  73.                 }
  74.                 prefab.SetActive(active);
  75.             }
  76.         }
  77.     }
  78.    
  79.     public static T Spawn<T>(T prefab, Transform parent, Vector3 position, Quaternion rotation) where T : Component
  80.     {
  81.         return Spawn(prefab.gameObject, parent, position, rotation).GetComponent<T>();
  82.     }
  83.     public static T Spawn<T>(T prefab, Vector3 position, Quaternion rotation) where T : Component
  84.     {
  85.         return Spawn(prefab.gameObject, null, position, rotation).GetComponent<T>();
  86.     }
  87.     public static T Spawn<T>(T prefab, Transform parent, Vector3 position) where T : Component
  88.     {
  89.         return Spawn(prefab.gameObject, parent, position, Quaternion.identity).GetComponent<T>();
  90.     }
  91.     public static T Spawn<T>(T prefab, Vector3 position) where T : Component
  92.     {
  93.         return Spawn(prefab.gameObject, null, position, Quaternion.identity).GetComponent<T>();
  94.     }
  95.     public static T Spawn<T>(T prefab, Transform parent) where T : Component
  96.     {
  97.         return Spawn(prefab.gameObject, parent, Vector3.zero, Quaternion.identity).GetComponent<T>();
  98.     }
  99.     public static T Spawn<T>(T prefab) where T : Component
  100.     {
  101.         return Spawn(prefab.gameObject, null, Vector3.zero, Quaternion.identity).GetComponent<T>();
  102.     }
  103.     public static GameObject Spawn(GameObject prefab, Transform parent, Vector3 position, Quaternion rotation)
  104.     {
  105.         List<GameObject> list;
  106.         Transform trans;
  107.         GameObject obj;
  108.         if (instance.pooledObjects.TryGetValue(prefab, out list))
  109.         {
  110.             obj = null;
  111.             if (list.Count > 0)
  112.             {
  113.                 while (obj == null && list.Count > 0)
  114.                 {
  115.                     obj = list[0];
  116.                     list.RemoveAt(0);
  117.                 }
  118.                 if (obj != null)
  119.                 {
  120.                     trans = obj.transform;
  121.                     trans.parent = parent;
  122.                     trans.localPosition = position;
  123.                     trans.localRotation = rotation;
  124.                     obj.SetActive(true);
  125.                     instance.spawnedObjects.Add(obj, prefab);
  126.                     return obj;
  127.                 }
  128.             }
  129.             obj = (GameObject)Object.Instantiate(prefab);
  130.             trans = obj.transform;
  131.             trans.parent = parent;
  132.             trans.localPosition = position;
  133.             trans.localRotation = rotation;
  134.             instance.spawnedObjects.Add(obj, prefab);
  135.             return obj;
  136.         }
  137.         else
  138.         {
  139.             obj = (GameObject)Object.Instantiate(prefab);
  140.             trans = obj.GetComponent<Transform>();
  141.             trans.parent = parent;
  142.             trans.localPosition = position;
  143.             trans.localRotation = rotation;
  144.             return obj;
  145.         }
  146.     }
  147.     public static GameObject Spawn(GameObject prefab, Transform parent, Vector3 position)
  148.     {
  149.         return Spawn(prefab, parent, position, Quaternion.identity);
  150.     }
  151.     public static GameObject Spawn(GameObject prefab, Vector3 position, Quaternion rotation)
  152.     {
  153.         return Spawn(prefab, null, position, rotation);
  154.     }
  155.     public static GameObject Spawn(GameObject prefab, Transform parent)
  156.     {
  157.         return Spawn(prefab, parent, Vector3.zero, Quaternion.identity);
  158.     }
  159.     public static GameObject Spawn(GameObject prefab, Vector3 position)
  160.     {
  161.         return Spawn(prefab, null, position, Quaternion.identity);
  162.     }
  163.     public static GameObject Spawn(GameObject prefab)
  164.     {
  165.         return Spawn(prefab, null, Vector3.zero, Quaternion.identity);
  166.     }
  167.  
  168.     public static void Recycle<T>(T obj) where T : Component
  169.     {
  170.         Recycle(obj.gameObject);
  171.     }
  172.     public static void Recycle(GameObject obj)
  173.     {
  174.         GameObject prefab;
  175.         if (instance.spawnedObjects.TryGetValue(obj, out prefab))
  176.             Recycle(obj, prefab);
  177.         else
  178.             Object.Destroy(obj);
  179.     }
  180.     static void Recycle(GameObject obj, GameObject prefab)
  181.     {
  182.         instance.pooledObjects[prefab].Add(obj);
  183.         instance.spawnedObjects.Remove(obj);
  184.         obj.transform.parent = instance.transform;
  185.         obj.SetActive(false);
  186.     }
  187.  
  188.     public static void RecycleAll<T>(T prefab) where T : Component
  189.     {
  190.         RecycleAll(prefab.gameObject);
  191.     }
  192.     public static void RecycleAll(GameObject prefab)
  193.     {
  194.         foreach (var item in instance.spawnedObjects)
  195.             if (item.Value == prefab)
  196.                 tempList.Add(item.Key);
  197.         for (int i = 0; i < tempList.Count; ++i)
  198.             Recycle(tempList[i]);
  199.         tempList.Clear();
  200.     }
  201.     public static void RecycleAll()
  202.     {
  203.         tempList.AddRange(instance.spawnedObjects.Keys);
  204.         for (int i = 0; i < tempList.Count; ++i)
  205.             Recycle(tempList[i]);
  206.         tempList.Clear();
  207.     }
  208.    
  209.     public static bool IsSpawned(GameObject obj)
  210.     {
  211.         return instance.spawnedObjects.ContainsKey(obj);
  212.     }
  213.  
  214.     public static int CountPooled<T>(T prefab) where T : Component
  215.     {
  216.         return CountPooled(prefab.gameObject);
  217.     }
  218.     public static int CountPooled(GameObject prefab)
  219.     {
  220.         List<GameObject> list;
  221.         if (instance.pooledObjects.TryGetValue(prefab, out list))
  222.             return list.Count;
  223.         return 0;
  224.     }
  225.  
  226.     public static int CountSpawned<T>(T prefab) where T : Component
  227.     {
  228.         return CountSpawned(prefab.gameObject);
  229.     }
  230.     public static int CountSpawned(GameObject prefab)
  231.     {
  232.         int count = 0 ;
  233.         foreach (var instancePrefab in instance.spawnedObjects.Values)
  234.             if (prefab == instancePrefab)
  235.                 ++count;
  236.         return count;
  237.     }
  238.  
  239.     public static int CountAllPooled()
  240.     {
  241.         int count = 0;
  242.         foreach (var list in instance.pooledObjects.Values)
  243.             count += list.Count;
  244.         return count;
  245.     }
  246.  
  247.     public static List<GameObject> GetPooled(GameObject prefab, List<GameObject> list, bool appendList)
  248.     {
  249.         if (list == null)
  250.             list = new List<GameObject>();
  251.         if (!appendList)
  252.             list.Clear();
  253.         List<GameObject> pooled;
  254.         if (instance.pooledObjects.TryGetValue(prefab, out pooled))
  255.             list.AddRange(pooled);
  256.         return list;
  257.     }
  258.     public static List<T> GetPooled<T>(T prefab, List<T> list, bool appendList) where T : Component
  259.     {
  260.         if (list == null)
  261.             list = new List<T>();
  262.         if (!appendList)
  263.             list.Clear();
  264.         List<GameObject> pooled;
  265.         if (instance.pooledObjects.TryGetValue(prefab.gameObject, out pooled))
  266.             for (int i = 0; i < pooled.Count; ++i)
  267.                 list.Add(pooled[i].GetComponent<T>());
  268.         return list;
  269.     }
  270.  
  271.     public static List<GameObject> GetSpawned(GameObject prefab, List<GameObject> list, bool appendList)
  272.     {
  273.         if (list == null)
  274.             list = new List<GameObject>();
  275.         if (!appendList)
  276.             list.Clear();
  277.         foreach (var item in instance.spawnedObjects)
  278.             if (item.Value == prefab)
  279.                 list.Add(item.Key);
  280.         return list;
  281.     }
  282.     public static List<T> GetSpawned<T>(T prefab, List<T> list, bool appendList) where T : Component
  283.     {
  284.         if (list == null)
  285.             list = new List<T>();
  286.         if (!appendList)
  287.             list.Clear();
  288.         var prefabObj = prefab.gameObject;
  289.         foreach (var item in instance.spawnedObjects)
  290.             if (item.Value == prefabObj)
  291.                 list.Add(item.Key.GetComponent<T>());
  292.         return list;
  293.     }
  294.  
  295.     public static void DestroyPooled(GameObject prefab)
  296.     {
  297.         List<GameObject> pooled;
  298.         if (instance.pooledObjects.TryGetValue(prefab, out pooled))
  299.         {
  300.             for (int i = 0; i < pooled.Count; ++i)
  301.                 GameObject.Destroy(pooled[i]);
  302.             pooled.Clear();
  303.         }
  304.     }
  305.     public static void DestroyPooled<T>(T prefab) where T : Component
  306.     {
  307.         DestroyPooled(prefab.gameObject);
  308.     }
  309.  
  310.     public static void DestroyAll(GameObject prefab)
  311.     {
  312.         RecycleAll(prefab);
  313.         DestroyPooled(prefab);
  314.     }
  315.     public static void DestroyAll<T>(T prefab) where T : Component
  316.     {
  317.         DestroyAll(prefab.gameObject);
  318.     }
  319.  
  320.     public static ObjectPool instance
  321.     {
  322.         get
  323.         {
  324.             if (_instance != null)
  325.                 return _instance;
  326.  
  327.             _instance = Object.FindObjectOfType<ObjectPool>();
  328.             if (_instance != null)
  329.                 return _instance;
  330.  
  331.             var obj = new GameObject("ObjectPool");
  332.             obj.transform.localPosition = Vector3.zero;
  333.             obj.transform.localRotation = Quaternion.identity;
  334.             obj.transform.localScale = Vector3.one;
  335.             _instance = obj.AddComponent<ObjectPool>();
  336.             return _instance;
  337.         }
  338.     }
  339. }
  340.  
  341. public static class ObjectPoolExtensions
  342. {
  343.     public static void CreatePool<T>(this T prefab) where T : Component
  344.     {
  345.         ObjectPool.CreatePool(prefab, 0);
  346.     }
  347.     public static void CreatePool<T>(this T prefab, int initialPoolSize) where T : Component
  348.     {
  349.         ObjectPool.CreatePool(prefab, initialPoolSize);
  350.     }
  351.     public static void CreatePool(this GameObject prefab)
  352.     {
  353.         ObjectPool.CreatePool(prefab, 0);
  354.     }
  355.     public static void CreatePool(this GameObject prefab, int initialPoolSize)
  356.     {
  357.         ObjectPool.CreatePool(prefab, initialPoolSize);
  358.     }
  359.    
  360.     public static T Spawn<T>(this T prefab, Transform parent, Vector3 position, Quaternion rotation) where T : Component
  361.     {
  362.         return ObjectPool.Spawn(prefab, parent, position, rotation);
  363.     }
  364.     public static T Spawn<T>(this T prefab, Vector3 position, Quaternion rotation) where T : Component
  365.     {
  366.         return ObjectPool.Spawn(prefab, null, position, rotation);
  367.     }
  368.     public static T Spawn<T>(this T prefab, Transform parent, Vector3 position) where T : Component
  369.     {
  370.         return ObjectPool.Spawn(prefab, parent, position, Quaternion.identity);
  371.     }
  372.     public static T Spawn<T>(this T prefab, Vector3 position) where T : Component
  373.     {
  374.         return ObjectPool.Spawn(prefab, null, position, Quaternion.identity);
  375.     }
  376.     public static T Spawn<T>(this T prefab, Transform parent) where T : Component
  377.     {
  378.         return ObjectPool.Spawn(prefab, parent, Vector3.zero, Quaternion.identity);
  379.     }
  380.     public static T Spawn<T>(this T prefab) where T : Component
  381.     {
  382.         return ObjectPool.Spawn(prefab, null, Vector3.zero, Quaternion.identity);
  383.     }
  384.     public static GameObject Spawn(this GameObject prefab, Transform parent, Vector3 position, Quaternion rotation)
  385.     {
  386.         return ObjectPool.Spawn(prefab, parent, position, rotation);
  387.     }
  388.     public static GameObject Spawn(this GameObject prefab, Vector3 position, Quaternion rotation)
  389.     {
  390.         return ObjectPool.Spawn(prefab, null, position, rotation);
  391.     }
  392.     public static GameObject Spawn(this GameObject prefab, Transform parent, Vector3 position)
  393.     {
  394.         return ObjectPool.Spawn(prefab, parent, position, Quaternion.identity);
  395.     }
  396.     public static GameObject Spawn(this GameObject prefab, Vector3 position)
  397.     {
  398.         return ObjectPool.Spawn(prefab, null, position, Quaternion.identity);
  399.     }
  400.     public static GameObject Spawn(this GameObject prefab, Transform parent)
  401.     {
  402.         return ObjectPool.Spawn(prefab, parent, Vector3.zero, Quaternion.identity);
  403.     }
  404.     public static GameObject Spawn(this GameObject prefab)
  405.     {
  406.         return ObjectPool.Spawn(prefab, null, Vector3.zero, Quaternion.identity);
  407.     }
  408.    
  409.     public static void Recycle<T>(this T obj) where T : Component
  410.     {
  411.         ObjectPool.Recycle(obj);
  412.     }
  413.     public static void Recycle(this GameObject obj)
  414.     {
  415.         ObjectPool.Recycle(obj);
  416.     }
  417.  
  418.     public static void RecycleAll<T>(this T prefab) where T : Component
  419.     {
  420.         ObjectPool.RecycleAll(prefab);
  421.     }
  422.     public static void RecycleAll(this GameObject prefab)
  423.     {
  424.         ObjectPool.RecycleAll(prefab);
  425.     }
  426.  
  427.     public static int CountPooled<T>(this T prefab) where T : Component
  428.     {
  429.         return ObjectPool.CountPooled(prefab);
  430.     }
  431.     public static int CountPooled(this GameObject prefab)
  432.     {
  433.         return ObjectPool.CountPooled(prefab);
  434.     }
  435.  
  436.     public static int CountSpawned<T>(this T prefab) where T : Component
  437.     {
  438.         return ObjectPool.CountSpawned(prefab);
  439.     }
  440.     public static int CountSpawned(this GameObject prefab)
  441.     {
  442.         return ObjectPool.CountSpawned(prefab);
  443.     }
  444.  
  445.     public static List<GameObject> GetSpawned(this GameObject prefab, List<GameObject> list, bool appendList)
  446.     {
  447.         return ObjectPool.GetSpawned(prefab, list, appendList);
  448.     }
  449.     public static List<GameObject> GetSpawned(this GameObject prefab, List<GameObject> list)
  450.     {
  451.         return ObjectPool.GetSpawned(prefab, list, false);
  452.     }
  453.     public static List<GameObject> GetSpawned(this GameObject prefab)
  454.     {
  455.         return ObjectPool.GetSpawned(prefab, null, false);
  456.     }
  457.     public static List<T> GetSpawned<T>(this T prefab, List<T> list, bool appendList) where T : Component
  458.     {
  459.         return ObjectPool.GetSpawned(prefab, list, appendList);
  460.     }
  461.     public static List<T> GetSpawned<T>(this T prefab, List<T> list) where T : Component
  462.     {
  463.         return ObjectPool.GetSpawned(prefab, list, false);
  464.     }
  465.     public static List<T> GetSpawned<T>(this T prefab) where T : Component
  466.     {
  467.         return ObjectPool.GetSpawned(prefab, null, false);
  468.     }
  469.  
  470.     public static List<GameObject> GetPooled(this GameObject prefab, List<GameObject> list, bool appendList)
  471.     {
  472.         return ObjectPool.GetPooled(prefab, list, appendList);
  473.     }
  474.     public static List<GameObject> GetPooled(this GameObject prefab, List<GameObject> list)
  475.     {
  476.         return ObjectPool.GetPooled(prefab, list, false);
  477.     }
  478.     public static List<GameObject> GetPooled(this GameObject prefab)
  479.     {
  480.         return ObjectPool.GetPooled(prefab, null, false);
  481.     }
  482.     public static List<T> GetPooled<T>(this T prefab, List<T> list, bool appendList) where T : Component
  483.     {
  484.         return ObjectPool.GetPooled(prefab, list, appendList);
  485.     }
  486.     public static List<T> GetPooled<T>(this T prefab, List<T> list) where T : Component
  487.     {
  488.         return ObjectPool.GetPooled(prefab, list, false);
  489.     }
  490.     public static List<T> GetPooled<T>(this T prefab) where T : Component
  491.     {
  492.         return ObjectPool.GetPooled(prefab, null, false);
  493.     }
  494.  
  495.     public static void DestroyPooled(this GameObject prefab)
  496.     {
  497.         ObjectPool.DestroyPooled(prefab);
  498.     }
  499.     public static void DestroyPooled<T>(this T prefab) where T : Component
  500.     {
  501.         ObjectPool.DestroyPooled(prefab.gameObject);
  502.     }
  503.  
  504.     public static void DestroyAll(this GameObject prefab)
  505.     {
  506.         ObjectPool.DestroyAll(prefab);
  507.     }
  508.     public static void DestroyAll<T>(this T prefab) where T : Component
  509.     {
  510.         ObjectPool.DestroyAll(prefab.gameObject);
  511.     }
  512. }
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