ObjectPool

来源:互联网 发布:淘宝主营类目占比 编辑:程序博客网 时间:2024/06/05 03:04
using System.Collections;using System.Collections.Generic;using UnityEngine;public class ObjectPool :SingletonByMono<ObjectPool> {    private static Dictionary<string, ArrayList> pool = new Dictionary<string, ArrayList> ();    public Object ReturnStorage(GameObject go)    {        string key = go.gameObject.name;        if(pool.ContainsKey (key))        {            pool[key].Add (go);        }        else        {            pool [key] = new ArrayList (){go};        }        go.SetActive (false);        return go;    }    public Object Get(GameObject prefab,Vector3 position,Quaternion quaternion)    {        string key = prefab.name + "(Clone)";        Object o;        if(pool.ContainsKey (key)&&pool[key].Count>0)        {            ArrayList list = pool [key];            o = list [0] as Object;            list.RemoveAt(0);            (o as GameObject).SetActive (true);            (o as GameObject).transform.position = position;            (o as GameObject).transform.rotation = quaternion;        }        else        {            o = Instantiate (prefab, position, quaternion);        }        return o;    }    public void WaitToReturn(GameObject go,float waitTime)    {        StartCoroutine (ReturnToPool(go,waitTime));    }    IEnumerator ReturnToPool(GameObject go ,float  waitTime)    {        yield return new WaitForSeconds (waitTime);        ReturnStorage (go);    }}
using System.Collections;using System.Collections.Generic;using UnityEngine;public class ObjectPool :SingletonByMono<ObjectPool> {private static Dictionary<string, ArrayList> pool = new Dictionary<string, ArrayList> ();public Object ReturnStorage(GameObject go){string key = go.gameObject.name;if(pool.ContainsKey (key)){pool[key].Add (go);}else{pool [key] = new ArrayList (){go};}go.SetActive (false);return go;}public Object Get(GameObject prefab,Vector3 position,Quaternion quaternion){string key = prefab.name + "(Clone)";Object o;if(pool.ContainsKey (key)&&pool[key].Count>0){ArrayList list = pool [key];o = list [0] as Object;list.RemoveAt(0);(o as GameObject).SetActive (true);(o as GameObject).transform.position = position;(o as GameObject).transform.rotation = quaternion;}else{o = Instantiate (prefab, position, quaternion);}return o;}public void WaitToReturn(GameObject go,float waitTime){StartCoroutine (ReturnToPool(go,waitTime));}IEnumerator ReturnToPool(GameObject go ,float  waitTime){yield return new WaitForSeconds (waitTime);ReturnStorage (go);}}

using System.Collections;using System.Collections.Generic;using UnityEngine;public class ObjectPool :SingletonByMono<ObjectPool> {private static Dictionary<string, ArrayList> pool = new Dictionary<string, ArrayList> ();public Object ReturnStorage(GameObject go){string key = go.gameObject.name;if(pool.ContainsKey (key)){pool[key].Add (go);}else{pool [key] = new ArrayList (){go};}go.SetActive (false);return go;}public Object Get(Vector3 position, Quaternion quaternion,GameObject prefab = null,string prefabName=null,string prefabPath=null){string key;if(prefab){key = prefab.name + "(Clone)";}else if(prefabName!=null){key = prefabName + "(Clone)";}else{key = "";}Object o=null;if(pool.ContainsKey (key)&&pool[key].Count>0){ArrayList list = pool [key];o = list [0] as Object;list.RemoveAt(0);(o as GameObject).SetActive (true);(o as GameObject).transform.position = position;(o as GameObject).transform.rotation = quaternion;}else{if(prefab){o = Instantiate (prefab, position, quaternion);}else if(prefabPath!=null){o = Instantiate (Resources.Load (prefabPath), position, quaternion);}}return o;}public void WaitToReturn(GameObject go,float waitTime){StartCoroutine (ReturnToPool(go,waitTime));}IEnumerator ReturnToPool(GameObject go ,float  waitTime){yield return new WaitForSeconds (waitTime);ReturnStorage (go);}}


原创粉丝点击