当前位置:网站首页>Complex nested object pool (4) -- manage the object pool of multi class instances and multi-stage instances
Complex nested object pool (4) -- manage the object pool of multi class instances and multi-stage instances
2022-06-28 22:52:00 【Eternal star】
【 Classification of objects 】
Throughout the game , There are many objects , Objects can be classified according to certain standards : For example, these objects belong to UI、 Those objects belong to the terrain 、 There are also objects that belong to roles, and so on ; Some objects only appear in the first stage 、 Some only appear in the second stage , Some only appear in the third stage and so on ; Some objects belong to a story or task ; Some objects are in a certain place or range, etc .
If we want to manage all instance objects in the game , Then create a complex nested super object pool to manage , How this object pool is nested inside , That is, how to classify objects , Yes, according to the game , That is, what the business does . We need to think ahead , This also reflects whether the game can be grasped as a whole .
In this paper , Classify objects into different categories and stages , There are different types of objects in different stages , You can also make different types of objects in different stages , The code is basically the same , Both are based on the object pool that manages a single instance object and the object pool that manages multiple instances .
【 Manage the implementation of multi class instance object pool 】
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
namespace Cache
{
public enum ObjectType
{
Common = 0,
UI = 1,
Role = 2,
Effect = 3,
Terrain = 4,
Collider = 5,
Camera = 6,
Special
}
public class ObjectTypePool : ObjectPoolBase
{
private Cache<ObjectType, MultiObjectPool> _poolCache;//ObjectType The essence is string, It's just that the number is less and enumeration is used , instead of MultiObjectPool of use string
private Dictionary<int, ObjectType> lentInstance2ObjectType = new Dictionary<int, ObjectType>();
private Dictionary<string, ObjectType> nameMap = new Dictionary<string, ObjectType>();
public ObjectTypePool(int capacity, string poolName, CacheWeedOutStrategy cacheWeedOutStrategy, Action<bool> onlendObjectOut = null, Action<bool> onRecycleObject = null) : base(capacity, poolName, onlendObjectOut, onRecycleObject)
{
poolCache = new Cache<ObjectType, MultiObjectPool>(cacheWeedOutStrategy, capacity, OnWeedOut);
_poolCache = poolCache as Cache<ObjectType, MultiObjectPool>;
foreach (ObjectType item in Enum.GetValues(typeof(ObjectType)))
{
nameMap.Add(item.ToString(), item);
}
}
public ObjectTypePool(PoolInfo poolInfo, Action<bool> onlendObjectOut = null, Action<bool> onRecycleObject = null) : base(poolInfo, onlendObjectOut, onRecycleObject)
{
if (poolInfo != null)
{
foreach (ObjectType item in Enum.GetValues(typeof(ObjectType)))
{
nameMap.Add(item.ToString(), item);
}
if (poolInfo.poolType == PoolType.ObjectType)
{
poolCache = new Cache<ObjectType, MultiObjectPool>(poolInfo.cacheWeedOutStrategy, poolInfo.capacity, OnWeedOut);
_poolCache = poolCache as Cache<ObjectType, MultiObjectPool>;
// Initialize child object pool
foreach (var item in poolInfo.subPoolInfo)
{
var subPool = new MultiObjectPool(item, OnLendObjectOut, OnRecycleObject);
_poolCache.Put(nameMap[subPool.poolName], subPool);
TotalCapcity += subPool.TotalCapcity;
}
// Use the default pool settings
for (int i = poolInfo.subPoolInfo.Count; i < poolInfo.subPoolCount; i++)
{
var subPool = new MultiObjectPool(poolInfo.defaultSubPoolInfo, OnLendObjectOut, OnRecycleObject);
_poolCache.Put(nameMap[subPool.poolName], subPool);
TotalCapcity += subPool.TotalCapcity;
}
}
}
}
public override GameObject LendObjectOut(string objectName, Transform parent = null, object[] extradata = null)
{
GameObject go = null;
if (string.IsNullOrEmpty(objectName))
return null;
MultiObjectPool multiObjectPool = null;
ObjectType objectType;// To pass in parameters of the object type
if(Enum.TryParse<ObjectType>(extradata[0].ToString(),out objectType))
{
if (_poolCache.Get(objectType, out multiObjectPool))
{
go = multiObjectPool.LendObjectOut(objectName, parent);
}
}
if(go != null)
lentInstance2ObjectType[go.GetInstanceID()] = objectType;
return go;
}
public override bool RecycleObject(GameObject go)
{
if (go == null)
return false;
if (!lentInstance2ObjectType.ContainsKey(go.GetInstanceID()))
return false;
MultiObjectPool pool;
if (_poolCache.Get(lentInstance2ObjectType[go.GetInstanceID()], out pool))
{
if (pool.RecycleObject(go))
{
lentInstance2ObjectType.Remove(go.GetInstanceID());
return true;
}
}
return false;
}
private void OnWeedOut(MultiObjectPool multiObjectPool)
{
TotalCount -= multiObjectPool.TotalCount;
CachedCount -= multiObjectPool.CachedCount;
TotalCapcity -= multiObjectPool.TotalCapcity;
multiObjectPool.Dispose();
}
private void OnLendObjectOut(bool instantiated)
{
if (instantiated)
TotalCount++;
else
CachedCount--;
onlendObjectOut?.Invoke(instantiated);
}
private void OnRecycleObject(bool destroyed)
{
if (destroyed)
{
TotalCount--;
CachedCount--;
}
else
CachedCount++;
onRecycleObject?.Invoke(destroyed);
}
}
}
【 Manage object pools for multi-stage instances 】
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
namespace Cache
{
public enum StageType
{
FirstStage = 1,
SecondStage,
ThirdStage,
FourthStage,
FifthStage,
}
public class StageObjectPool : ObjectPoolBase
{
private Cache<StageType, ObjectTypePool> _poolCache;
private Dictionary<int, StageType> lentInstance2StageType = new Dictionary<int, StageType>();
private Dictionary<string, StageType> nameMap = new Dictionary<string, StageType>();
public StageObjectPool(int capacity, string poolName, CacheWeedOutStrategy cacheWeedOutStrategy, Action<bool> onlendObjectOut = null, Action<bool> onRecycleObject = null) : base(capacity, poolName, onlendObjectOut, onRecycleObject)
{
poolCache = new Cache<StageType, ObjectTypePool>(cacheWeedOutStrategy, capacity, OnWeedOut);
_poolCache = poolCache as Cache<StageType, ObjectTypePool>;
foreach (StageType item in Enum.GetValues(typeof(StageType)))
{
nameMap.Add(item.ToString(), item);
}
}
public StageObjectPool(PoolInfo poolInfo, Action<bool> onlendObjectOut = null, Action<bool> onRecycleObject = null) : base(poolInfo, onlendObjectOut, onRecycleObject)
{
if (poolInfo != null)
{
foreach (StageType item in Enum.GetValues(typeof(StageType)))
{
nameMap.Add(item.ToString(), item);
}
if (poolInfo.poolType == PoolType.StageType)
{
poolCache = new Cache<StageType, ObjectTypePool>(poolInfo.cacheWeedOutStrategy, poolInfo.capacity, OnWeedOut);
_poolCache = poolCache as Cache<StageType, ObjectTypePool>;
// Initialize child object pool
foreach (var item in poolInfo.subPoolInfo)
{
var subPool = new ObjectTypePool(item, OnLendObjectOut, OnRecycleObject);
_poolCache.Put(nameMap[subPool.poolName], subPool);
TotalCapcity += subPool.TotalCapcity;
}
// Use the default pool settings
for (int i = poolInfo.subPoolInfo.Count; i < poolInfo.subPoolCount; i++)
{
var subPool = new ObjectTypePool(poolInfo.defaultSubPoolInfo, OnLendObjectOut, OnRecycleObject);
_poolCache.Put(nameMap[subPool.poolName], subPool);
TotalCapcity += subPool.TotalCapcity;
}
}
}
}
public override int Capacity()
{
return poolCache.Capacity();
}
public override int Count()
{
return poolCache.Count();
}
public override GameObject LendObjectOut(string objectName, Transform parent = null, object[] extradata = null)
{
GameObject go = null;
if (string.IsNullOrEmpty(objectName))
return null;
StageType stageType;// The phase type and object type should also be passed in
if (Enum.TryParse<StageType>(extradata[0].ToString(), out stageType))
{
ObjectType objectType;
if(Enum.TryParse<ObjectType>(extradata[0].ToString(), out objectType))
{
ObjectTypePool objectTypePool = null;
if (_poolCache.Get(stageType, out objectTypePool))
{
go = objectTypePool.LendObjectOut(objectName, parent,objectType);
}
}
}
if (go != null)
lentInstance2StageType[go.GetInstanceID()] = stageType;
return go;
}
public override bool RecycleObject(GameObject go)
{
if (go == null)
return false;
if (!lentInstance2StageType.ContainsKey(go.GetInstanceID()))
return false;
ObjectTypePool pool;
if (_poolCache.Get(lentInstance2StageType[go.GetInstanceID()], out pool))
{
if (pool.RecycleObject(go))
{
lentInstance2StageType.Remove(go.GetInstanceID());
return true;
}
}
return false;
}
private void OnWeedOut(ObjectTypePool objectTypePool)
{
TotalCount -= objectTypePool.TotalCount;
CachedCount -= objectTypePool.CachedCount;
TotalCapcity -= objectTypePool.TotalCapcity;
objectTypePool.Dispose();
}
private void OnLendObjectOut(bool instantiated)
{
if (instantiated)
TotalCount++;
else
CachedCount--;
onlendObjectOut?.Invoke(instantiated);
}
private void OnRecycleObject(bool destroyed)
{
if (destroyed)
{
TotalCount--;
CachedCount--;
}
else
CachedCount++;
onRecycleObject?.Invoke(destroyed);
}
}
}
【 Need management class 】
There are now many types of object pools , Just as we need to use the object pool to manage many instances , We need an object pool management class to manage many object pools . Take a look back. , The object pool of multi class instances and the object pool of phase instances in this paper also manage a series of the same objects .
边栏推荐
- Online text filter less than specified length tool
- Google Earth Engine(GEE)——利用sentinel-2数据进行农作物提取分析
- Powerful open source API interface visual management platform Yapi
- 如何结合均线分析伦敦金行情走势线图
- Simple understanding of counting and sorting
- 全面掌握const的用法《一》
- Code example of hiredis
- k线图基础知识图解——单根K线的含义
- Google Earth engine (GEE) -- crop extraction and analysis using sentinel-2 data
- IC Nansha|AMD高级副总裁、大中华区总裁潘晓明:制程、架构、平台优化突破计算边界
猜你喜欢

Leetcode detailed explanation of stack type

LeCun预言AGI:大模型和强化学习都是斜道!我的世界模型才是新路

Windows mysql5.7 enable binlog log

Zadig 构建究竟何强大?一起来实践

如何使用伦敦金画出支撑阻力线

Online text filter less than specified length tool

C#/VB. Net to convert PDF to excel

How powerful is the Zadig build? Practice together

Qtcreater5.15.0 source code compilation process record

Multiomics single cell data integration and regulatory reasoning based on graph linked embedding
随机推荐
How to solve the problem of desktop without sound
Qt5.15中qsrand,srand随机数生成函数已弃用问题
Is it safe and reliable for changtou school to help open a securities account? How to drive
flowable 边界定时器
Panxiaoming, senior vice president of IC nansha|amd and President of Greater China: process, architecture and platform optimization break through the computing boundary
Career consultation | how to answer the question of career planning during the interview?
Water brother's code
长投学堂帮忙开证券账户是安全靠谱的吗?个人如何开
With the development of industrial Internet as the starting point, the industry can enter a new stage of development
leetCode-栈类型详解
This simple little function saves 213 hours for our production research team in half a year
深入虚拟内存(Virtual Memory,VM)
【HackTheBox】dancing(SMB)
Powerful open source API interface visual management platform Yapi
Research Report on workers: middle-aged people account for the highest proportion of naked words
00 後雲原生工程師:用 Zadig 為思創科技(廣州公交)研發開源節流
网上办理股票开户安全性高吗?
计数排序的简单理解
[gateway development] handle the IP address segment represented by CIDR when NGX nested Lua
oracle设置密码复杂度及设置超时退出的功能