对象池
提示:个人学习总结,如有错误,敬请指正。
ObjectPoolManager.cs
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
//池管理器
public class ObjectPoolManager : MonoBehaviour
{
//设置为静态类,便于在静态方法中调用
public static List<PooledObjectInfo> ObjectPools = new List<PooledObjectInfo>();
public GameObject _objectPoolEmptyHolder;
private static GameObject _particleSystemsEmpty;
private static GameObject _GameObjectSystemsEmpty;
/// <summary>
/// 对象池类型枚举
/// </summary>
public enum PoolType
{
ParticleSystem,
GameobjectGO,
None,
}
public static PoolType PoolingType;
private void Awake()
{
SetupEmpties();
}
/// <summary>
/// 放进对象里
/// </summary>
private void SetupEmpties()
{
_objectPoolEmptyHolder = new GameObject("Pooled Objects");
_particleSystemsEmpty = new GameObject("Particles Effects");
_particleSystemsEmpty.transform.SetParent(_objectPoolEmptyHolder.transform);
_GameObjectSystemsEmpty = new GameObject("Gameobjects");
_GameObjectSystemsEmpty.transform.SetParent(_GameObjectSystemsEmpty.transform);
}
/// <summary>
/// 生成对象方法
/// </summary>
/// <param name="objectToSpawn"></param>
/// <param name="partentTransform"></param>
/// <returns></returns>
public static GameObject SpawnObject(GameObject objectToSpawn, Transform partentTransform)
{
//搜索所有的对象池
PooledObjectInfo pool = ObjectPools.Find(p => p.LookupString == objectToSpawn.name);
//如查询不到
if (pool == null)
{
pool = new PooledObjectInfo() { LookupString = objectToSpawn.name };
ObjectPools.Add(pool);
}
//检查
GameObject spawnableObj = pool.InactiveObjects.FirstOrDefault(obj => obj != null);
if (spawnableObj == null)
{
spawnableObj = Instantiate(objectToSpawn, partentTransform);
}
else
{
pool.InactiveObjects.Remove(spawnableObj);
spawnableObj.SetActive(true);
}
return spawnableObj;
}
/// <summary>
/// 生成对象方法
/// </summary>
/// <param name="objectToSpawn"></param>
/// <param name="spawnPisition"></param>
/// <param name="spawnRotation"></param>
/// <param name="poolType"></param>
/// <returns></returns>
public static GameObject SpawnObject(GameObject objectToSpawn, Vector3 spawnPisition, Quaternion spawnRotation,
PoolType poolType = PoolType.None)
{
//搜索所有的对象池
PooledObjectInfo pool = ObjectPools.Find(p => p.LookupString == objectToSpawn.name);
//如查询不到
if (pool == null)
{
pool = new PooledObjectInfo() { LookupString = objectToSpawn.name };
ObjectPools.Add(pool);
}
//检查
GameObject spawnableObj = pool.InactiveObjects.FirstOrDefault(obj => obj != null);
if (spawnableObj == null)
{
GameObject partentObject = SetParentObject(poolType);
spawnableObj = Instantiate(objectToSpawn, spawnPisition, spawnRotation);
if (spawnableObj != null)
{
spawnableObj.transform.SetParent(partentObject.transform);
}
}
else
{
spawnableObj.transform.position = spawnPisition;
spawnableObj.transform.rotation = spawnRotation;
pool.InactiveObjects.Remove(spawnableObj);
spawnableObj.SetActive(true);
}
return spawnableObj;
}
/// <summary>
/// 回收对象
/// </summary>
/// <param name="obj"></param>
public static void RetrunObjectToPool(GameObject obj)
{
//去除“ (Clone) ”
string goName = obj.name.Substring(0, obj.name.Length - 7);
PooledObjectInfo pool = ObjectPools.Find(p => p.LookupString == goName);
if (pool == null)
{
Debug.LogWarning("Trying to release an object is not poolde: " + obj.name);
}
else
{
obj.SetActive(false);
pool.InactiveObjects.Add(obj);
}
}
/// <summary>
/// 获取父物体,以便于设置层级
/// </summary>
/// <param name="poolType"></param>
/// <returns></returns>
public static GameObject SetParentObject(PoolType poolType)
{
switch (poolType)
{
case PoolType.ParticleSystem:
return _particleSystemsEmpty;
break;
case PoolType.GameobjectGO:
return _GameObjectSystemsEmpty;
break;
case PoolType.None:
return null;
break;
default:
return null;
}
}
}
//可以看做一个对象池
public class PooledObjectInfo
{
//查找字符串
public string LookupString;
//池子
public List<GameObject> InactiveObjects = new List<GameObject>();
}