系统模块:简易对象池-加载-回收

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

namespace Miss
{
    /// <summary>
    /// 对象池管理
    /// </summary>
    public class ObjectPoolManager : SingletonMono<ObjectPoolManager>
    {   
        //键为string类型,资源路径;值为List实体对象类
        private Dictionary<string, List<ObjectPoolEntityBase>> pool;

        private GameObject m_RootPool;
        private GameObject RootPool
        {
            get
            {
                if (null == m_RootPool)
                {
                    m_RootPool = new GameObject("RoolPool");
                }
                return m_RootPool;
            }
        }

        private void Awake()
        {
            pool = new Dictionary<string, List<ObjectPoolEntityBase>>();
        }

        //直接加载Resources下的预制
        public T LoadEntityByResources<T>(string resPath) where T : ObjectPoolEntityBase
        {
            List<ObjectPoolEntityBase> entityList = null;
            ObjectPoolEntityBase entity = null;
            //从池子中取
            if (pool.TryGetValue(resPath, out entityList))
            {
                if (entityList.Count > 0)
                {
                    entity = entityList[0];
                    entityList.RemoveAt(0);
                    entity.gameObject.SetActive(true);
                    Debug.Log($"{resPath}  出池");
                    return entity as T;
                }
            }
            //池中没有,加载生成
            GameObject go = Resources.Load(resPath) as GameObject;
            if (go == null)
            {
                Debug.LogError($"go 为空  路径:{resPath}");
                return null;
            }
            go = GameObject.Instantiate(go, RootPool.transform);
            go.SetActive(true);
            if (!go.TryGetComponent(out entity))
            {
                entity = go.AddComponent<T>();
                entity.Init(resPath);
                Debug.Log($"{resPath}  生成");
            }
            return entity as T;
        }

        #region 异步出池
        public void LoadEntityAsyncByResources<T>(string resPath, Action<ObjectPoolEntityBase> finishCallBack) where T : ObjectPoolEntityBase
        {
            ObjectPoolEntityBase entity = null;
            if (pool.ContainsKey(resPath) && pool[resPath].Count > 0)
            {
                entity = pool[resPath][0];
                entity.gameObject.SetActive(true);
                pool[resPath].RemoveAt(0);
                Debug.Log("异步出池");
                finishCallBack?.Invoke(entity);
                return;
            }
            StartCoroutine(IE_AsyncLoadPrefab<T>(resPath, finishCallBack));
        }
        //异步生成模型
        private IEnumerator IE_AsyncLoadPrefab<T>(string resPath, Action<ObjectPoolEntityBase> finishCallBack) where T : ObjectPoolEntityBase
        {
            ResourceRequest request = Resources.LoadAsync(resPath, typeof(GameObject));
            yield return request;
            if (request.isDone)
            {
                GameObject go = request.asset as GameObject;
                if (go == null)
                {
                    Debug.LogError("go 为空");
                }
                go = GameObject.Instantiate(go, RootPool.transform);
                go.SetActive(true);
                ObjectPoolEntityBase entity = null;
                if (!go.TryGetComponent(out entity))
                {
                    entity = go.AddComponent<T>();
                    entity.Init(resPath);
                }
                Debug.Log("异步生成");
                finishCallBack?.Invoke(entity);
            }
        }
        #endregion

        #region 回池
        //把实例化出来的物体都存到池子中
        public void PushObjectToPool(ObjectPoolEntityBase entity)
        {
            string resPath = entity.ResPath;
            entity.transform.SetParent(RootPool.transform);
            if (pool.ContainsKey(resPath))
            {
                pool[resPath].Add(entity);
            }
            else
            {
                pool[resPath] = new List<ObjectPoolEntityBase>() { entity };
            }
            Debug.Log($"{resPath}  回收完成");
        }
        #endregion
    }
}
using UnityEngine;

namespace Miss
{
    /// <summary>
    /// 对象实体类基类
    /// </summary>
    public class ObjectPoolEntityBase : MonoBehaviour
    {
        //资源路径
        private string m_ResPath;
        public string ResPath => m_ResPath;


        public void Init(string resPath)
        {
            OnInit(resPath);
        }

        protected virtual void OnInit(string resPath)
        {
            m_ResPath = resPath;
        }

        public void Recycle()
        {
            OnRecycle();
        }

        /// <summary>
        /// 回收
        /// </summary>
        protected virtual void OnRecycle()
        {
            ObjectPoolManager.GetInstance().PushObjectToPool(this);
            ResetPrefab();
            gameObject.SetActive(false);
        }

        //重置预设数据
        private void ResetPrefab()
        {
            transform.localPosition = Vector3.zero;
            transform.localEulerAngles = Vector3.zero;
        }
    }
}
namespace Miss
{
    /// <summary>
    /// 简易的对象实体类
    /// </summary>
    public class SimpleEntity : ObjectPoolEntityBase
    {

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值