之前做的一个项目,需要很频繁的生成多种不同模型,所以我就把之前单一对象池修改了下,可以使用多种GameObject~~。 废话不多说,上代码
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;
}
}
////// 从池中取出一个对象
//////
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);
}
///
/// 创建新的一个对象
///
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++); 修改名字
}
///
/// 在池中一次创建多个新的对象
/////
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;
}
}
接下来是创建不同的池,使用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);
}
////
// 创建一个对象池
//////
public void AddPool(GameObject prefabObj)
{
AddPool(prefabObj.name, prefabObj, poolRoot);
}
//创建一个池对象并添加到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);
}
//从一个池中取出一个对象
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();
}
//将对象还给池
public void DeSpawn(string nam, Transform tran, bool parentChange =true)
{
if(!basePools.ContainsKey(nam))
{
return;
}
basePools[nam].DeSpawn(tran,parentChange);
}
到此为止,生成的时候调用这个管理类来生成就行了,释放也是一样的~~