com.pika.objectpooling
v0.1.3
Published
Object Pooling is a great way to optimize your projects and lower the burden that is placed on the CPU when having to rapidly create and destroy GameObjects.
Downloads
1
Maintainers
Readme
Object Pooling SDK
Object Pooling is a great way to optimize your projects and lower the burden that is placed on the CPU when having to rapidly create and destroy GameObjects.
- Generic type objects
- Particle System
Table of Contents
Requirements
- Unity 2019.4 or newer
Installation
There is only one method of installation available for the Object Pooling SDK:
From within the Unity Editor navigate to Edit > Project Settings and then to the Package Manager settings tab.
Create a New Scoped Registry by entering
Name npmjs URL https://registry.npmjs.org Scope(s) com.pika
and click Save.
Open the Window > Package Manager and switch to My Registries via the Packages: dropdown menu. You will see the Object Pooling SDK package available on which you can then click Install for the platforms you would like to include. Dependencies will be added automatically.
Depending on your project configuration and if you are upgrading from a previous version, some of these steps may already be marked as "completed"
Usage
Creating Instance
There is two method of creating a pool:
- Creating directly from the ObjectPooling class:
//Create a pool from ObjectPooling.Instance with desired prefab to be pooled as parameter
var exampleObjectPool = ObjectPooling.Instance.CreatePool(exampleObjectPrefab);
//You can expand pool options with these methods
exampleObjectPool.AsParent(transform); // Default transfrom of inactive items in the pool
exampleObjectPool.WithInitialSize(10); // Initial size o the pool
- Creating the instance
//Create a pool directly
var pool = new Pool<ExampleObject>(exampleObjectPrefab);
exampleObjectPool = ObjectPooling.Instance.InitializePool<Pool<ExampleObject>>(pool);
Spawn Objects
// Spawn object from the pool
var clone = exampleObjectPool.Spawn();
//If you trying to access the pool from other scopes, and you have the reference of the prefab
//You can access to that pool by calling this method
var exampleObjectPool = ObjectPooling.Instance.GetPool(prefab);
//Then spawn from that pool
var clone = exampleObjectPool.Spawn();
// You can pass parameters to the Spawn method like as Unity.Object.Instantiate(...) method
// Transform as parent
// Vector3 as global position
// Quaternion as global rotation
var clone = exampleObjectPool.Spawn(parent, position, rotation);
// If you have the reference of the prefab, you can just call:
prefab.Spawn(); // This method automaticly creates the pool by itself with default options
Recycle Objects
// Once you want to return the object to the pool, you need to call recycle method from the pool
exampleObject.Recycle(clone);
// If you have not reference of the pool you can just call these
// **Reminder: Calling Recycle() directly from the related pool as mentioned above is recommended in concern of performance!***
ObjectPooling.Instance.Recycle(clone);
// Or
clone.Recycle();
//All clones can be returned to the pool by this method
exampleObjectPool.RecycleAll();
//All clones can be destroyed including actives and inactives by this method
exampleObjectPool.DestroyAll();
Particle Pooling
To use particles with this system, you need to do two steps in target prefab:
- Set "Stop Action" to "CallBack" in particle system
- Add Component PooledParticleObject
Callbacks
- ICreationCallbackReceiver
- ISpawnCallbackReceiver
- IRecycleCallbackReceiver
These interfaces help you to receive callbacks when object is created, spawned and recycled
public class ExampleObject : MonoBehaviour, ICreationCallbackReceiver, ISpawnCallbackReceiver, IRecycleCallbackReceiver
{
public void OnCreated()
{
}
public void OnSpawned()
{
}
public void OnRecycle()
{
}
}