This is a simple pool for optimizing object loading.
Permanent creation and destruction of objects in your scene, very expensive in terms of memory management, use the Simple Pool object pool to save memory and improve performance.
All objects in the pool are loaded during initialization, and then retrieved from the pool and returned back to the pool without sacrificing performance.
The pool allows you to completely abandon Instantiate and Destroy after initialization.
To work on the project, you will need a Unity version of at least 2019.1.8 (64-bit).
GameObject obj = SimplePool.GiveObj(0);
SimplePool.GiveObj() instead of using Instantiate(Object).
SimplePool.GiveObj(numElement)-> numElement - number of the Element in editor (SimplePool).
obj.SetActive(true);
SimplePool.Takeobj(obj);
SimplePool.Takeobj() instead of using - Destroy(Object).
SimplePool.Takeobj(GameObject)-> GameObject - is an object that to be returned to the pool.
SimplePool.numObjectsList
GameObject obj = SimplePool.GiveObj(0);
if (obj != null)
{
}
If the pool is empty it will return null.
using UnityEngine;
public class Example : MonoBehaviour
{
GameObject obj;
void Start()
{
if(SimplePool.numObjectsList == 0) obj = SimplePool.GiveObj(0);
else obj = SimplePool.GiveObj(Random.Range(0, SimplePool.numObjectsList));
if (obj != null)
{
obj.transform.SetPositionAndRotation(transform.position, transform.rotation);
obj.transform.parent = transform;
obj.SetActive(true);
Invoke("DestroyObject", 5);
}
}
void DestroyObject()
{
SimplePool.Takeobj(obj);
}
}
https://www.youtube.com/watch?v=fhuhPnpSoJU
Authors: #NVJOB. Developer Nicholas Veselov. Разработчик Николай Веселов. Санкт-Петербург.
License: MIT License. Clarification of licenses.
🖤 Donate:
paypal.me/nvjob