Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mukarillo/unitypooling

An optimized approach object pooling.
https://github.com/mukarillo/unitypooling

pool-controller pooling pooling-utility unity unity2d unity3d-plugin

Last synced: 2 months ago
JSON representation

An optimized approach object pooling.

Awesome Lists containing this project

README

        

# UnityPooling
An optimized approach object pooling.


Example

## How to use
*you can find a pratical example inside this repository in PoolingScene scene*

### 1 - Create a class that extends `PoolingObject` and, if you wish, implement its virtual members (`OnRelease` and `OnCollect`)
```c#
public class ObjectExample : PoolingObject {
private Renderer mRenderer;
private MaterialPropertyBlock mPropBlock;

void Awake()
{
mPropBlock = new MaterialPropertyBlock();
mRenderer = GetComponent();
}

public override void OnCollect()
{
mRenderer.GetPropertyBlock(mPropBlock);
mPropBlock.SetColor("_Color", Random.ColorHSV());
mRenderer.SetPropertyBlock(mPropBlock);

Invoke("OnRelease", 2f);

base.OnCollect();
}
}
```
### 2 - Create a class to initiate the Pooling and call `Collect` whenever you need an instance of T
```c#
public class PoolingExample : MonoBehaviour {
public GameObject referenceObject;

private Pooling mPooling = new Pooling();

void Start () {
mPooling.Initialize(10, referenceObject, transform);
}

void Update () {
if(Input.GetKeyDown(KeyCode.Space))
{
var o = mPooling.Collect();
o.transform.position = new Vector3(Random.Range(-13f, 13f), Random.Range(-6.5f, 6.5f), 2.66f);
}
}
}
```

## Pooling< T > `public` overview
### Properties
|name |type |description |
|--|--|--|
|`createMoreIfNeeded` |**bool** |*boolean to control if the system can create more objects if needed. Its true by default.* |
|`OnObjectCreationCallBack` |**delegate ObjectCreationCallback** |*callback triggered whenever a new object is created, send T as parameter* |

### Methods

> `pooling.Initialize`
- *Description*: Initiate the pooling, creating `amount` `objReference` and attaching `T` to the object.

- *Parameters*:

|name |type |description |
|--|--|--|
|`amount` |**int** |*initial amount to be created.* |
|`refObject` |**GameObject** |*a reference of the object that will be instantiated and handled by the pool.* |
|`parent` |**Transform** |*the parent of the instantiated object. set `null` if creating in root.* |
|`worldPos` |**Vector3** |*the initial position for instantiated object.* |
|`startState` |**int** |*the initial state for instantiated object, if true, it will call OnCollect when instantiating each object, if false, OnRelease* |

> `pooling.Collect`
- *Description*:
Returns an instance of `T` object that is not currently being used.

- *Parameters* :

|name |type |description |
|--|--|--|
|`parent` |**Transform** |*if you need to change the parent of the object, do it here.* |
|`position` |**Vector3** |*position of the object* |
|`localPosition` |**bool** |*if true, `position` will be int local space, else, it will be world space.* |

> `dynamicScroll.Release`
- *Description*: Releases the object.

- *Parameters* :

|name |type |description |
|--|--|--|
|`obj` |**T** |*Releases the object.* |

> `dynamicScroll.GetAllWithState`
- *Description*: Returns a list with objects either being used or not being used.

- *Parameters* :

|name |type |description |
|--|--|--|
|`active` |**bool** |*if true, return a `List` of actived objects, else, unused objects.* |