Skip to content

Latest commit

 

History

History
 
 

ObjectPooling

Object pooling helper class


These scripts help you set up an object pooling system in your game. It's pretty simple to use, let me show you how:


1. Initializing the pool

As you can see in the image bellow, you can initialize the pool by creating a new pool and then calling it in the start.

Init Image

You'll also have to add the namespace that the pool class is in using UnityHelper.Pooling;

2. Before you start the game

You will have to add the objects that you want the pool to instantiate when it starts. As you can see in the image bellow, the pool object looks like this in the editor:

Look Image

The pool contains a List of objects that you want to instantiate in the scene. The objects are of type PoolObject which you can see here:

List Image

The obj_to_pool is the gameobject that you want to instantiate and the amount_to_pool is the amount of objects that you want to instantiate.

3. Spawning the objects in the scene

Spawning an object is very easy, the **Pool** class comes with a function called **Spawn_Item(Type type, Vector3 position, Quaternion direction)** it returns a **Component** so you can get direct reference to the object. Here's how you do it:

Spawn Image

In my example I have a custom Object created by me called CubeObj it doesn't do much, but it helps with the example. So when you spawn an item in the scene you have to give the function the following parameters:

Type the typeof object that the list needs to look for and spawn it in the scene

Position the position where the item should spawn

Rotation the rotation of the item

One more thing to remember. Pool has a function to bring back the objects to the pool, but it requires the object as a parameter. That's why I made an interface called IPoolActions that comes with ReturnObjectToPool void and will help you return the items to the pool. It's pretty easy, to return an item you simply have to gameobject.SetActive(false) and also reset its position transform.position = Vector3.zero.