【简单实用框架】【对象池】【可移植】

文章目录

  • GameObject对象池
    • 方法
      • ObjectPoolManager
        • CreateGameObjectPool
        • DestroyGameObjectPool
        • GetGameObject
        • RecycleGameObject
      • GameObjectPool
        • ClearAll
        • GetGameObject
        • RecycleGameObject
        • ClearRedundantObjects
        • CreateGameObjecet
    • 使用方法
    • 全部代码
      • GameObjectPool
      • ObjectPoolManager
      • IManager
      • IModel
      • 移植使用注意
  • 更新记录
  • 我的Git

GameObject对象池

  • IManager
  • IModel
  • GameObjectPool
  • ObjectPoolManager

方法

ObjectPoolManager

CreateGameObjectPool

创建对象池

DestroyGameObjectPool

删除对象池

GetGameObject

获得某个对象池里的对象

RecycleGameObject

回收对象

GameObjectPool

ClearAll

清除所有信息

GetGameObject

获得对象

RecycleGameObject

回收对象

ClearRedundantObjects

清除多余对象

CreateGameObjecet

创建对象

使用方法

//创建对象池(默认大小5)
ObjectPoolManager.Instance.CreateGameObjectPool("Basketball", "Basketball",20);
//获得某个对象池中对象
GameObject obj = ObjectPoolManager.Instance.GetGameObject("Basketball");
//回收对象
ObjectPoolManager.Instance.RecycleGameObject("Basketball",gameObject);
//删除对象池
ObjectPoolManager.Instance.DestroyGameObjectPool("Basketball");

全部代码

GameObjectPool

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

public class GameObjectPool : IModel
{
    private int m_PoolSize;
    private string m_ResName;

    private GameObject m_OriginalRes;
    private GameObject m_GameObjectPoolParent;

    private Queue m_NotUseGameObjectPool;

    private bool m_SuccessFlag;
    public bool SuccessFlag { get { return m_SuccessFlag; } }
    public GameObjectPool(string name, string resName, GameObject objectPool, int poolSize)
    {
        m_SuccessFlag = true;
        m_NotUseGameObjectPool = new Queue(poolSize);

        m_PoolSize = poolSize;
        m_ResName = resName;

        string poolName = name + "Pool";
        m_GameObjectPoolParent = new GameObject(poolName);
        m_GameObjectPoolParent.transform.parent = objectPool.transform;

        OnInit();
    }

    /// 
    /// 清除所有信息
    /// 
    public void ClearAll()
    {
        while (m_NotUseGameObjectPool.Count > 0) Object.Destroy(m_NotUseGameObjectPool.Dequeue());

        m_NotUseGameObjectPool = null;

        m_PoolSize = 0;
        m_ResName = string.Empty;

        m_OriginalRes = null;

        Object.Destroy(m_GameObjectPoolParent);

        ResourcesManager.Instance.UnloadUnusedAssets();
    }

    /// 
    /// 获得对象
    /// 
    /// 
    public GameObject GetGameObject()
    {
        if (m_NotUseGameObjectPool.Count > 0)
        {
            return m_NotUseGameObjectPool.Dequeue();
        }
        else
        {
            GameObject obj = CreateGameObjecet();
            if (obj != null) return obj;
            else return null;
        }
    }

    /// 
    /// 回收对象
    /// 
    /// 
    public void RecycleGameObject(GameObject obj)
    {
        if (!m_NotUseGameObjectPool.Contains(obj))
        {
            obj.SetActive(false);
            obj.transform.parent = m_GameObjectPoolParent.transform;
            obj.transform.localPosition = Vector3.zero;
            m_NotUseGameObjectPool.Enqueue(obj);
        }
        else
        {
            Debug.LogError($"该对象已经归还过了,对象名{obj.name}");
        }
     
        ClearRedundantObjects();
    }

    /// 
    /// 清除多余对象
    /// 
    private void ClearRedundantObjects()
    {
        while (m_NotUseGameObjectPool.Count > m_PoolSize) Object.Destroy(m_NotUseGameObjectPool.Dequeue());
    }

    /// 
    /// 创建新对象
    /// 
    /// 
    private GameObject CreateGameObjecet()
    {
        GameObject obj = GameObject.Instantiate(m_OriginalRes, m_GameObjectPoolParent.transform);
        if (obj != null)
        {
            obj.SetActive(false);
            obj.transform.localPosition = Vector3.zero;
            return obj;
        }
        else
        {
            Debug.LogError($"实例化对象为空,资源名为{m_ResName}");
            return null;
        }
    }


    public void OnInit()
    {
        m_OriginalRes = ResourcesManager.Instance.Load(m_ResName);
        if (m_OriginalRes != null)
        {
            for (int i = 0; i < m_PoolSize; i++)
            {
                GameObject obj = CreateGameObjecet();
                if (obj != null) m_NotUseGameObjectPool.Enqueue(obj);
                else
                {
                    m_SuccessFlag = false;
                    break;
                }
            }
        }
        else
        {
            Debug.LogError($"资源{m_ResName}未找到");
            ClearAll();
            m_SuccessFlag = false;
        }
    }
}

ObjectPoolManager

using System.Collections.Generic;
using UnityEngine;
using MGPCommons.Class;

public class ObjectPoolManager : SingletonSuperMono, IManager
{
    private GameObject m_ObjectPool;

    private Dictionary m_AllObjectPoll;

    //初始化池
    //

    private void Awake()
    {
        OnInit();
    }

    public void OnInit()
    {
        m_ObjectPool = new GameObject("ObjectPool");
        DontDestroyOnLoad(m_ObjectPool);

        m_AllObjectPoll = new Dictionary();
    }

    /// 
    /// 创建对象池
    /// 
    /// 对象池名字
    /// 资源名
    /// 对象池大小
    public void CreateGameObjectPool(string poolName, string resName,int poolSize = 5)
    {
        if (!m_AllObjectPoll.ContainsKey(poolName.Trim()))
        {
            GameObjectPool pool = new GameObjectPool(poolName, resName, m_ObjectPool, poolSize);
            if (pool.SuccessFlag) m_AllObjectPoll.Add(poolName, pool);
            else pool = null;
        }
        else
        {
            Debug.LogError($"对象池{poolName}Pool已经存在");
        }
    }

    /// 
    /// 删除对象池
    /// 
    /// 
    public void DestroyGameObjectPool(string poolName)
    {
        if (m_AllObjectPoll.ContainsKey(poolName.Trim()))
        {
            m_AllObjectPoll[poolName].ClearAll();
            m_AllObjectPoll.Remove(poolName);
        }
        else
        {
            Debug.LogError($"对象池{poolName}Pool不存在");
        }
    }

    /// 
    /// 获取对象
    /// 
    /// 
    /// 
    public GameObject GetGameObject(string poolName)
    {
        if (m_AllObjectPoll.ContainsKey(poolName.Trim()))
        {
            return m_AllObjectPoll[poolName].GetGameObject();
        }
        else
        {
            Debug.LogError($"对象池{poolName}Pool不存在");
            return null;
        }
    }

    /// 
    /// 回收对象
    /// 
    /// 
    /// 
    public void RecycleGameObject(string poolName,GameObject obj)
    {
        if (m_AllObjectPoll.ContainsKey(poolName.Trim()))
        {
             m_AllObjectPoll[poolName].RecycleGameObject(obj);
        }
        else
        {
            Debug.LogError($"对象池{poolName}Pool不存在");
        }
    }
}

IManager

public interface IManager
{
    public void OnInit();
}

IModel

public interface IModel 
{
    public void OnInit();
}

移植使用注意

  1. ObjectPoolManager继承的单例类替换
  2. GameObjectPool.OnInitResourcesManager.Instance.Load(ResName)替换
  3. GameObjectPool.ClearAllResourcesManager.Instance.UnloadUnusedAssets()卸载资源替换
  4. 单例类和ResourcesManager在其它同专栏内。

更新记录

  • 2023/3/23 第一次更新

我的Git

码云记录
(Github)MFramework中的Unity框架里

你可能感兴趣的:(Unity实用框架,c#,开发语言)