https://github.com/rodrigoueda/com.rodrigoueda.objectpool
https://github.com/rodrigoueda/com.rodrigoueda.objectpool
objectpool objectpool-pattern package unity unity3d
Last synced: 4 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/rodrigoueda/com.rodrigoueda.objectpool
- Owner: rodrigoueda
- License: mit
- Created: 2020-09-23T18:20:40.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-09-23T20:29:43.000Z (over 5 years ago)
- Last Synced: 2025-05-17T14:09:17.906Z (8 months ago)
- Topics: objectpool, objectpool-pattern, package, unity, unity3d
- Language: C#
- Homepage:
- Size: 10.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Unity Object Pool
## Usage
- Use the namespace ``` ObjectPool ```
- Your new prefab reference needs to be of type ``` PoolableGameObject ``` instead of ``` GameObject ```
- On your ``` Start ``` method, call the ``` Prewarm() ```function
```C#
using UnityEngine;
using ObjectPool;
public class TestObjectPool : MonoBehaviour
{
public PoolableGameObject cubes;
void Start()
{
cubes.Prewarm();
}
}
```
- On unity editor, there is additional information to edit:

- **Prefab**: The GameObject to be pooled
- **AmountToPool**: How many GameObjects should be instatiated
- **ShouldExpand**: Should expand the pool when every instantiated objects are already in use?
- **Disposable**: Should dispose when the pool is soft flushed?
## Retrieving
- Instead of using unity ``` Object.Instantiate ``` static method, switch to ``` ObjectPoolManager.Instance.Retrieve(PooledGameObject) ```
- ``` PooledGameObject.instance ``` is the now the way to access the created GameObject
```C#
void Update()
{
if (Input.GetKeyUp(KeyCode.Space)) {
PooledGameObject gameObj = ObjectPoolManager.Instance.Retrieve(cubes);
gameObj.instance.transform.parent = this.transform;
gameObj.instance.transform.position = Vector3.zero;
gameObj.instance.SetActive(true);
}
}
```
- There is a second optional parameter to the ``` Retrieve ``` function which creates the objects with a delay self recycle
- This parameter represents the time in seconds
```C#
void Update()
{
if (Input.GetKeyUp(KeyCode.Space)) {
PooledGameObject gameObj = ObjectPoolManager.Instance.Retrieve(cubes, 2f);
gameObj.instance.transform.parent = this.transform;
gameObj.instance.transform.position = Vector3.zero;
gameObj.instance.SetActive(true);
}
}
```
## Recycling
- Switch ``` Object.Destroy ``` to ``` ObjectPoolManager.Instance.Recycle(PooledGameObject) ```
```C#
void Update()
{
if (Input.GetKeyUp(KeyCode.Escape)) {
ObjectPoolManager.Instance.Recycle(gameObj);
}
}
```
- Also, it's possible to delay the recycle using a second parameter, the value is in seconds
```C#
void Update()
{
if (Input.GetKeyUp(KeyCode.Escape)) {
ObjectPoolManager.Instance.Recycle(gameObj, 1f);
}
}
```
## Flushing the pool
- Use the method ``` ObjectPoolManager.Instance.DisposePool(); ``` to empty the pool
- Remember that only the objects marked as Disposable will be destroyed
- To hard flush the pool, removing every single object, use a boolean parameter ``` ObjectPoolManager.Instance.DisposePool(true); ```
## IPoolable interface
- Optionally, the prefab script can implement IPoolable interface
- This way, every time it's instance is retrieved or recycled, the methods ``` Retrieve() ``` and ``` Recycle() ``` will be fired respectively.
```C#
using UnityEngine;
using ObjectPool;
public class CubeBehaviour : MonoBehaviour, IPoolable
{
public void Retrieve()
{
print("Object Retrieved");
}
public void Recycle()
{
print("Object Recycled");
}
}
```
## Managed Update
- When working with a great amount of objects, it's considered a good practice to center each individual ``` Update() ``` into a single behaviour in order to optimize performance
- ObjectPoolManager is also able to concentrate instanced objects updates, just extend IManagedUpdate interface
- It's possible to combine IPoolable and IManagedUpdate interfaces
```C#
using UnityEngine;
using ObjectPool;
public class CubeBehaviour : MonoBehaviour, IPoolable, IManagedUpdate
{
public void Retrieve()
{
ObjectPoolManager.Instance.RegisterManagedUpdate(this);
}
public void Recycle()
{
ObjectPoolManager.Instance.UnregisterManagedUpdate(this);
}
public void ManagedUpdate(float deltaTime)
{
print(deltaTime);
}
}
```