Pro Pooling Tutorial – Pooling, Spawning and Despawning


This tutorial will show you in detail how to get started with Pro Poolingand and how it can both increase the performance of your game and save on development time. It will walk you through setting up pools, spawning, despawning and the spawn lifecycle. I completed demo is available here.

This tutorial assumes a basic familiarity with Unity such as being able to import assets from the asset store, add gameobjects and components.

Initial Setup

Create a new Unity project set to 3D mode and import the Pro Pooling asset from the asset store.

When Pro Pooling is imported, you will find all of the Pro Pooling components referred to in this tutorial under the Add Components menu within the Pro Pooling submenu.

Setting Up Pools

We will set up our pools within the Global Pools component. As Global Pools is designed to live across any scene changes we will need to have a seperate gameobject for it. You can also place any other ‘global’ components here too.

Add a new gameobject to your scene (GameObject Menu->Create Empty), and rename it to _GameScope and then from the Add Components menu add a Pro Pooling | Global Pools component.

Tutorial-GlobalPools

To add a pool, we need a prefab that the pool will hold instances of. In your game this might be a projectile, an enemy or some other item. Here we will create something simple for demonstration purposes.

Create a cube in your scene by right clicking in the Hierarchy tab and selecting 3D Object | Cube. We want out cube to react to gravity so add a Rigidbody component (Add Component | Physics | Rigidbody).

Next right click in the Project tab and create a new folder (Create | Folder). Rename the new folder to Prefabs. Drag the cube from the Hierarchy tab into the new Prefabs folder to create a prefab for our pool to use and delete the cube gameobject from the scene.

Finally we will create a pool for our prefab. Click on the _GameScope gameobject and drag the Cube prefab onto the Global PoolsDrag a Prefab here to add a pool” field. You should see that a pool is added with the default pool settings. The default pool settings will create a pool with 1 pre-allocated item and increase the pool size, adding new instances of the prefab, as needed. As we know we will need more that one instance at a time, set Initial Size to 50 so that we have 50 instances pre allocate on startup.

Spawning

With our pool setup we can start using the pooled items. Create a new gameobject in the Hierarchy root and rename it to Spawner, setting it’s Position to 0, 4, 0.

Click the Spawner gameobject and add a spawner component by clicking the Add Components menu and selecting Pro Pooling | Spawner. For the Spawner to do anything we need to tell it what prefabs to spawn, and this should be something that is setup as a pool. Drag the Cube prefab we created earlier onto the Spawners “Drag a Prefab to add” field.

You can now run the scene and you should see that a cube is spawned every second from the spawners location.

Stop the scene and decrease the Spawners Spawn Interval to 0.1 to make new items be spawned faster. Set also the Spawn Area Size to 10, 1, 0 to increase the area from which items are spawned. If you look at the Scene window you will see that the spawn area is shown by a ‘Gizmo’ to aid with setting it up.

Run the scene again and notice the difference.

Despawning

Spawning is now working, however if you expand the Spawner gameobject when the scene is running and look at it’s children you will see the list just grows and grows. You can confirm this also by zooming out the scene view and seeing the items falling to forever. It is also possible to look at the Global Pools component (under the special DontDestroyOnLoad runtime scene) which shows at run time a report of the pool size (you might need to resize the inspector window to get values to update). This behaviour happens because we are spawning items, but never despawning them (removing them from the scene back to the pool).

We have several options for despawning, including when physics collisions occur, however here we will despawn items after a given delay for simplicity.

Click on the Cube prefab in the Project tab. and then add a Despawn After Delay component (under Add Component | Pro Pooling). Change the Delay to 5 to despawn after 5 seconds.

Run the scene again and you should see using the above verification methods that the pool size is limited, probably to around 50 items. You might also at this stage notice some strange behaviour where our spawned items seem to be getting faster and faster. We will address that next.

Life Cycle

If you leave the scene running for a bit you will see that our spawned items seem to be getting faster and faster. This kind of behaviour is a side effect of pooling where we are pre-allocating and reusing gameobjects to avoid the runtime overheads of creating new ones. In our case, when an item is spawned for the first time it’s physics velocity is zero. As gravity takes effect it’s downwards velocity increase until it is despawned after 5 seconds. The next time the same item is reused it’s initial velocity is not zero, but whatever it was when it was despawned. This cycle repeats with the velocity continually increasing.

We need a way of resetting any necessary state such as the velocity when an item is spawned / despawned. Luckily this is made easy with Pro Pooling both with ready made components and also an interface that you can implement if you are creating your own components so you can be notified with an item is spawned or despawned.

Click on the Cube prefab in the Project tab. and then add a On Despawn Reset Rigidbody component to reset the rigidbody including the velocity.

Run the scene again and you should see everything works as expected.

Custom Life Cycle Component

It might be that we want to perform some custom action when a pool item is spawned or despawned. If used with the free Game Framework asset (or the Game Framework Extras Bundle with which Pro Pooling is included) you can simple add the OnLifeCycleRunAction component to run one or more of the many Game Framework actions when the item is spawned such as playing audio, adding a force and more. Please see the seperate Pro Pooling with Game Framework tutorial (work in progress) for more information on this component.

In this section we look at adding support for spawn and despawn events to an existing component to add a physics force when the component is spawned.

Right click in the root of the project, and select Create | C# Script. Name the new script OnSpawnAddForce. This is the existing component that we will add spawn and despawn events to (here the component is pretty much blank, but it could be an existing component with lots of code.

Double click the script to edit it. We will make the following changes (full code is shown below):

  • At the top add the line: using ProPooling;
  • Provide a parameter so the user can specify a force to add.
  • Change the public class line to implement from IPoolComponent interface: public class NewBehaviourScript : MonoBehaviour, IPoolComponent {
  • Add the IPoolComponent methods that are called when the gameobject is spawned / despawned. We will apply the force in OnSpawned
  • Cache a reference to the Rigidbody component to avoid looking up everytime.

Copy the full code below which contains the above modifications into the OnSpawnAddForce.cs script and save:

Back in the Unity Editor, click on the Cube prefab and drag our new component onto it. Set Force to 0, 0, 100.

Run the scene and you will see that out cube is now ‘fired’ with the specified force when it is spawned.

What Next

You have hopefully gotten some insight into how Pro Pooling works and what it offers. You should try playing around with some of the other options, particularly on the Spawner component. Consult the documentation for reference and look at the demos on the documentation page and included with the asset.

If you need help or support then please visit our support forum, or let us know if you have any suggestions, feature requests or comments. We are always looking to improve our products.

If you like Pro Pooling then please leave us a review on the asset store as it is great to hear how it is being used and encourages us to continue adding new features.