Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // A class used to pool any Unity Component.
- // This class will return an object stored in the pool, or make an object if there aren't any objects in the pool.
- // Note: this class does not initialize or even control the objects, it only handles creating and storing the object.
- // Therefore, if you want to initialize the object, you have to use the MadeObjectEvent,
- // which is triggered when an object is first created.
- using System.Collections.Generic;
- using UnityEngine;
- namespace R_Utility
- {
- public class ObjectPool<O> where O : Component
- {
- private List<O> objectPool = new List<O>();
- private List<O> activeObjects = new List<O>();
- private O prefab;
- private int totalAmountOfObjects = 0, maxAmountOfObjects = int.MaxValue;
- public int TotalAmountOfObjects { get { return totalAmountOfObjects; } }
- public delegate void MadeObjectEventHandler(O theObject, int numberOfObjects);
- public event MadeObjectEventHandler MadeObjectEvent;
- public ObjectPool(O prefab)
- {
- this.prefab = prefab;
- }
- public O GetNewObject()
- { // get an object that is either inactive, or not made yet
- O theObject = null;
- for (int i = objectPool.Count - 1; i >= 0; i--)
- {
- O _object = objectPool[i];
- if (_object != null)
- {
- if (_object.gameObject.activeSelf == false)
- {
- theObject = _object;
- MoveToActive(theObject);
- theObject.gameObject.SetActive(true);
- break;
- }
- else
- {
- MoveToActive(theObject);
- }
- }
- else
- {
- objectPool.Remove(_object);
- }
- }
- if (theObject == null)
- {
- theObject = MakeObject();
- }
- return theObject;
- }
- private void MoveToPool(O _theObject)
- {
- activeObjects.Remove(_theObject);
- objectPool.Add(_theObject);
- }
- private void MoveToActive(O _theObject)
- {
- objectPool.Remove(_theObject);
- activeObjects.Add(_theObject);
- }
- private O MakeObject()
- {
- O theObject = null;
- if (totalAmountOfObjects < maxAmountOfObjects)
- {
- GameObject go = GameObject.Instantiate(prefab.gameObject);
- totalAmountOfObjects++;
- theObject = go.GetComponent<O>();
- if (go.activeSelf == false) { go.SetActive(true); }
- activeObjects.Add(theObject);
- if(MadeObjectEvent != null)
- { // 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
- MadeObjectEvent(theObject, totalAmountOfObjects);
- }
- }
- return theObject;
- }
- public void RemoveObject(O _theObject)
- {
- if (_theObject != null)
- {
- if (_theObject.gameObject.activeSelf == true){ _theObject.gameObject.SetActive(false); }
- MoveToPool(_theObject);
- }
- }
- public void RemoveObject(GameObject _theObject)
- {
- O theObject = _theObject.GetComponent<O>();
- RemoveObject(theObject);
- }
- public void SetAllInactive()
- {
- for (int i = activeObjects.Count - 1; i >= 0; i--)
- {
- O theObject = activeObjects[i];
- MoveToPool(theObject);
- if(theObject.gameObject.activeSelf == true) { theObject.gameObject.SetActive(false); }
- }
- }
- public void LimitObjects(int number)
- { // limits the amount of active objects
- maxAmountOfObjects = number;
- for (int i = activeObjects.Count - 1; i >= maxAmountOfObjects; i--)
- {
- O theObject = activeObjects[i];
- MoveToPool(theObject);
- if (theObject.gameObject.activeSelf == true) { theObject.gameObject.SetActive(false); }
- }
- }
- public List<O> GetObjects(int number, bool allNew)
- { // gets a list of objects, can be from the active objects list, if allnew is set to false
- List<O> theObjects = new List<O>();
- if (allNew == false)
- {
- for (int i = 0; i < activeObjects.Count && number > 0; i++, number--)
- {
- theObjects.Add(activeObjects[i]);
- }
- }
- for (; number > 0; number--)
- {
- theObjects.Add(GetNewObject());
- }
- return theObjects;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement