Created
February 5, 2012 13:46
-
-
Save boj/1745663 to your computer and use it in GitHub Desktop.
Unity3d Object Recycle System
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class LinkObject { | |
public LinkObject prev = null; | |
public LinkObject next = null; | |
public GameObject data; | |
public LinkObject(GameObject link) { | |
data = link; | |
} | |
} | |
public class GameObjectQueue { | |
public GameObject objectPrefab; | |
private LinkObject list = null; | |
private LinkObject last = null; | |
private int limit = 0; | |
private int count = 0; | |
private int objectCount = 0; | |
public GameObjectQueue(GameObject prefab, int spawnLimit) { | |
objectPrefab = prefab; | |
limit = spawnLimit; | |
} | |
public int DebugCount() { | |
return count; | |
} | |
public int DebugObjectCount() { | |
return objectCount; | |
} | |
public void Prime(int amount) { | |
for (int i = 0; i < amount; i++) { | |
GameObject obj = Object.Instantiate(objectPrefab) as GameObject; | |
obj.GetComponent<QueueBehaviour>().DestroyToQueue(); | |
} | |
} | |
public void Push(GameObject link) { | |
count++; | |
if (list == null) { | |
list = new LinkObject(link); | |
last = list; | |
} else { | |
LinkObject obj = new LinkObject(link); | |
list.prev = obj; | |
obj.next = list; | |
list = obj; | |
} | |
} | |
public GameObject Pop() { | |
if (limit > 0 && objectCount > limit) | |
return null; | |
if (list == null) { | |
objectCount++; | |
GameObject obj = Object.Instantiate(objectPrefab) as GameObject; | |
return obj; | |
} else { | |
count--; | |
// set object to last in list | |
LinkObject obj = last; | |
// pull out the data | |
GameObject link = obj.data; | |
// reset the list to the previous object | |
if (obj.prev != null) { | |
last = obj.prev; | |
last.next = null; | |
} else { | |
list.next = null; | |
list.prev = null; | |
list.data = null; | |
list = last = null; | |
} | |
// reset object | |
link.GetComponent<QueueBehaviour>().Reset(); | |
// return GameObject | |
return link; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
// Inherits from custom class CoreBehaviour which inherits from Unity's MonoBehaviour | |
// and implements small optimizations such as transform caching, etc. | |
public class QueueBehaviour : CoreBehaviour { | |
// the string signifying this object type for queue tracking | |
public string prefabType; | |
public virtual void Reset() { | |
// optional reset handler | |
transform.SendMessage("OnReset", SendMessageOptions.DontRequireReceiver); | |
// various bits of code to manage turning on colliders, audio, animations, etc | |
// or simply enable the object | |
... | |
} | |
public virtual void DestroyToQueue() { | |
// optional destroy handler | |
transform.SendMessage("OnDestroyToQueue", SendMessageOptions.DontRequireReceiver); | |
// various bits of code to manage turning off colliders, audio, animations, etc | |
// or simply disable the object | |
... | |
queueSystem.Push(prefabType, gameObject); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections.Generic; | |
[System.Serializable] | |
public class QueueLinks { | |
public string prefabType; | |
public GameObject prefab; | |
public int spawnLimit = 0; | |
public int primerAmount = 0; | |
} | |
public class QueueSystem : MonoBehaviour { | |
public QueueLinks[] queueLinks; | |
private Dictionary<string, GameObjectQueue> queueDict = new Dictionary<string, GameObjectQueue>(); | |
void Awake() { | |
foreach (QueueLinks p in queueLinks) { | |
queueDict.Add(p.prefabType, new GameObjectQueue(p.prefab, p.spawnLimit)); | |
} | |
} | |
void Start() { | |
foreach (QueueLinks p in queueLinks) { | |
queueDict[p.prefabType].Prime(p.primerAmount); | |
} | |
} | |
public int DebugCount(string type) { | |
return queueDict[type].DebugCount(); | |
} | |
public int DebugObjectCount(string type) { | |
return queueDict[type].DebugObjectCount(); | |
} | |
public void Push(string type, GameObject obj) { | |
queueDict[type].Push(obj); | |
} | |
public GameObject Pop(string obj) { | |
return queueDict[obj].Pop(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment