Skip to content

nvjob/simple-pool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

87 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

#NVJOB Simple Pool 1.2.1

GitHub Logo

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.


Prerequisites

To work on the project, you will need a Unity version of at least 2019.1.8 (64-bit).

Information

Getting an object from a pool:

GameObject obj = SimplePool.GiveObj(0);

SimplePool.GiveObj() instead of using Instantiate(Object).
SimplePool.GiveObj(numElement)-> numElement - number of the Element in editor (SimplePool).

GitHub Logo

After all the transformations of the object, activate it:

obj.SetActive(true);

Return object to the pool, remove from the scene:

SimplePool.Takeobj(obj);

SimplePool.Takeobj() instead of using - Destroy(Object).
SimplePool.Takeobj(GameObject)-> GameObject - is an object that to be returned to the pool.

The number of elements in the editor (SimplePool):

SimplePool.numObjectsList

Checking that the pool is not empty:

GameObject obj = SimplePool.GiveObj(0);
if (obj != null)
{
}

If the pool is empty it will return null.

Example script:

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);
    }
}

Video manual:

https://www.youtube.com/watch?v=fhuhPnpSoJU


GitHub Logo


Authors: #NVJOB. Developer Nicholas Veselov. Разработчик Николай Веселов. Санкт-Петербург.

License: MIT License. Clarification of licenses.

🖤 Donate:
paypal.me/nvjob