/// <summary> /// 创建Pool并设置Pool的预制体(安全的,重复的key不添加,poolName不写为Pool_+prefab.name) /// </summary> public Pool CreatPool(GameObject prefab, bool isDontDestroyOnLoad = false, string poolName = null, int maxCapacity = -1) { if (poolName == null) { poolName = string.Format("Pool_{0}", prefab.name); } if (dicPrefab.ContainsKey(poolName)) { return(dicPool.TryGet(poolName)); } //设置Pool的预制体 dicPrefab.Add(poolName, prefab); PoolObject poolObject = prefab.GetComponent <PoolObject>(); bool isInheritPoolObject = poolObject ? true : false; //创建Pool GameObject poolGo = new GameObject(poolName); Pool pool = poolGo.AddComponent <Pool>(); pool.SetPoolName(poolName).SetPoolPrefab(prefab).SetDontDestroyOnLoad(isDontDestroyOnLoad).SetInheritPoolObject(isInheritPoolObject).SetPoolMaxCapacity(maxCapacity).StartPool(); dicPool.Add(poolName, pool); //设置PoolObject if (isInheritPoolObject) { poolObject.poolName = poolName; poolObject.pool = pool; //Debug.Log("poolName" + poolObject.poolName + poolObject.pool.ToString()); dicTypePoolName.Add(poolObject.GetType(), poolName);//经测试,这里存储的poolObject.GetType()为子类的Type } return(pool); }
private void CreateNewInstance(PoolObject obj) { PoolObject newObj = Instantiate(obj) as PoolObject; newObj.gameObject.SetActive(false); newObj.transform.parent = this.transform; newObj.name = obj.name; poolObjects[obj.GetType()].Push(newObj); }
public static void DeSpawn(GameObject go) { //despawn object and push it back into the dictionary stack PoolObject poolObj = go.GetComponent <PoolObject>(); poolObj.OnDespawn(); //PoolObject OnDestroy version poolObj.transform.SetParent(thisTransform); //Get it back to the pool go.SetActive(false); //turn it off to stop using resources Type type = poolObj.GetType(); poolObjects[type].Push(poolObj); //Push it back into the stack }
private static void PreLoad(PoolObject obj) { //load specific PoolObject and push it into its dictionary stack Type type = obj.GetType(); poolObjects.Add(type, new Stack <PoolObject>()); //set up a new stack for the current type for (int i = 0; i < obj.nPreloads; i++) { PoolObject newObj = Instantiate(obj) as PoolObject; newObj.name = obj.name; //remove (Clone) newObj.gameObject.SetActive(false); //make sure objects are not consuming resources newObj.transform.SetParent(thisTransform); poolObjects[type].Push(newObj); //push into the type stack } }
public void DeSpawn(PoolObject obj) { obj.gameObject.SetActive(false); obj.gameObject.transform.parent = this.transform; poolObjects[obj.GetType()].Push(obj); }