Object pools in the Gameobjectpool--unity

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.