NightPool
Fast object pool for Unity
Install / Use
/learn @MeeXaSiK/NightPoolREADME
🚄 Night Pool
Night Pool is a fast and lightweight game object pooling solution for Unity by Night Train Code
- 🚀 Improves performance
- 😃 Allows you to cache game objects for future use
- ✅ Supports functionality of
InstantiateandDestroymethods - 🔄 Supports recycling of game objects if pools overflow
- 😋 Easily to attach your DI solution
- 🗑 Reduces GC allocations
Warning! All internal checks and exceptions works only in
DEBUGbuild versions to improve performance. EnableDevelopment Buildoption in Build Settings for development builds and disable for releases!
🚘 Old default variant:
var newGameObject = Object.Instantiate(_prefab);
Object.Destroy(newGameObject);
🚀 New performant variant:
var newGameObject = NightPool.Spawn(_prefab);
NightPool.Despawn(newGameObject);
🌐 Navigation
- Main
- Installation
- Initial setup
- How to use
- Components
- Callbacks
- Events
- Enums
- Extensions
- Preload In Editor
- How to attach DI container
- How to reset pooled rigidbody
▶ Installation
As a Unity module
Supports installation as a Unity module via a git link in the PackageManager
https://github.com/MeeXaSiK/NightPool.git
or direct editing of Packages/manifest.json:
"com.nighttraincode.nightpool": "https://github.com/MeeXaSiK/NightPool.git",
As source
You can also clone the code into your Unity project.
🔸 Initial setup
Add the Night Pool Global component to any game object in scene.
From the component menu:
Add Component -> Night Train Code -> Night Pool -> Night Pool Global
Or from project files:
Find NightPoolGlobal.cs in the project files and drag it onto any game object.
The Night Pool asset will work without manually adding the main component, but you will lose the ability to change the global settings.
🔸 How to use
Basics
Use static class NightPool to Spawn or Despawn game objects instead of Instantiate and Destroy.
using NTC.Pool;
public class Example : MonoBehaviour
{
[SerializeField] private GameObject _gameObjectPrefab;
[SerializeField] private TestComponent _componentPrefab;
public void Foo()
{
// Spawns a GameObject clone by prefab.
GameObject newGameObject = NightPool.Spawn(_gameObjectPrefab);
// Spawns a TestComponent clone by prefab.
TestComponent newTestComponent = NightPool.Spawn(_componentPrefab);
// Despawns a GameObject clone.
NightPool.Despawn(newGameObject);
// Despawns a TestComponent clone.
NightPool.Despawn(newTestComponent);
}
}
Overloads
where T : Component or GameObject
public static T Spawn<T>(T prefab);
public static T Spawn<T>(T prefab, Vector3 position, Quaternion rotation);
public static T Spawn<T>(T prefab, Vector3 position, Quaternion rotation, Transform parent);
public static T Spawn<T>(T prefab, Transform parent, bool worldPositionStays = false);
public static T Despawn<T>(T clone, float delay = 0f);
Delayed despawn
If you want to perform a despawn with a delay, you can pass the delay time to the arguments of the Despawn method:
NightPool.Despawn(newGameObject, 5f);
GameObject will be despawned after 5 seconds.
🔸 Components
Night Pool Global
This component allows you to set global pool settings, preload pools and change safety options.
| Field | Info | | ------ | ------ | | Update Type | Determines in which update method the processing should be performed. Delayed despawns are currently handled in the selected update loop of this component. | | Preload Pools Type | Should the pools be preloaded? | | Pools Preset | Stores settings for preloading pools. |
Global pool settings:
| Field | Info | | ------ | ------ | | Behaviour On Capacity Reached | What to do when pool overflows? | | Despawn Type | How clones of the pool's prefab must be despawned? | | Callbacks Type | Whether to make callbacks when clones spawn and despawn? | | Capacity | Default capacity for runtime created pools. | | Dont Destroy On Load | Should pools created at runtime be persistent? | | Send Warnings | Should pools created at runtime find issues and log warnings by default? |
Global settings will be applied to new pools created at runtime.
Safety:
| Field | Info |
| ------ | ------ |
| Night Pool Mode | Should Night Pool focus more on performance or total safety? |
| Reaction On Repeated Delayed Despawn | What will be done if you try to destroy the same clone multiple times with a delay? |
| Despawn Persistent Clones On Destroy | Should persistent clones be despawned on destroy? |
| Check Clones For Null | Should clones be checked for null on spawn? |
| Check For Prefab | Should a pool prefab be checked during setup to see if it is really prefab? |
| Clear Events On Destroy | Should the NightPool static events be cleared on destroy? |
Pools Preset
Stores settings for preloading pools.
Create -> Night Train Code -> Night Pool -> Pools Preset
| Field | Info | | ------ | ------ | | Name | It is here for convenience and beauty in the inspector and does not affect anything. The name of an element in the list will change if you change the name. | | Prefab | The prefab that pool will control. | | Behaviour On Capacity Reached | What to do when pool overflows? | | Despawn Type | How clones of the prefab must be despawned? | | Callbacks Type | Whether to make callbacks when clones spawn and despawn? | | Capacity | Capacity of the pool. | | Preload Size | Preload size of the pool. | | Dont Destroy On Load | Should the pool be persistent? | | Warnings | Should the pool find issues and log warnings by default? |
Night Game Object Pool
This component allows you to set up a specific pool.
Add Component -> Night Train Code -> Night Pool -> Night Game Object Pool
Fields
| Field | Info | | ------ | ------ | | Prefab | The prefab that pool will control. | | Behaviour On Capacity Reached | What to do when pool overflows? | | Despawn Type | How clones of the prefab must be despawned? | | Capacity | Capacity of the pool. | | Preload Type | Clones preload type of the pool. | | Preload Size | Preload size of the pool. | | Callbacks Type | Whether to make callbacks when clones spawn and despawn? | | Dont Destroy On Load | Should the pool be persistent? | | Send Warnings | Should the pool find issues and log warnings by default? |
ReadOnly Fields
| ReadOnly Field | Info | | ------ | ------ | | All Clones Count | Number of all game objects in the pool. | | Spawed Clones Count | Number of spawned game objects in the pool. | | Despawned Clones Count | Number of despawned game objects in the pool. | | Spawns Count | Total number of spawns in the pool. | | Despawns Count | Total number of despawns in the pool. | | Total | Total number of spawns and despawns in the pool. | | Instantiated | Total number of instantiates in the pool. |
Properties
[SerializeField] private NightGameObjectPool _pool;
private void Foo()
{
// Prefab attached to the pool.
GameObject prefab = _pool.AttachedPrefab;
// Overflow behaviour of the pool.
BehaviourOnCapacityReached behaviourOnCapacityReached = _pool.BehaviourOnCapacityReached;
// GameObject despawn type of the pool.
DespawnType despawnType = _pool.DespawnType;
// Callbacks type on GameObject spawned or despawned of the pool.
CallbacksType callbacksType = _pool.CallbacksType;
// Capacity of the pool.
int capacity = _pool.Capacity;
// Number of spawned clones.
int spawnedClonesCount = _pool.SpawnedClonesCount;
// Number of despawned clones.
int despawnedClonesCount = _pool.DespawnedClonesCount;
// Number of all clones.
int allClonesCount = _pool.AllClonesCount;
