美文网首页
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 类对象池资源池对象池

    类对象池 包含创建对象池,取对象池中的内容,回收。 对象管理类 因为使用加载AB包的时候可能会频繁创建类,但是ne...

  • Unity 学习笔记

    Unity 对象池实现方案:

  • Unity对象池

  • unity对象池

    在网上看见了对象池的一些应用,一直很好奇,所以来研究一下 对象池主要的用途就是在那些需要重复被创建和销毁的物体上可...

  • Unity——对象池

    前言 实现对象池的步骤 代码 该脚本可以添加到创建的空物体身上,注意游戏场景中必须要有带有碰撞器的对象存在,不然,...

  • [Unity]对象池

    对象池的作用 避免一直重复的创建和销毁某个对象,增加消耗 理解 就是在开辟一个地方,去储存会大量实例化和销毁的对象...

  • Unity 对象池

    最近在学习 Unity 官方的 《Tower Defense Template》[https://unity3d....

  • Apache Commons-pool2要点整理

    为什么要用对象池 解决大对象的创建和销毁时的资源消耗。所以,常见的对象池有数据库连接池、线程池等 Apache C...

  • Unity-对象池

    简介 解决某些对象频繁创建或销毁造成的时间资源消耗 实例 参考 https://www.jianshu.com/p...

  • Unity对象池封装

    一共分为两个类,SubPool与ObjectPool,SubPool类为总对象池包含ObjectPool的子池子,...

网友评论

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

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