using System.Collections;using System.Collections.Generic;using UnityEngine;publicclassObjectPool:SingletonByMono<ObjectPool>{privatestaticDictionary<string,ArrayList>pool=newDictionary<string,ArrayList>();publicObjectReturnStorage(GameObjectgo){stringkey=go.gameObject.name;if(pool.ContainsKey(key)){pool[key].Add(go);}else{pool[key]=newArrayList(){go};}go.SetActive(false);returngo;}publicObjectGet(GameObjectprefab,Vector3position,Quaternionquaternion){stringkey=prefab.name+"(Clone)";Objecto;if(pool.ContainsKey(key)&&pool[key].Count>0){ArrayListlist=pool[key];o=list[0]asObject;list.RemoveAt(0);(oasGameObject).SetActive(true);(oasGameObject).transform.position=position;(oasGameObject).transform.rotation=quaternion;}else{o=Instantiate(prefab,position,quaternion);}returno;}publicvoidWaitToReturn(GameObjectgo,floatwaitTime){StartCoroutine(ReturnToPool(go,waitTime));}IEnumeratorReturnToPool(GameObjectgo,floatwaitTime){yieldreturnnewWaitForSeconds(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);
}
}
using System.Collections;using System.Collections.Generic;using UnityEngine;public class ObjectPool :SingletonByMono { private static Dictionary pool = new Dictionary (); public Object ReturnSto