All Projects → IntoTheDev → Object-Pooling-for-Unity

IntoTheDev / Object-Pooling-for-Unity

Licence: MIT, Unknown licenses found Licenses found MIT LICENSE Unknown LICENSE.meta
Object Pooling for Unity

Programming Languages

C#
18002 projects

Projects that are alternatives of or similar to Object-Pooling-for-Unity

simple-pool
#NVJOB Simple Pool. Pool for optimizing object loading. Unity Asset.
Stars: ✭ 16 (-86.44%)
Mutual labels:  object-pool, unity-pool, object-pooling
qr-code-unity-3d-read-generate
Generating a QR code / Scanning a QR code in Unity 3D. Pre-build DLL and sample code from old Unity
Stars: ✭ 70 (-40.68%)
Mutual labels:  unity-scripts, unity2d
Unitylibrary
📚 Library of all kind of scripts, snippets & shaders for Unity
Stars: ✭ 1,968 (+1567.8%)
Mutual labels:  unity-scripts, unity2d
Qframework
Unity3D System Design Architecture
Stars: ✭ 2,326 (+1871.19%)
Mutual labels:  unity-scripts, unity2d
gocs
GameObject Component System for Unity
Stars: ✭ 102 (-13.56%)
Mutual labels:  unity-scripts, unity2d
Unity-Visual-Behavior-Tree
Reactive Visual Scripting Behavior Tree Tool for Unity 2018.x+
Stars: ✭ 36 (-69.49%)
Mutual labels:  unity-scripts, unity2d
unity-dijkstras-pathfinding
Dijkstra's Pathfinding Algorithm Unity Implementation. (Not being maintained by me, it is just an experiment.)
Stars: ✭ 80 (-32.2%)
Mutual labels:  unity-scripts, unity2d
UnityDebug
A wrapper script for Unity debug calls to use conditional attributes in order to avoid debug code being compiled into release builds.
Stars: ✭ 29 (-75.42%)
Mutual labels:  unity-scripts, unity2d
Savegamepro
A Complete and Powerful Save Game Solution for Unity (Game Engine)
Stars: ✭ 30 (-74.58%)
Mutual labels:  unity-scripts, unity2d
Unity Ui Examples
📚 A collection of UI examples for Unity.
Stars: ✭ 152 (+28.81%)
Mutual labels:  unity-scripts, unity2d
Unity Assetpipeline Presentation
Unity project for "A Technical Deep-Dive into Unity's Asset Pipeline" presented at Develop: 2018
Stars: ✭ 31 (-73.73%)
Mutual labels:  unity-scripts, unity2d
U.movin
Unity library for rendering After Effects shape animations
Stars: ✭ 122 (+3.39%)
Mutual labels:  unity-scripts, unity2d
Unity-FPS-Counter
#NVJOB FPS Counter and Graph. Free Unity Asset.
Stars: ✭ 44 (-62.71%)
Mutual labels:  unity-scripts, unity2d
Impulse
A barebones C# bootstrap framework for building scalable projects quickly and easily in Unity.
Stars: ✭ 109 (-7.63%)
Mutual labels:  unity-scripts, unity2d
Apple Signin Unity
Unity plugin to support Sign In With Apple Id
Stars: ✭ 228 (+93.22%)
Mutual labels:  unity-scripts, unity2d
Unity Script Collection
A maintained collection of useful & free unity scripts / library's / plugins and extensions
Stars: ✭ 3,640 (+2984.75%)
Mutual labels:  unity-scripts, unity2d
Unity3d Dynamicallyloadinganimation
👾 Unity3D Loading and unloading animations at runtime (Example)
Stars: ✭ 74 (-37.29%)
Mutual labels:  unity-scripts, unity2d
UnityHexagonLibrary2d
A library to manage 2D hexagonal tiles in Unity.
Stars: ✭ 58 (-50.85%)
Mutual labels:  unity-scripts, unity2d
Nvjob Water Shader Simple And Fast
#NVJOB Simple Water Shaders. Free Unity Asset.
Stars: ✭ 172 (+45.76%)
Mutual labels:  unity-scripts
Infinity Square Space
Infinity Square/Space. The prototype of the game is open source. Unity Asset.
Stars: ✭ 122 (+3.39%)
Mutual labels:  unity-scripts

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/IntoTheDev/Object-Pooling-for-Unity.git

or

Open the manifest.json file of your Unity project. Add "com.intothedev.objectpooling": "https://github.com/IntoTheDev/Object-Pooling-for-Unity.git"

Manual Installation (Version can be outdated)

Download latest package from the Release section. Import ObjectPooling.unitypackage to your Unity Project

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
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].