Skip to content

Repository files navigation

Object Pooling for Unity

TODO

  • Rename API to make it more clarifying

Features

  • Faster in terms of performance than Instantiate/Destroy (Test at the end of README)
  • Easy to use
  • Easy to integrate with already written spawn systems
  • Callbacks OnReuse & OnRelease to reset object's state

How to Install

Git Installation (Best way to get latest version)

If you have Git on your computer, you can open Package Manager indside Unity, select "Add package from Git url...", and paste link https://github.com/IntoTheDev/Object-Pooling-for-Unity.git

or

Open the manifest.json file of your Unity project. Add "com.intothedev.objectpooling": "https://github.com/IntoTheDev/Object-Pooling-for-Unity.git"

Manual Installation (Version can be outdated)

Download latest package from the Release section. Import ObjectPooling.unitypackage to your Unity Project

Usage

How to populate pool:

usingToolBox.Pools;usingUnityEngine;publicclassSpawner:MonoBehaviour{[SerializeField]privateGameObject_prefab=null;privatevoidAwake(){_prefab.Populate(count:50);}}

Also, you can just put PoolInstaller component on any object on Scene and select which objects you want to prepopulate

How to clear pool and it's instances

usingToolBox.Pools;usingUnityEngine;publicclassSpawner:MonoBehaviour{[SerializeField]privateGameObject_prefab=null;privatevoidAwake(){_prefab.Populate(count:50);// If destroy active is true then even active instances will be destroyed_prefab.Clear(destroyActive:true)}}

How to get object from pool:

usingToolBox.Pools;usingUnityEngine;publicclassSpawner:MonoBehaviour{[SerializeField]privateGameObject_prefab=null;publicvoidSpawn(){_prefab.Reuse(transform.position,transform.rotation);// Get object from pool with component_prefab.Reuse<Rigidbody>(transform.position,transform.rotation).isKinematic=true;}}

How to release object:

usingToolBox.Pools;usingUnityEngine;publicclassSpawner:MonoBehaviour{[SerializeField]privateGameObject_prefab=null;publicvoidSpawn(){varinstance=_prefab.Reuse(transform.position,transform.rotation);instance.Release();}}

How to use callbacks:

usingToolBox.Pools;usingUnityEngine;publicclassHealth:MonoBehaviour,IPoolable{[SerializeField]privatefloat_maxHealth=100f;privatefloat_health=0f;// Awake will be called on first _prefab.Reuse()privatevoidAwake(){OnReuse();}// IPoolable method/// <summary>/// This method will be called on 2nd Reuse call./// Use Unity's Awake method for first initialization and this method for others/// </summary>publicvoidOnReuse(){_health=_maxHealth;}// IPoolable methodpublicvoidOnRelease(){}}

Peformance test:

Creating and destroying 1000 objects.

Instantiate/Destroy:

usingSirenix.OdinInspector;usingSystem.Diagnostics;usingUnityEngine;publicclassTester:MonoBehaviour{[SerializeField]privateGameObject_object=null;[Button]privatevoidTest(){Stopwatchstopwatch=newStopwatch();stopwatch.Start();for(inti=0;i<1000;i++){varinstance=Instantiate(_object);Destroy(instance);}stopwatch.Stop();print($"Milliseconds: {stopwatch.ElapsedMilliseconds}");}}
Result: [16:26:15] Milliseconds: 6

Get/Release:

usingSirenix.OdinInspector;usingSystem.Diagnostics;usingToolBox.Pools;usingUnityEngine;publicclassTester:MonoBehaviour{[SerializeField]privateGameObject_object=null;privatevoidAwake(){_object.Populate(1000);}[Button]privatevoidTest(){Stopwatchstopwatch=newStopwatch();stopwatch.Start();for(inti=0;i<1000;i++){varinstance=_object.Reuse();instance.Release();}stopwatch.Stop();print($"Milliseconds: {stopwatch.ElapsedMilliseconds}");}}
Result: [16:29:36] Milliseconds: 2

About

Object Pooling for Unity

Topics

Resources

Stars

214 stars

Watchers

4 watching

Forks

Releases

Packages

Used by

Contributors

Languages