daily pastebin goal
0%
SHARE
TWEET

Object Pool

Looksky Mar 9th, 2016 (edited) 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // A class used to pool any Unity Component.
  2. // This class will return an object stored in the pool, or make an object if there aren't any objects in the pool.
  3. // Note: this class does not initialize or even control the objects, it only handles creating and storing the object.
  4. // Therefore, if you want to initialize the object, you have to use the MadeObjectEvent,
  5. // which is triggered when an object is first created.
  6.  
  7. using System.Collections.Generic;
  8. using UnityEngine;
  9.  
  10. namespace R_Utility
  11. {
  12.     public class ObjectPool<O> where O : Component
  13.     {
  14.         private List<O> objectPool = new List<O>();
  15.         private List<O> activeObjects = new List<O>();
  16.  
  17.         private O prefab;
  18.         private int totalAmountOfObjects = 0, maxAmountOfObjects = int.MaxValue;
  19.         public int TotalAmountOfObjects { get { return totalAmountOfObjects; } }
  20.  
  21.         public delegate void MadeObjectEventHandler(O theObject, int numberOfObjects);
  22.         public event MadeObjectEventHandler MadeObjectEvent;
  23.  
  24.         public ObjectPool(O prefab)
  25.         {
  26.             this.prefab = prefab;
  27.         }
  28.  
  29.         public O GetNewObject()
  30.         { // get an object that is either inactive, or not made yet
  31.             O theObject = null;
  32.  
  33.             for (int i = objectPool.Count - 1; i >= 0; i--)
  34.             {
  35.                 O _object = objectPool[i];
  36.                 if (_object != null)
  37.                 {
  38.                     if (_object.gameObject.activeSelf == false)
  39.                     {
  40.                         theObject = _object;
  41.                         MoveToActive(theObject);
  42.                         theObject.gameObject.SetActive(true);
  43.                         break;
  44.                     }
  45.                     else
  46.                     {
  47.                         MoveToActive(theObject);
  48.                     }
  49.                 }
  50.                 else
  51.                 {
  52.                     objectPool.Remove(_object);
  53.                 }
  54.             }
  55.             if (theObject == null)
  56.             {
  57.                 theObject = MakeObject();
  58.             }
  59.  
  60.             return theObject;
  61.         }
  62.  
  63.         private void MoveToPool(O _theObject)
  64.         {
  65.             activeObjects.Remove(_theObject);
  66.             objectPool.Add(_theObject);
  67.         }
  68.  
  69.         private void MoveToActive(O _theObject)
  70.         {
  71.             objectPool.Remove(_theObject);
  72.             activeObjects.Add(_theObject);
  73.         }
  74.  
  75.         private O MakeObject()
  76.         {
  77.             O theObject = null;
  78.             if (totalAmountOfObjects < maxAmountOfObjects)
  79.             {
  80.                 GameObject go = GameObject.Instantiate(prefab.gameObject);
  81.                 totalAmountOfObjects++;
  82.                 theObject = go.GetComponent<O>();
  83.                 if (go.activeSelf == false) { go.SetActive(true); }
  84.                 activeObjects.Add(theObject);
  85.                 if(MadeObjectEvent != null)
  86.                 { // the object pool only handles the making of the objects, it doesnt actually know what to do with the objects, if you want to initialize it, use this event
  87.                     MadeObjectEvent(theObject, totalAmountOfObjects);
  88.                 }
  89.             }
  90.             return theObject;
  91.         }
  92.  
  93.         public void RemoveObject(O _theObject)
  94.         {
  95.             if (_theObject != null)
  96.             {
  97.                 if (_theObject.gameObject.activeSelf == true){ _theObject.gameObject.SetActive(false); }
  98.                 MoveToPool(_theObject);
  99.             }
  100.         }
  101.  
  102.         public void RemoveObject(GameObject _theObject)
  103.         {
  104.             O theObject = _theObject.GetComponent<O>();
  105.             RemoveObject(theObject);
  106.         }
  107.  
  108.         public void SetAllInactive()
  109.         {
  110.             for (int i = activeObjects.Count - 1; i >= 0; i--)
  111.             {
  112.                 O theObject = activeObjects[i];
  113.                 MoveToPool(theObject);
  114.                 if(theObject.gameObject.activeSelf == true) { theObject.gameObject.SetActive(false); }
  115.             }
  116.         }
  117.  
  118.         public void LimitObjects(int number)
  119.         { // limits the amount of active objects
  120.             maxAmountOfObjects = number;
  121.             for (int i = activeObjects.Count - 1; i >= maxAmountOfObjects; i--)
  122.             {
  123.                 O theObject = activeObjects[i];
  124.                 MoveToPool(theObject);
  125.                 if (theObject.gameObject.activeSelf == true) { theObject.gameObject.SetActive(false); }
  126.             }
  127.         }
  128.  
  129.         public List<O> GetObjects(int number, bool allNew)
  130.         { // gets a list of objects, can be from the active objects list, if allnew is set to false
  131.             List<O> theObjects = new List<O>();
  132.  
  133.             if (allNew == false)
  134.             {
  135.                 for (int i = 0; i < activeObjects.Count && number > 0; i++, number--)
  136.                 {
  137.                     theObjects.Add(activeObjects[i]);
  138.                 }
  139.             }
  140.             for (; number > 0; number--)
  141.             {
  142.                 theObjects.Add(GetNewObject());
  143.             }
  144.  
  145.             return theObjects;
  146.         }
  147.     }
  148. }
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