美文网首页
Unity 类对象池资源池对象池

Unity 类对象池资源池对象池

作者: ___________6a1d | 来源:发表于2021-06-18 08:06 被阅读0次
    image.png

    类对象池

    包含创建对象池,取对象池中的内容,回收。

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
     
    /// <summary>
    /// 类对象池
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ClassObjectPool<T> where T : class, new()
    {
        /// <summary>
        /// 偏底层的东西,尽量别使用list
        /// </summary>
        protected Stack<T> m_Pool = new Stack<T>();
        /// <summary>
        /// 最大对象个数 小于等于0 表示不限个数
        /// </summary>
        protected int m_MaxCount = 0;
        /// <summary>
        /// 未回收的对象个数
        /// </summary>
        protected int m_NoRecycleCount = 0;
     
        public ClassObjectPool(int maxCount)
        {
            m_MaxCount = maxCount;
            for (int i = 0; i < maxCount; i++)
            {
                m_Pool.Push(new T());
            }
        }
        /// <summary>
        /// 从池子里去对象
        /// </summary>
        /// <param name="createPoolEmpty">如果为空是否需要new对象</param>
        /// <returns></returns>
        public T Spawn(bool createPoolEmpty)
        {
            if (m_Pool.Count>0)
            {
                T rtn = m_Pool.Pop();
                if (rtn==null)
                {
                    if (createPoolEmpty)
                    {
                        rtn = new T();
                    }
                }
                m_NoRecycleCount++;
                return rtn;
            }
            else
            {
                if (createPoolEmpty)
                {
                    T rtn = new T();
                    m_NoRecycleCount ++;
                    return rtn;
                }
            }
            return null;
        }
        /// <summary>
        /// 回收类对象
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool Recycle(T obj)
        {
            if (obj==null)
            {
                return false;
            }
            m_NoRecycleCount--;
            //不在池子里的情况下  就是个数小于0的情况下,new出来的
            if (m_Pool.Count>=m_MaxCount&&m_MaxCount>=0)
            {
                obj = null;
                return false;
            }
            m_Pool.Push(obj);
            return true;
        }
    }
    

    对象管理类

    因为使用加载AB包的时候可能会频繁创建类,但是new类的时候会产生一定的GC,这会有一定的卡顿,因此提前缓存一些基础类的对象,在其他管理类要使用时从类对象池加载就行了。但是注意类对象池没有提供还原的方法,要自行还原,比如一个类有四个对象,还给类对象池的时候需要自行清空还原成默认值,一般在管理类中做。
    单例类

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    /// <summary>
    /// 单例类
    /// </summary>
    /// <typeparam name="T"></typeparam>
     
    public class Singleton<T> where T:new()
    {
        private static T m_instance;
        public static T Instance
        {
            get
            {
                if (m_instance==null)
                {
                    m_instance = new T();
                }
                return m_instance;
            }
        }
    }
    
    
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    /// <summary>
    /// 对象管理类
    /// </summary>
    public class ObjectManager : Singleton<ObjectManager>
    {
        #region 测试
        //public ClassObjectPool<TestLoad> testLoadPool = ObjectManager.Instance. GetOrCreateClassPool< TestLoad>(1000);
        //void Test()
        //{
        //   TestLoad temp= testLoadPool.Spawn(true);
        //    testLoadPool.Recycle(temp);
        //}
        #endregion
     
        #region 类对象池的使用
        //类对象字典
        protected Dictionary<Type, object> m_ClassPoolDict = new Dictionary<Type, object>();
     
        /// <summary>
        /// 创建类对象池,创建完以后可以在外边保存ClassObjectPool<T>,然后可以调用Spwan和Recycle来创建或回收对象。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="maxCount"></param>
        /// <returns></returns>
        public ClassObjectPool<T> GetOrCreateClassPool<T> (int maxCount) where T :class,new()
        {
            Type type = typeof(T);
            object outObj = null;
            if (!m_ClassPoolDict.TryGetValue(type,out outObj)||outObj==null)
            {
                ClassObjectPool<T> newPool = new ClassObjectPool<T>(maxCount);
                m_ClassPoolDict.Add(type, newPool);
                return newPool;
            }
            return outObj as ClassObjectPool<T>;
        }
        /// <summary>
        /// 从对象池中取对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="maxCount"></param>
        /// <returns></returns>
        public T NewClassObjectFromPool<T>(int maxCount) where T : class, new()
        {
            ClassObjectPool<T> pool= GetOrCreateClassPool<T>(maxCount);
            if (pool==null)
            {
                return null;
            }
            return pool.Spawn(true);
        }
        #endregion
    }
    
    

    相关文章

      网友评论

          本文标题:Unity 类对象池资源池对象池

          本文链接:https://www.haomeiwen.com/subject/xpmtyltx.html