Giter Site home page Giter Site logo

objectpooling's Introduction

Object Pooling for Unity

TODO

  • Rename API to make it more clarifying

Features

  • Faster in terms of performance than Instantiate/Destroy (Test at the end of README)
  • Easy to use
  • Easy to integrate with already written spawn systems
  • Callbacks OnReuse & OnRelease to reset object's state

How to Install

Git Installation (Best way to get latest version)

If you have Git on your computer, you can open Package Manager indside Unity, select "Add package from Git url...", and paste link https://github.com/Binaryinject/ObjectPooling.git

Usage

How to populate pool:

using ToolBox.Pools;
using UnityEngine;

public class Spawner : MonoBehaviour
{
	[SerializeField] private GameObject _prefab = null;
	
	private void Awake()
	{
		_prefab.Populate(count: 50);
	}
}

Also, you can just put PoolInstaller component on any object on Scene and select which objects you want to prepopulate

How to clear pool and it's instances

using ToolBox.Pools;
using UnityEngine;

public class Spawner : MonoBehaviour
{
	[SerializeField] private GameObject _prefab = null;
	
	private void Awake()
	{
		_prefab.Populate(count: 50);
		
		// If destroy active is true then even active instances will be destroyed
		_prefab.Clear(destroyActive: true)
	}
}

How to get object from pool:

using ToolBox.Pools;
using UnityEngine;

public class Spawner : MonoBehaviour
{
	[SerializeField] private GameObject _prefab = null;
	
	public void Spawn()
	{
		_prefab.Reuse(transform.position, transform.rotation);
		
		// Get object from pool with component
		_prefab.Reuse<Rigidbody>(transform.position, transform.rotation).isKinematic = true;
	}
}

How to release object:

using ToolBox.Pools;
using UnityEngine;

public class Spawner : MonoBehaviour
{
	[SerializeField] private GameObject _prefab = null;
	
	public void Spawn()
	{
		var instance = _prefab.Reuse(transform.position, transform.rotation);
		instance.Release();
	}
}

How to use callbacks:

using ToolBox.Pools;
using UnityEngine;

public class Health : MonoBehaviour, IPoolable
{
	[SerializeField] private float _maxHealth = 100f;
	
	private float _health = 0f;
	
	// Awake will be called on first _prefab.Reuse()
	private void Awake() 
	{
		OnReuse();
	}
		
	// IPoolable method
	/// <summary>
	/// This method will be called on 2nd Reuse call.
	/// Use Unity's Awake method for first initialization and this method for others
	/// </summary>
	public void OnReuse()
	{
		_health = _maxHealth;
	}
		
	// IPoolable method
	public void OnRelease() { }
}

Peformance test:

Creating and destroying 1000 objects.

Instantiate/Destroy:

using Sirenix.OdinInspector;
using System.Diagnostics;
using UnityEngine;

public class Tester : MonoBehaviour
{
	[SerializeField] private GameObject _object = null;
	
	[Button]
	private void Test()
	{
		Stopwatch stopwatch = new Stopwatch();
		stopwatch.Start();
		
		for (int i = 0; i < 1000; i++)
		{
			var instance = Instantiate(_object);
			Destroy(instance);
		}
		
		stopwatch.Stop();
		print($"Milliseconds: {stopwatch.ElapsedMilliseconds}");
	}
}
Result: [16:26:15] Milliseconds: 6

Get/Release:

using Sirenix.OdinInspector;
using System.Diagnostics;
using ToolBox.Pools;
using UnityEngine;

public class Tester : MonoBehaviour
{
	[SerializeField] private GameObject _object = null;
	
	private void Awake()
	{
		_object.Populate(1000);
	}
	
	[Button]
	private void Test()
	{
		Stopwatch stopwatch = new Stopwatch();
		stopwatch.Start();
		
		for (int i = 0; i < 1000; i++)
		{
			var instance = _object.Reuse();
			instance.Release();
		}
		
		stopwatch.Stop();
		print($"Milliseconds: {stopwatch.ElapsedMilliseconds}");
	}
}
Result: [16:29:36] Milliseconds: 2

objectpooling's People

Contributors

binaryinject avatar

Stargazers

 avatar

Watchers

 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.