Here is a description of the object pool. The advantage of this is that it is a constant amount of time whether it is taken out or inserted into a game object.
Using unityengine;using system.collections;using system.collections.generic;//Object Pool public class Gameobjectpool: monosingleton<gameobjectpool>{//<summary> may store multiple kinds of objects, with multiple objects per category </summary> private DICTIONARY&L t;string, linkedlist<gameobject>> Pool = new dictionary<string, linkedlist<gameobject>> (); Private Dictionary<gameobject, linkedlistnode<gameobject>> objectdic = new Dictionary<gameobject, Linkedlistnode<gameobject>> (); <summary> add objects into the pool (added by category) </summary> public void Add (string key, Gameobject go) {//1. If key is stored in the container In, the go is added to the corresponding list//2. If key does not exist in the container, create a list first, and then add the IF (! Pool.containskey (key)) {Pool.add (Key, New linkedlist<gameobject> ()); } linkedlistnode<gameobject> t = new linkedlistnode<gameobject> (go); Pool[key]. AddLast (t); Objectdic[go] = t; }///<summary> Destroy objects (hide objects) </summary> public voiD mydestory (String key, Gameobject Destorygo) {//Set Destorygo hide Objectdic[destorygo]. Value.setactive (FALSE); linkedlistnode<gameobject> temp = Objectdic[destorygo]; Pool[key]. Remove (temp); Pool[key]. AddFirst (temp); }///<summary> objects are grouped into pools <summary> public void mydestory (string key, Gameobject tempgo, float delay) { Open a co-startcoroutine (Delaydestory (key, TEMPGO, delay)); }///<summary> delayed destruction </summary> private IEnumerator delaydestory (string key, Gameobject Destorygo, float D Elay) {//wait for a delay time yield return new waitforseconds (delay); Mydestory (key, Destorygo); }///<summary> Create a game object to scene </summary> public gameobject CreateObject (string key, Gameobject go, Vector3 Position, quaternion quaternion) {//Find out if there are any available, if not created, if there is a position set after finding, face back gameobject tempgo = Pool[key]. First.value; if (tempgo.activeself = = False) { TempGo.transform.position = position; TempGo.transform.rotation = quaternion; Tempgo.setactive (TRUE); Pool[key]. Removefirst (); Pool[key]. AddLast (Objectdic[tempgo]); } else {tempgo = Gameobject.instantiate (go, Position, quaternion) as Gameobject; ADD (key, TEMPGO); } return TEMPGO; }///<summary> empty a certain type of game object </summary> public void Clear (string key) {var t = Pool[key]. First; while (T.next!=null) {objectdic.remove (t.value); } pool.remove (key); }///<summary> empties all game objects in the pool </summary> public void ClearAll () {objectdic.clear (); Pool.clear (); }}
Object pools in the Gameobjectpool--unity