Unity对象池

本文介绍了一种在Unity中实现对象池的技术方案,包括不可销毁的父节点管理、对象的生命周期控制、对象的实例化与回收等核心功能,并提供了详细的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文章目录

需求

  1. 需要一个不可销毁的对象池容器节点,作为对象池里的GameObject的父节点。
  2. 对象池对象需要挂载自定义的MonoBehaviour,目的是增加对象池的生命周期,从对象池取出与被对象池回收的生命周期。
  3. 从对象池取出一个对象,如果没有,直接创建它。
  4. 将一个对象放入对象池。

代码

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

public class GameObjectManager :MonoBehaviour
{
    static Vector3 s_OutOfRange = new Vector3(9000, 9000, 9000);

    private static Transform s_poolParent;
    public static Transform PoolParent
    {
        get
        {
            if (s_poolParent == null)
            {
                GameObject instancePool = new GameObject("ObjectPool");
                s_poolParent = instancePool.transform;
                if(Application.isPlaying)
                DontDestroyOnLoad(s_poolParent);
            }

            return s_poolParent;
        }
    }

    #region 新版本对象池

    static Dictionary<string, List<PoolObject>> s_objectPool_new = new Dictionary<string, List<PoolObject>>();

    /// <summary>
    /// 加载一个对象并把它实例化
    /// </summary>
    /// <param name="gameObjectName">对象名</param>
    /// <param name="parent">对象的父节点,可空</param>
    /// <returns></returns>
    static PoolObject CreatePoolObject(string gameObjectName, GameObject parent = null)
    {
        GameObject go = ResourceManager.Load<GameObject>(gameObjectName);

        if (go == null)
        {
            throw new Exception("CreatPoolObject error dont find : ->" + gameObjectName + "<-");
        }

        GameObject instanceTmp = Instantiate(go);
        instanceTmp.name = go.name;

        PoolObject po = instanceTmp.GetComponent<PoolObject>();

        if (po == null)
        {
            throw new Exception("CreatPoolObject error : ->" + gameObjectName + "<- not is PoolObject !");
        }

        po.OnCreate();

        if (parent != null)
        {
            instanceTmp.transform.SetParent(parent.transform);
        }

        instanceTmp.SetActive(true);

        return po;
    }

    /// <summary>
    /// 把一个对象放入对象池
    /// </summary>
    /// <param name="gameObjectName"></param>
    public static void PutPoolObject(string gameObjectName)
    {
        DestroyPoolObject(CreatePoolObject(gameObjectName));
    }

    /// <summary>
    /// 预存入对象池
    /// </summary>
    /// <param name="name"></param>
    public static void PutPoolGameOject(string name)
    {
        DestroyGameObjectByPool(CreateGameObjectByPool(name));
    }

    public static bool IsExist_New(string objectName)
    {
        if (objectName == null)
        {
            Debug.LogError("IsExist_New error : objectName is null!");
            return false;
        }

        if (s_objectPool_new.ContainsKey(objectName) && s_objectPool_new[objectName].Count > 0)
        {
            return true;
        }

        return false;
    }

    /// <summary>
    /// 从对象池取出一个对象,如果没有,则直接创建它
    /// </summary>
    /// <param name="name">对象名</param>
    /// <param name="parent">要创建到的父节点</param>
    /// <returns>返回这个对象</returns>
    public static PoolObject GetPoolObject(string name, GameObject parent = null)
    {
        PoolObject po;
        if (IsExist_New(name))
        {
            po = s_objectPool_new[name][0];
            s_objectPool_new[name].RemoveAt(0);
            if (po && po.SetActive)
                po.gameObject.SetActive(true);

            if (parent == null)
            {
                po.transform.SetParent(null);
            }
            else
            {
                po.transform.SetParent(parent.transform);
            }
        }
        else
        {
            po = CreatePoolObject(name, parent);
        }

        po.OnFetch();

        return po;
    }

    /// <summary>
    /// 将一个对象放入对象池
    /// </summary>
    /// <param name="obj">目标对象</param>
    public static void DestroyPoolObject(PoolObject obj)
    {
        string key = obj.name.Replace("(Clone)", "");

        if (s_objectPool_new.ContainsKey(key) == false)
        {
            s_objectPool_new.Add(key, new List<PoolObject>());
        }

        if (s_objectPool_new[key].Contains(obj))
        {
            throw new Exception("DestroyPoolObject:-> Repeat Destroy GameObject !" + obj);
        }

        s_objectPool_new[key].Add(obj);

        if (obj.SetActive)
            obj.gameObject.SetActive(false);
        else
            obj.transform.position = s_OutOfRange;

        obj.OnRecycle();

        obj.name = key;
        obj.transform.SetParent(PoolParent);
    }

    public static void DestroyPoolObject(PoolObject go, float time)
    {
        Timer.DelayCallBack(time, (object[] obj) =>
        {
            DestroyPoolObject(go);
        });
    }

    /// <summary>
    /// 清空对象池
    /// </summary>
    public static void CleanPool_New()
    {
        foreach (string name in s_objectPool_new.Keys)
        {
            if (s_objectPool_new.ContainsKey(name))
            {
                List<PoolObject> objList = s_objectPool_new[name];

                for (int i = 0; i < objList.Count; i++)
                {
                    try
                    {
                        objList[i].OnObjectDestroy();
                    }
                    catch (Exception e)
                    {
                        Debug.Log(e.ToString());
                    }

                    Destroy(objList[i].gameObject);
                }

                objList.Clear();
            }
        }

        s_objectPool_new.Clear();
    }

    /// <summary>
    /// 清除掉某一个对象的所有对象池缓存
    /// </summary>
    public static void CleanPoolByName_New(string name)
    {
        if (s_objectPool_new.ContainsKey(name))
        {
            List<PoolObject> objList = s_objectPool_new[name];

            for (int i = 0; i < objList.Count; i++)
            {
                try
                {
                    objList[i].OnObjectDestroy();
                }
                catch(Exception e)
                {
                    Debug.Log(e.ToString());
                }

                Destroy(objList[i].gameObject);
            }

            objList.Clear();
            s_objectPool_new.Remove(name);
        }
    }

    #endregion

    #region 新版本对象池 异步方法

    public static void CreatePoolObjectAsync(string name, CallBack<PoolObject> callback, GameObject parent = null)
    {
        ResourceManager.LoadAsync(name, (status,res) =>
        {
            try
            {
                callback(CreatePoolObject(name, parent));
            }
            catch(Exception e)
            {
                Debug.LogError("CreatePoolObjectAsync Exception: " + e.ToString());
            }
        });
    }

    #endregion
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值