对象池

  先讲一下我对对象池的理解吧,说白了就是两个集合和一个要存储的对象,一个集合是存储着激活的物体,一个是存储着未激活的,要拿物体的时候,先去未激活的里面找,如果有就返回,然后从未激活的列表移除,然后在激活列表添加,如果未激活的列表里没有,就实例生成一个对象存到激活的列表里.

      再说一下对象池要实现的功能吧
      第一个就是一个取物体的方法,先查找未激活列表有东西吗(这里用到一个查找的方法),如果没有就创建一个(这里需要一个增加的方法),第二个是放回的方法,放回的时候从激活列表移除,在未激活列表添加,第三个就是清理方法,把两个列表里存储的物体给Destory掉,然后把两个列表清空
废话不多说,上代码

   

 using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
     
    public class ObjectPool
    {
        public List<GameObject> activeObj = new List<GameObject>();     // 激活的
        public List<GameObject> inactiveObj = new List<GameObject>();   // 未激活的
        public GameObject prefab;   // 要储存的预设
        private int objectsNum = 0;
        public int ObjectsNum {
            get {
                return objectsNum;
            }
            protected set {
                objectsNum = value;
            }
        }
        public ObjectPool(GameObject prefab)
        {
            if (prefab != null)
            {
                objectsNum = 0;
                this.prefab = prefab;
            }
        }
        public GameObject Creat()
        {
            if (prefab != null)
            {
                GameObject newObj = Object.Instantiate(prefab, Vector3.zero, Quaternion.identity);
                if (newObj != null)
                {
                    Object.DontDestroyOnLoad(newObj);
                    objectsNum++;
                    return newObj;
                }
            }
            return null;
        }
        public GameObject TakeObj()
        {
            if (prefab != null)
            {
                GameObject newObj;
                if (inactiveObj.Count > 0)
                {
                    newObj = inactiveObj[0];
                    inactiveObj.RemoveAt(0);
                }
                else
                {
                    newObj = Creat();
                }
                if (newObj != null)
                {
                    activeObj.Add(newObj);
                    SetActive(newObj, true);
                }
                return newObj;
            }
            return null;
        }
        public void Restor(GameObject obj, Transform savePanel, bool destory = false)
        {
            if (obj != null)
            {
                if (activeObj.Contains(obj))
                {
                    activeObj.Remove(obj);
     
                    if (destory)
                    {
                        Destory(obj);
                    }
                    else
                    {
                        obj.transform.SetParent(savePanel);
                        SetActive(obj, false);
                        inactiveObj.Add(obj);
                    }
                }
            }
        }
        private void Destory(GameObject go)
        {
            if (go != null)
            {
                Object.Destroy(go);
                objectsNum--;
                if (objectsNum < 0)
                {
                    objectsNum = 0;
                }
            }
        }
        private void SetActive(GameObject go, bool active)
        {
            if (go != null && go.activeSelf != active)
            {
                go.SetActive(active);
            }
        }
        public void Release()
        {
            if (inactiveObj.Count > 0)
            {
                for (int i = 0; i < inactiveObj.Count; i++)
                {
                    Destory(inactiveObj[i]);
                }
            }
            if (activeObj.Count > 0)
            {
                for (int i = 0; i < activeObj.Count; i++)
                {
                    Destory(activeObj[i]);
                }
            }
            inactiveObj.Clear();
            activeObj.Clear();
            objectsNum = 0;
            prefab = null;
        }
    }

当然了,对象池也要有个管理器,管理着所有的对象池,管理器有一个键值对,存储着所有的对象池,key是存储的对象,value是存储对象的对象池,对象池的管理者有得到对象的接口(同上如果没有就出创建),有个放回对象的接口,还有个释放(清理)所有的接口
代码如下

using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
     
    public class ObjectPoolMgr : MonoBehaviour
    {
        public static ObjectPoolMgr Instance {get;protected set;}
        protected Dictionary<GameObject, ObjectPool> pools = new Dictionary<GameObject, ObjectPool>();
        private void Awake()
        {
            if (Instance == null)
            {
                Instance = this;
                DontDestroyOnLoad(gameObject);
            }
        }
        /// <summary>
        /// 从对象池取值(如果没有对象池就创建一个)
        /// </summary>
        /// <param name="prefab">对象池集合的key</param>
        /// <returns></returns>
        public GameObject GetObj(GameObject prefab)
        {
            if (prefab != null)
            {
                ObjectPool pool = GetPool(prefab);
                if (pool == null)
                {
                    pool = Creat(prefab);
                }
                if (pool != null)
                {
                    return pool.TakeObj();
                }
            }
            return null;
        }
        private ObjectPool GetPool(GameObject prafab)
        {
            if (prafab != null)
            {
                ObjectPool pool;
                return pools.TryGetValue(prafab, out pool) ? pool : null;
            }
            return null;
        }
        private ObjectPool Creat(GameObject prefab)
        {
            if (prefab != null)
            {
                ObjectPool pool = new ObjectPool(prefab);
                pools.Add(prefab, pool);
                return pool;
            }
            return null;
        }
        /// <summary>
        /// 放回对象池或者销毁
        /// </summary>
        /// <param name="go">要放回对象池或者销毁的对象</param>
        /// <param name="destory"></param>
        public void Restor(GameObject go, bool destory = false)
        {
            if (pools.Count > 0)
            {
                var pool = pools.GetEnumerator();
                while (pool.MoveNext())
                {
                    Restor(pool.Current.Key, go, destory);
                }
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="prefab">对象池集合的Key</param>
        /// <param name="go">对象池的key</param>
        /// <param name="destory"></param>
        private void Restor(GameObject prefab, GameObject go, bool destory)
        {
            ObjectPool pool = GetPool(prefab);
            if (pool != null)
            {
                pool.Restor(go, transform, destory);
            }
        }
        public void Release()
        {
            if (pools.Count > 0)
            {
                List<GameObject> keys = new List<GameObject>();
                keys.AddRange(pools.Keys);
                for (int i = 0; i < keys.Count; i++)
                {
                    ReleasePool(keys[i]);
                }
            }
            pools.Clear();
        }
        private void ReleasePool(GameObject prefab)
        {
            ObjectPool pool = GetPool(prefab);
            if (pool != null)
            {
                pool.Release();
            }
        }
    }

   
原文链接:https://blog.csdn.net/marccco/article/details/88357118

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TO_ZRG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值