当前位置:网站首页>复杂嵌套的对象池(5)——对象池的统一管理和拓展
复杂嵌套的对象池(5)——对象池的统一管理和拓展
2022-07-24 03:36:00 【永恒星】
【统一管理对象池】
统一管理对象池,需要提供初始化、借出对象、回收对象、增加对象池、减少对象池、清空对象池的方法,比较简单。
【代码实现】
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
namespace Cache
{
public class ObjectPoolManager
{
private static ObjectPoolManager _instance;
public static ObjectPoolManager Instance
{
get
{
if (_instance == null)
_instance = new ObjectPoolManager();
return _instance;
}
}
public Dictionary<string, ObjectPoolBase> pools = new Dictionary<string, ObjectPoolBase>();
private Dictionary<int, string> lentInstance2Object = new Dictionary<int, string>();
private ObjectPoolAsset objectPoolAsset;
private string assetPath = "Assets/Resources/ObjectPoolAsset.asset";
#if UNITY_EDITOR
public void Awake()
{
objectPoolAsset = Resources.Load<ObjectPoolAsset>(assetPath);
if (objectPoolAsset == null)
{
objectPoolAsset = ScriptableObject.CreateInstance<ObjectPoolAsset>();
AssetDatabase.CreateAsset(objectPoolAsset, assetPath);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
}
#endif
public void InitPool()
{
#if !UNITY_EDITOR
objectPoolAsset = Resources.Load<ObjectPoolAsset>(assetPath);//不是Editor的情况下如何载入配置,这个可以根据需要自己写
#endif
foreach (PoolInfo poolInfo in objectPoolAsset.poolInfos)
{
AddPool(poolInfo);
}
}
public GameObject LendObjectOut(string poolName, string objectName, Transform parent = null, object[] extradata = null)
{
GameObject go = null;
if(!pools.ContainsKey(poolName))
{
Debug.LogWarning("该对象池不存在:" + poolName);
return null;
}
go = pools[poolName].LendObjectOut(objectName, parent, extradata);
if(go)
{
lentInstance2Object[go.GetInstanceID()] = poolName;
}
return go;
}
public bool RecycleObject(GameObject go)
{
if (go == null)
return false;
if (!lentInstance2Object.ContainsKey(go.GetInstanceID()))//表示不是从该对象池中借出的
return false;
return pools[lentInstance2Object[go.GetInstanceID()]].RecycleObject(go);
}
public bool AddPool(PoolInfo poolInfo)
{
if (pools.ContainsKey(poolInfo.poolName))
{
Debug.LogWarning("已包含该对象池:" + poolInfo.poolName);
return false;
}
switch (poolInfo.poolType)
{
case PoolType.Single:
pools.Add(poolInfo.poolName, new SingleObjectPool(poolInfo.capacity, poolInfo.poolName, poolInfo.expansionStrategy));
break;
case PoolType.Multi:
pools.Add(poolInfo.poolName, new MultiObjectPool(poolInfo));
break;
case PoolType.ObjectType:
pools.Add(poolInfo.poolName, new ObjectTypePool(poolInfo));
break;
case PoolType.StageType:
pools.Add(poolInfo.poolName, new StageObjectPool(poolInfo));
break;
case PoolType.NONE:
Debug.LogWarning("无效的对象池类型:" + poolInfo.poolName);
break;
}
return true;
}
public bool RemovePool(string poolName)
{
if (!pools.ContainsKey(poolName))
{
Debug.LogWarning("不存在该对象池:" + poolName);
return false;
}
ObjectPoolBase pool = pools[poolName];
pools.Remove(poolName);
pool.Dispose();
return true;
}
public void ClearPool()
{
foreach (var item in pools.Values)
{
item.Dispose();
}
pools.Clear();
lentInstance2Object.Clear();
}
}
}
【拓展】
1.扩展编辑器:保证在任意编辑ObjectPoolAsset.asset时,不会导致在InitPool()时报错,目前的代码里并没有控制。
2.减少内存:依靠lentInstance2Object来找到对象属于哪个池子,因为做了多层嵌套,会导致重复,可以给物体添加一个类来读取,就省去了lentInstance2Object。
3.全部用ObjectPoolBase来实现不同类型的对象池的自由组合
4.改用基于AA的加载方式来加载物体
5.添加异步加载的方法
边栏推荐
- Conteneur STL set
- A series of problems of dp+ backtracking segmentation palindrome string
- Binary tree traversal
- leetcode hot 100(刷题篇8)(232/88/451/offer10/offer22/344/)
- 08 reptile project
- JS 數組 isAarray() typeof
- Simulink code generation: variable subsystem and its code
- Standard C language 10
- [super complete sorting] Cisco and Huawei order comparison memo, take it away without thanks! Anytime, anywhere
- Leetcode Hot 100 (Brush Topic 8) (232 / 88 / 451 / offer10 / offer22 / 344 /)
猜你喜欢

Jump statements break and continue

正则表达式 \b \B 深入浅出理解单词边界的匹配

Emqx v4.4.5 Publishing: new exclusive subscriptions and mqtt 5.0 publishing attribute support

How emqx 5.0 under the new architecture of mria + rlog realizes 100million mqtt connections

A simple and perfect WPF management system framework source code

Data Lake: introduction to Apache Hudi

buu web

Gpushare.com | 如何使用TensorBoardX可视化工具?
![[JS reverse hundred examples] a public resource trading network, reverse analysis of announcement URL parameters](/img/0b/3645a0a8ec677f96427b35bd371e7e.png)
[JS reverse hundred examples] a public resource trading network, reverse analysis of announcement URL parameters

SolidWorks CAM data cannot be recovered because a processed part has been detected.
随机推荐
MySql的DDL和DML和DQL的基本语法
Realize the communication before two pages (using localstorage)
什么是IMU?
[super complete sorting] Cisco and Huawei order comparison memo, take it away without thanks! Anytime, anywhere
Hcip day 9 notes (OSPF routing feedback, routing policy, and Configuration Guide)
Gpushare.com | 如何使用TensorBoardX可视化工具?
移动通信的新定义:R&SCMX500 将提高5G设备的IP数据吞吐量
STL set container
IO stream sorting
[wepy2.0] installation
Express内置的中间件
C language classic exercises (2) - "bubble sort"“
如何在 pyqt 中实现桌面歌词
How to efficiently install the GPU version of mindspire
Arduino interrupt realizes rising edge detection and executes other functions
Convert the pseudo array returned by childNodes into a true array
Emqx v4.4.5 Publishing: new exclusive subscriptions and mqtt 5.0 publishing attribute support
Jump statement in day011 loop structure
MySql的DDL和DML和DQL的基本语法
C文件操作详解