Unity 类对象池资源池对象池

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
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android性能优化(高级) 简述Android的系统架构?android的系统架构从下往上分为Linux内核层...
    梧桐树biu阅读 573评论 0 2
  • 我是黑夜里大雨纷飞的人啊 1 “又到一年六月,有人笑有人哭,有人欢乐有人忧愁,有人惊喜有人失落,有的觉得收获满满有...
    陌忘宇阅读 8,593评论 28 53
  • 信任包括信任自己和信任他人 很多时候,很多事情,失败、遗憾、错过,源于不自信,不信任他人 觉得自己做不成,别人做不...
    吴氵晃阅读 6,224评论 4 8
  • 步骤:发微博01-导航栏内容 -> 发微博02-自定义TextView -> 发微博03-完善TextView和...
    dibadalu阅读 3,167评论 1 3
  • 回这一趟老家,心里多了两个疙瘩。第一是堂姐现在谈了一个有妇之夫,在她的语言中感觉,她不打算跟他有太长远的计划,这让...
    安九阅读 3,526评论 2 4