- Rename API to make it more clarifying
- 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
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"
Download latest package from the Release section. Import ObjectPooling.unitypackage to your Unity Project
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
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)}}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;}}usingToolBox.Pools;usingUnityEngine;publicclassSpawner:MonoBehaviour{[SerializeField]privateGameObject_prefab=null;publicvoidSpawn(){varinstance=_prefab.Reuse(transform.position,transform.rotation);instance.Release();}}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(){}}Creating and destroying 1000 objects.
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}");}}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}");}}