当前位置:网站首页>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 .
边栏推荐
- 题解 洛谷P1762 偶数/6.21校内考试T2
- C#/VB. Net to convert PDF to excel
- 宜明昂科在港交所递表:2021年亏损翻倍,过往融资额存在夸大情形
- Redis+AOP+自定义注解实现限流
- Pytorch builds transformer to realize multivariable and multi step time series forecasting (load forecasting)
- 网上办理股票开户安全性高吗?
- The Best of Many Worlds_ Dual Mirror Descent for Online Allocation Problems
- Business atlas in super factory
- Online linear programming: Dual convergence, new algorithms, and regret bounds
- [gateway development] handle the IP address segment represented by CIDR when NGX nested Lua
猜你喜欢
![[deep learning] (3) encoder mechanism in transformer, complete pytoch code attached](/img/cb/d385bee7a229e8d11f5fa8af66311f.gif)
[deep learning] (3) encoder mechanism in transformer, complete pytoch code attached

Pytorch builds transformer to realize multivariable and multi step time series forecasting (load forecasting)

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

Zadig officially launched vs code plug-in, making local development more efficient

全面掌握const的用法《一》

FANUC机器人_KAREL编程入门(2)_通用IO信号的使用方法

深入虚拟内存(Virtual Memory,VM)

Zadig + SonarQube,为开发过程安全保驾

邂逅阿维塔 11:强产品力下久违的新鲜感

This simple little function saves 213 hours for our production research team in half a year
随机推荐
邂逅阿维塔 11:强产品力下久违的新鲜感
Wave picking of WMS warehouse management system module
With the development of industrial Internet as the starting point, the industry can enter a new stage of development
【kotlin】好看的弹出框、自定义弹出框(对话框)、扩展函数、菊花等待条、消息提示框
Code example of hiredis
Flowable boundary timer
CPU、GPU、TPU、NPU区别
论文解读(DCN)《Towards K-means-friendly Spaces: Simultaneous Deep Learning and Clustering》
FANUC机器人_KAREL编程入门(2)_通用IO信号的使用方法
Prometeus 2.36.0 新特性
超级工厂里的生意图鉴
How to solve the problem of desktop without sound
Is it safe and reliable to open a securities account in changtou school?
Career consultation | how to answer the question of career planning during the interview?
Description détaillée du schéma technique du sous - environnement syntonique auto - test de Zadig pour les développeurs
Business atlas in super factory
DBNN实验进展
题解 洛谷P1762 偶数/6.21校内考试T2
生产环境sonarqube安装
Common tool classes and Commons class libraries