【Unity】对象池管理模式简单应用

对象池

提示:个人学习总结,如有错误,敬请指正。


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>();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值