Code Monkey home page Code Monkey logo

unity-object-pool's Introduction

Unity Object Pool

An elegant object pool and manager implementation for Unity

Overview

An object pool provides an efficient way to reuse objects, and thus keep the memory foot print of all dynamically created objects within fixed bounds. This is crucial for maintianing consistent framerates in realtime games (especially on mobile), as frequent garbage collection spikes would likley lead to inconsistent performance.

Usage

There are two main points of interest:

  1. A pool manager class for Unity Game Objects that allows you to easily pool scene objects

  2. A generic object pool collection that can be used for non Unity Game Objects.

###Pooling Unity Game Objects: PoolManager.cs is a very flexible class that co-ordinates all your pooling requirements in a scene.

It requires no initialization, can be called from anywhere, and dynamically accomodates any prefab game object.

//Optional: Warm the pool and preallocate memory
void Start()
{
	PoolManager.WarmPool(bulletPrefab, 50);
	
	//Notes
	// Make sure the prefab is inactive, or else it will run update before first use
}

//Spawn pooled objects
void FireBullet(Vector3 position, Quaternion rotation)
{
	var bullet = PoolManager.SpawnObject(bulletPrefab, position, rotation).GetComponent<Bullet>();
		
	//Notes:
	// bullet.gameObject.SetActive(true) is automatically called on spawn 
	// When done with the instance, you MUST release it!
	// if the number of objects in use exceeds the pool size, new objects will be created
	
}

//In Bullet.cs
void Finish()
{
    PoolManager.ReleaseObject(this.gameObject);
    
    //Notes
    // This takes the gameObject instance, and NOT the prefab instance.
    // Without this call the object will never be available for re-use!
    // gameObject.SetActive(false) is automatically called;
}

###Pooling C# objects: This allows you to pool objects not derived from the Unity engine. In fact if you replaced the Debug statemetns you could use this in any other .NET or C# project.

This is the backbone of the PoolManager.cs class, but you can use it directly. For instance you could use it to pool events in a memory friendly observer pattern:

//The factoryFunc (first arg) is the crux of the ObjectPool class. 
//It privodes a way for the ObjectPool to dynamically create new objects
eventPool = new ObjectPool<DelayedEvent>(()=> new DelayedEvent(), 5);

var evt = eventPool.GetItem();
evt.Start(Time.time, delay); //Configure object

//On event done:
eventPool.ReleaseItem(evt);
	

History

Created by Peter Cardwell-Gardner

Born out of the realization that he was writing the samething over and over again, this was designed to be the last object pool you ever need.

This project is used extensively in the upcoming Unity game Cadence by Made With Monster Love.

Licence

MIT

unity-object-pool's People

Contributors

thefuntastic avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.