Simple Pool by nvjob - 10

Games & Projects

#NVJOB Simple Pool

Unity 2019.1.8f1MIT LicenseUpdated 33 days agoCreated on October 20th, 2019
Go to source

#NVJOB Simple Pool

Version 1.2

This is a simple pool for optimizing object loading.
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.

GitHub Logo

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

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

GitHub Logo


Authors

Designed by #NVJOB Nicholas Veselov - https://nvjob.github.io

License

MIT License - https://nvjob.github.io/mit-license

Help for this project - https://nvjob.github.io/donate

Report a Problem

Report a Problem / Issue Tracker / FAQ - https://nvjob.github.io/reportaproblem

Show all projects by nvjob