Unity Pool Manager With Code

PoolManager is a concept based on garbage collection and performance improvement of games. When we building game but we have to focus performance of game should be same when we large amount of object are spawning when game don't need those objects and we are using Destroy(gameobject); but it cost game performance it will decrease performance of and decrease Framerate of game.

What should we do where we will increase Performance of Game without compromising Game Performance.

When instances of a Prefab are needed over and over, it is more efficient to only create one, and instead of destroying it, reuse it, and we achieve by using PoolManger

Init Prefab Pool using below code. // Objects to be pooled at initialization. public ObjectToPool[] prefabsToPool;

Dictionary helps to manage pool objects by its name to reuse object. private Dictionary pools;

Create Pool using below code when pools Dictionary is null we create using new keyword. pools = new Dictionary();

Example : // Create a new pool of objects at runtime. public void CreatePool(string prefabName) { if (pools == null) pools = new Dictionary();

ObjectPool newPool = new ObjectPool(prefabName); pools.Add(prefabName, newPool); }

4)Spawn2D Method used for Spawning GameObjects using Pool checkout below code public GameObject Spawn2D(GameObject gameObj) { if (pools == null || !pools.ContainsKey(gameObj.name)) CreatePool (gameObj.name); return pools[gameObj.name].Spawn2D(gameObj); }

5) Recycle Object after his work is done. it's the same as Destroy GameObject but from Pool check out below code. Example :

// Recycle an object with the given name. public void Recycle(string prefabName, GameObject obj) {

if(prefabName==null || prefabName.Equals("")) { return; }

if (!pools.ContainsKey(prefabName)) return;

pools[prefabName].Recycle(obj); }

How To use Singleton PoolManager class for Game

1) Spawn Game Object using Below code PoolManager.Instance.Spawn2D(Ball);

2) Put below code in What you Spawning like Ball: private void Awake() { gameObject.name = gameObject.name.Replace("(Clone)",""); }

3)Now gameobject completed his task Recycle to Dictionary for next use

PoolManager.Instance.Recycle(gameObject.name,this.gameObject);

Code ��

PoolManager :

using UnityEngine; using System.Collections; using System.Collections.Generic;

// A struct for objects to be pooled. [System.Serializable] public class ObjectToPool { //public GameObject prefab; public int initialCapacity; }

// Singleton for managing pools of different objects. public class PoolManager : MonoBehaviour {

#region PUBLIC VARIABLES // Objects to be pooled at initialization. public ObjectToPool[] prefabsToPool; #endregion

#region PRIVATE VARIABLES private Dictionary pools; #endregion

#region SINGLETON PATTERN public static PoolManager _instance; public static PoolManager Instance { get { if (_instance == null) { _instance = GameObject.FindObjectOfType(); if (_instance == null) { GameObject container = new GameObject("PoolManager"); //container.transform.parent = GameObject.Find("GameSceneRoot").transform; _instance = container.AddComponent(); } } return _instance; } } #endregion

#region PUBLIC METHODS

void Awake() { if(PoolManager.Instance != null) { DontDestroyOnLoad(gameObject); } }

// Create a new pool of objects at runtime. public void CreatePool(string prefabName) { if (pools == null) pools = new Dictionary();

ObjectPool newPool = new ObjectPool(prefabName); pools.Add(prefabName, newPool); }

// Spawn an object with the given name. public GameObject Spawn(string prefabName) { if (pools == null || !pools.ContainsKey(prefabName)) CreatePool (prefabName);

return pools[prefabName].Spawn(); }

public GameObject Spawn2D(string prefabName) { if (pools == null || !pools.ContainsKey(prefabName)) CreatePool (prefabName); return pools[prefabName].Spawn2D(); }

public GameObject Spawn2D(GameObject gameObj) { if (pools == null || !pools.ContainsKey(gameObj.name)) CreatePool (gameObj.name); return pools[gameObj.name].Spawn2D(gameObj); }

// Spawn an object with the given name and position. public GameObject SpawnAtPosition(string prefabName, Vector3 pos) { if (!pools.ContainsKey(prefabName)) return null; return pools[prefabName].SpawnAtPosition(pos); }

// Recycle an object with the given name. public void Recycle(string prefabName, GameObject obj) {

if(prefabName==null || prefabName.Equals("")) { return; }

if (!pools.ContainsKey(prefabName)) return;

pools[prefabName].Recycle(obj); }

#endregion }

2) ObjectPool Code:

using UnityEngine; using System.Collections; using System.Collections.Generic;

// A pool of objects that can be reused. public class ObjectPool {

#region PRIVATE VARIABLES private Queue pool; private GameObject prefab; private string prefabName;

private Transform parent; #endregion

#region PUBLIC METHODS // Create a new object pool.

public ObjectPool(string _prefabName) { pool = new Queue(); prefabName = _prefabName; parent = new GameObject(prefabName + " Pool").transform; // parent.parent = GameObject.Find ("GameSceneRoot").transform;

}

// Spawn an object from the pool. public GameObject Spawn() { GameObject obj;

if (pool.Count > 0) obj = pool.Dequeue(); else { obj = GameObject.Instantiate(Resources.Load(prefabName))as GameObject ; obj.transform.parent = parent; } obj.SetActive(true); return obj; }

public GameObject Spawn2D() { GameObject obj; if (pool.Count > 0) obj = pool.Dequeue(); else { obj = GameObject.Instantiate(Resources.Load(prefabName))as GameObject ; //obj.transform.parent = parent; } obj.SetActive(true); return obj; }

public GameObject Spawn2D(GameObject gameObj) { GameObject obj; if (pool.Count > 0) obj = pool.Dequeue(); else { obj = GameObject.Instantiate(gameObj)as GameObject ; //obj.transform.parent = parent; } obj.SetActive(true); return obj; } // Spawn an object from the pool at specific position. public GameObject SpawnAtPosition(Vector3 pos) { GameObject obj = Spawn(); obj.transform.position = pos; return obj; }

// Recycle an object back into the pool. public void Recycle(GameObject obj) { pool.Enqueue(obj); obj.SetActive(false); } #endregion }

Comments

Popular posts from this blog

Vidmake Video Editor

Super Unicorn

Obscan Privacy Policy