objectpool_preview

ObjectPool

Posted by on September 25th, 2013

Download Package GitHub Repository

Change Log

VERSION 1.00 – September 25, 2013

License

The MIT License (MIT)

Copyright (c) 2013 UnityPatterns

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Features

  • Selectively pool objects based on prefab type
  • Simple and expressive syntax for instantiation and recycling

How to Use

Create the Prefab and Pool

This script stores objects based on what prefab they were instantiated with, so first you have to create a prefab and reference it from the scene object that is going to create it. Here I have a Turret that is going to instantiate a Bullet prefab.

using UnityEngine;
using System.Collections;

public class Turret : MonoBehaviour
{
	public Bullet bulletPrefab;
}

Once that is ready, all you have to do is create the object pool for the Bullet.

using UnityEngine;
using System.Collections;

public class Turret : MonoBehaviour
{
	public Bullet bulletPrefab;

	void Start()
	{
		ObjectPool.CreatePool(bulletPrefab);
	}
}

You only need to do this once per prefab, so usually this makes sense to do in Start() or Awake(). subsequent calls will be safely ignored.

Spawn/Recycle

To use pooled objects, you simply have to replace your normal Instantiate() and Destroy() calls with calls to ObjectPool.Spawn() and ObjectPool.Recycle() instead!

To instantiate an object, call ObjectPool.Spawn() and pass in the prefab (and optionally position/rotation). This function returns a reference to the created instance.

//Spawn an object from the pool
bulletInstance = ObjectPool.Spawn(bulletPrefab);
bulletInstance = ObjectPool.Spawn(bulletPrefab, position);
bulletInstance = ObjectPool.Spawn(bulletPrefab, position, rotation);

When you want to “destroy” the object, just call ObjectPool.Recycle() and pass in the reference that was returned by Spawn().

//Disable the object and return it to the pool
ObjectPool.Recycle(bulletInstance);

Shorthand Syntax

The script comes with a few extension methods to make using the object pools even more natural and expressive!

//Just call Spawn() on the prefab directly!
bulletInstance = bulletPrefab.Spawn(position, rotation);

//And to recycle the instance...
bulletInstance.Recycle();

That looks super clean, and requires no casting on your part like the usual Unity instantiation calls.

The best way to use ObjectPool is to just completely replace Instantiate/Destroy with Spawn/Recycle. If CreatePool() was not called, Spawn/Recycle will just instantiate and destroy objects as usual, making it really easy to toggle pooling on/off per prefab.

Problems, Assistance, Bugs?

If you have any suggestions for how this tool could be improved, features you’d like to see, or any bugs you’ve found, please contact me and let me know!

Email Chevy