簡(jiǎn)單對(duì)象池的實(shí)現(xiàn)(多種GameObject)

之前做的一個(gè)項(xiàng)目,需要很頻繁的生成多種不同模型,所以我就把之前單一對(duì)象池修改了下,可以使用多種GameObject~~。 廢話不多說(shuō),上代碼?

ObjectPool類

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class ObjectPool

{

private ListdespawnedTrans;

public AnimalTransformInfo origianTran;

public Transform prefab;

public Transform root;

public int curTranCount = 0;

public int TranCount

{

get

{

return curTranCount;

}

}

public ObjectPool(Transform _prefab, Transform _root)? ??

{? ? ? ?

?despawnedTrans = new List();

origianTran = new AnimalTransformInfo();

prefab = _prefab;

root = _root;

prefab.SetParent(root);

prefab.gameObject.SetActive(false);

}

public int DespawnedCout

{

get

{

return despawnedTrans.Count;

}

}

////// 從池中取出一個(gè)對(duì)象

//////

public Transform Spwan()? ? {? ? ? ?

?if(despawnedTrans.Count == 0)? ? ? ? {? ? ? ? ? ?

?CreateInstance();? ? ? ? }? ? ? ??

Transform tran = despawnedTrans[0];? ? ? ??

origianTran.initPos = tran.position;? ? ? ??

origianTran.initRoate = tran.rotation;? ? ? ??

origianTran.initScale = tran.localScale;? ? ? ??

despawnedTrans.RemoveAt(0);? ? ? ??

tran.gameObject.SetActive(true);? ? ? ??

return tran;? ??

}

public void DeSpawn(Transform tran, bool parentChange = true)

{

if(despawnedTrans.Contains(tran))

{

return;

}

if(parentChange)

{

tran.SetParent(root);

}

tran.position = origianTran.initPos;

tran.rotation = origianTran.initRoate;

tran.localScale = origianTran.initScale;

despawnedTrans.Add(tran);

tran.gameObject.SetActive(false);

}

///

/// 創(chuàng)建新的一個(gè)對(duì)象

///

public void CreateInstance()

{

GameObject obj = GameObject.Instantiate(prefab.gameObject);

obj.transform.SetParent(root);

despawnedTrans.Add(obj.transform);

obj.name = prefab.name;

// obj.name = string.Concat(prefab.name, curTranCount++);? 修改名字

}

///

/// 在池中一次創(chuàng)建多個(gè)新的對(duì)象

/////

public IEnumerator CreateInstances(int count)

{

for(int i =0;i < count;i++)

{

CreateInstance();

yield return null;

}

}

///

/// 釋放池

///

public void Dispose()

{

curTranCount = 0;

despawnedTrans.ForEach(delegate (Transform tran)

{

if(tran != null)

{

GameObject.Destroy(tran.gameObject);

}

});

if(prefab != null)

{

GameObject.Destroy(prefab.gameObject);

}

despawnedTrans = null;

}

}

接下來(lái)是創(chuàng)建不同的池,使用ItemCreator類

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class ItemCreator {? ??

public DictionarybasePools;? ??

private Transform poolRoot;? ? ??

?public ItemCreator()? ? {? ? ??

? basePools = new Dictionary();

poolRoot = new GameObject("PoolRoot").transform;

MonoBehaviour.DontDestroyOnLoad(poolRoot.gameObject);

}

////

// 創(chuàng)建一個(gè)對(duì)象池

//////

public void AddPool(GameObject prefabObj)

{

AddPool(prefabObj.name, prefabObj, poolRoot);

}

//創(chuàng)建一個(gè)池對(duì)象并添加到Dic中

public void AddPool(string prefabName ,GameObject prefabObj ,Transform root)

{

if(basePools.ContainsKey(prefabName))

{

return;

}

ObjectPool prefabPool = new ObjectPool(prefabObj.transform, root);

basePools.Add(prefabName, prefabPool);

}

//從一個(gè)池中取出一個(gè)對(duì)象

public Transform SpawnObjByName(string name )? ? {? ? ??

? if(!basePools.ContainsKey(name))? ? ? ? {? ? ? ? ? ? ? ? ? ? ?

?GameObject newPoolObj = MonoBehaviour.Instantiate(Resources.Load("model/"+name));

newPoolObj.name = name;

AddPool(newPoolObj);

return basePools[name].Spwan();

}

return basePools[name].Spwan();

}

//將對(duì)象還給池

public void DeSpawn(string nam, Transform tran, bool parentChange =true)

{

if(!basePools.ContainsKey(nam))

{

return;

}

basePools[nam].DeSpawn(tran,parentChange);

}

到此為止,生成的時(shí)候調(diào)用這個(gè)管理類來(lái)生成就行了,釋放也是一樣的~~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 本節(jié)通過(guò)一個(gè)簡(jiǎn)單的射擊子彈的示例來(lái)介紹Transform的用法。子彈射擊本身很容易制作,只要制作一個(gè)子彈Prefa...
    OneMore2018閱讀 644評(píng)論 0 2
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,545評(píng)論 19 139
  • About these tips(Edit: August 2016. I have revised these ...
    Francis_Rose閱讀 627評(píng)論 0 0
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語(yǔ)法,類相關(guān)的語(yǔ)法,內(nèi)部類的語(yǔ)法,繼承相關(guān)的語(yǔ)法,異常的語(yǔ)法,線程的語(yǔ)...
    子非魚(yú)_t_閱讀 34,653評(píng)論 18 399
  • 看到大家都在走路,今天(6月8曰)下班后決定也走回去。 既然走路,那就找風(fēng)景好的路徑。 沿著龍子湖邊走應(yīng)該是不錯(cuò)的...
    禹王山人閱讀 388評(píng)論 1 2

友情鏈接更多精彩內(nèi)容