当前位置:网站首页>Complex nested object pool (5) -- unified management and expansion of object pool
Complex nested object pool (5) -- unified management and expansion of object pool
2022-07-24 03:41:00 【Eternal star】
【 Unified management of object pool 】
Unified management of object pool , Initialization is required 、 Lending to 、 Recovery object 、 Add object pool 、 Reduce the object pool 、 How to empty the object pool , Relatively simple .
【 Code implementation 】
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);// No Editor How to load the configuration in the case of , This can be written by yourself as needed
#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(" The object pool does not exist :" + 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()))// Indicates that it is not borrowed from the object pool
return false;
return pools[lentInstance2Object[go.GetInstanceID()]].RecycleObject(go);
}
public bool AddPool(PoolInfo poolInfo)
{
if (pools.ContainsKey(poolInfo.poolName))
{
Debug.LogWarning(" The object pool is already included :" + 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(" Invalid object pool type :" + poolInfo.poolName);
break;
}
return true;
}
public bool RemovePool(string poolName)
{
if (!pools.ContainsKey(poolName))
{
Debug.LogWarning(" The object pool does not exist :" + 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();
}
}
}
【 expand 】
1. Extended editor : Be sure to edit at will ObjectPoolAsset.asset when , Will not result in InitPool() Times wrong , There is no control in the current code .
2. Reduce memory : rely on lentInstance2Object To find out which pool the object belongs to , Because of the multi-layer nesting , Will lead to repetition , You can add a class to the object to read , You don't have to lentInstance2Object.
3. All use ObjectPoolBase To realize the free combination of different types of object pools
4. Use based on AA Loading method to load objects
5. Add the method of asynchronous loading
边栏推荐
- Xiaodi and Xiaohui
- [super complete sorting] Cisco and Huawei order comparison memo, take it away without thanks! Anytime, anywhere
- RTOS internal skill cultivation (10) | in depth analysis of RTOS kernel context switching mechanism
- Network parameter management
- leetcode hot 100(刷題篇8)(232/88/451/offer10/offer22/344/)
- Convert the pseudo array returned by childNodes into a true array
- Master chip csu18m92 develops intelligent scale scheme
- Developers share mindspire Lite experience, one click image segmentation
- C file operation details
- Technical dry goods | evaluation index based on mindspire detailed perflexity language model
猜你喜欢

数据湖:开源数据湖方案DeltaLake、Hudi、Iceberg对比分析

Gpushare. COM | how to use tensorboardx visualization tool?

Appendtofile append failed

Paper reading: the perfect match: 3D point cloud matching with smoothed densities

idea写web项目时报错Failed to load resource: the server responded with a status of 404 (Not Found)

Talk about the application of FIFO

正則錶達式 \b \B 深入淺出理解單詞邊界的匹配

排雷游戏(解析)

Bet on the whole scene, what is the odds of glory?

Shengsi YiDianTong | deep learning analysis of classical convolutional neural network
随机推荐
Standard C language 10
Redis transaction learning
C language implementation of user login program
Arduino interrupt realizes rising edge detection and executes other functions
What are Caicai and qiniu doing? Is it safe to open an account?
Conteneur STL set
The local picture cannot be displayed after the uniapp H5 is packaged
93. (leaflet chapter) leaflet situation plotting - modification of attack direction
Preliminary exploration of Flink principle and flow batch integration API (II) V2
Convert the pseudo array returned by childNodes into a true array
QT custom class uses custom parametric signals and slots
Read and understand the advantages of the LAAS scheme of elephant swap
Cache component status when Vue components are switched, that is, dynamic components keep alive dynamic components and asynchronous components
DOM相关的方法概念
An in-depth explanation of CAS is necessary for interview practice
一篇搞定CAS,深度讲解,面试实践必备
leetcode hot 100(刷題篇8)(232/88/451/offer10/offer22/344/)
Learning summary | truly record what mindspire two-day training camp can bring to you (1)!
MLP-多层感知机
STL multimap