当前位置:网站首页>Complex nested object pool (2) -- manage the object pool of a single instance object
Complex nested object pool (2) -- manage the object pool of a single instance object
2022-06-10 05:37:00 【Eternal star】
【 Object pool interface 】
According to the data and operations required by the object pool , First define the basic interface
public interface IObjectPool : ICountable, IShrinkable, IDisposable
{
GameObject LendObjectOut(string objectName, Transform parent = null,params object[] extradata);
bool RecycleObject(GameObject go);
}The operation methods are lending object and recycling object .
When lending an object, you need to pass in the object name , Identify which object you want to lend ;parent Indicates who the parent object of the object to be lent is ; There is also a default parameter array .
【 Object pool base class 】
The key data to be counted is the total number of instance objects generated , Total number of cached instance objects , The total number of instance objects that can be cached .
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
namespace Cache
{
public interface IObjectPool : ICountable, IShrinkable, IDisposable
{
GameObject LendObjectOut(string objectName, Transform parent = null,params object[] extradata);
bool RecycleObject(GameObject go);
}
public class ObjectPoolBase : IObjectPool
{
/// <summary>
/// Total number of instance objects generated
/// </summary>
public int TotalCount { get; protected set; }
/// <summary>
/// Total number of cached instance objects
/// </summary>
public int CachedCount { get; protected set; }
/// <summary>
/// Cacheable instance object capacity
/// </summary>
public virtual int TotalCapcity { get; protected set; }
protected ICache poolCache;
/// <summary>
/// Object pool name
/// </summary>
protected string poolName;
/// <summary>
/// Object pool capacity size
/// </summary>
protected int capacity;
protected Action<bool> onlendObjectOut;
protected Action<bool> onRecycleObject;
public virtual int Capacity()
{
return poolCache.Capacity();
}
public virtual int Count()
{
return poolCache.Count();
}
public virtual void Dispose()
{
poolCache.Dispose();
}
public virtual bool Shrink(ShrinkStrategy shrinkStrategy, float percent, int count, bool weedout)
{
return poolCache.Shrink(shrinkStrategy, percent, count, weedout);
}
public virtual GameObject LendObjectOut(string objectName, Transform parent = null, params object[] extradata)
{
return null;
}
public virtual bool RecycleObject(GameObject go)
{
return false;
}
}
}【 Manage object pools for instance objects of a single source object 】
We know that all objects in an object pool are instances of a source object , That is to call go = UnityEngine.Object.Instantiate(SourceGo,parent) when , The object pool holds a series of go, and Source Is what we call the source object , This source object should be obtained by loading . The object pool manages instance objects , Not the source object , The two should be managed separately .
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
namespace Cache
{
public class SingleObjectPool:ObjectPoolBase
{
private FIFO<GameObject> _poolCache;
private ExpansionStrategy expansionStrategy;
// Override the... Of the cacheable instance object get Method , It changes as it shrinks
public override int TotalCapcity { get => _poolCache.Capacity(); protected set => TotalCapcity = value; }
public SingleObjectPool(int capacity,string poolName, ExpansionStrategy expansionStrategy = ExpansionStrategy.NONE, Action<bool> onlendObjectOut = null, Action<bool> onRecycleObject = null)
{
poolCache = new FIFO<GameObject>(capacity, expansionStrategy, OnWeedOut);
_poolCache = poolCache as FIFO<GameObject>;
this.capacity = capacity;
this.poolName = poolName;
this.expansionStrategy = expansionStrategy;
this.onlendObjectOut = onlendObjectOut;
this.onRecycleObject = onRecycleObject;
}
public override GameObject LendObjectOut(string objectName,Transform parent = null, params object[] extradata)
{
if(string.IsNullOrEmpty(objectName))
return null;
if (!objectName.Equals(poolName))// If the name of the object to be lent is different from the name of the current object pool , Description cannot borrow objects from the current object pool
return null;
GameObject go = null;
if(_poolCache.Get(out go))// Get it from the pool first
{
CachedCount--;// Cache object reduction
onlendObjectOut?.Invoke(false);
if(parent != null)
go.transform.SetParent(parent);
return go;
}
// No, just instantiate one , Get a source object to instantiate
go = UnityEngine.Object.Instantiate(SourceGameObjectMap.GetSourceGameObject(objectName),parent);
if (go != null)
{
TotalCount++;// Instance objects are added
onlendObjectOut?.Invoke(true);
}
return go;
}
public override bool RecycleObject(GameObject go)
{
if(_poolCache.Put(go))
{
CachedCount++;// Cache objects increase
onRecycleObject?.Invoke(false);
return true;
}
return false;
}
protected void OnWeedOut(GameObject go)// Destroy if eliminated
{
if (go == null)
return;
go.transform.SetParent(null); // To prevent Destroy One frame slower is still GetComponentsInChildren Get
GameObject.Destroy(go);
TotalCount--;// At the time of destruction , The number of cached and generated instance objects is reduced
CachedCount--;
onRecycleObject?.Invoke(true);
}
}
}【 Management of source objects 】
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Cache
{
public class SourceGameObjectMap
{
public static Dictionary<string, GameObject> map = new Dictionary<string, GameObject>();
public static GameObject GetSourceGameObject(string objectName)
{
if(map.TryGetValue(objectName, out GameObject go))
{
return go;
}
go = Resources.Load<GameObject>(objectName);// First, simply use Resources.Load, You can change it to AB or AA
map.Add(objectName, go);
return go;
}
public static bool UnlaodSourceGameObject(string objectName)
{
if(map.TryGetValue(objectName, out GameObject go))
{
map.Remove(objectName);
Resources.UnloadAsset(go);
return true;
}
return false;
}
}
}边栏推荐
- QT配置OpenCV-4.5.1并运行程序
- 自定義Tooltips提示氣泡Js插件
- An analysis of DuPont analysis: the financial situation of East China Construction Group Co., Ltd
- 全球首个金融图数据库测试基准立项,蚂蚁集团开放专利共建
- Api 接口优化的几个技巧
- . Net C Foundation (7): interface - how people interact with cats
- Model Lightweight - cutting distillation Series yolov5 nonestructive Cutting (attached source)
- 第六章 软件测试工具(此章完结)
- Win10 installation process
- 使用GAT解析Minidump(图形界面)
猜你喜欢

Study notes for typescript
![[STM32] transplantation of Hal library on 4-pin 0.96 inch OLED screen - hardware IIC (I)](/img/38/53059d442cfd6d5ed5a940586b363c.jpg)
[STM32] transplantation of Hal library on 4-pin 0.96 inch OLED screen - hardware IIC (I)

MYSQL第一篇(基础知识)

【软件工程导论】知识点汇总 | 适用于考试复习 | 轻松通过考试

Three principles of layout design

生成boot_para.img

在感知情绪上,理财助理机器人“支小宝”更进一步

MySQL Part 1 (basic knowledge)
![[introduction to software engineering] summary of knowledge points | suitable for examination review | easy to pass the exam](/img/6c/4aa3d995f6d6c1a46d50e4532b459d.png)
[introduction to software engineering] summary of knowledge points | suitable for examination review | easy to pass the exam

How do you do if your English is too poor? Write a "100 word chop" software to recite words for yourself
随机推荐
An analysis of DuPont analysis: the financial situation of East China Construction Group Co., Ltd
Ant group joined the commitment of low-carbon patents, opened patents to the world free of charge, and promoted energy conservation and emission reduction
openGauss数据库性能调优概述及实例分析
Sequential search, binary search
Contact QR code generation plug-in qrcode js
Installation and configuration of NPM and yarn
使用GAT解析Minidump(图形界面)
Idea cancels data simplification
Blocking problem after the mobile terminal pulls up the keyboard
MySQL Part 2 (core technology)
Five best! Ant group passed the highest level evaluation of the "stability Assurance Plan" of the ICT Institute
JS wechat games - fighting mosquitoes
聊一聊蚂蚁AI技术里的“老实担当”
[homeassistant drive servo]
如何保障系统稳定性并实现减排?蚂蚁集团有这些关键技术
IDC released China Cloud native market analysis. Ant group has become one of the most comprehensive manufacturers
Bubble Sort Bubble_ sort
Model lightweight pruning distillation quantification series yolov5 lossless pruning (with source code)
Be diligent to tell me what I have done these days
[STM32] transplantation of Hal library on 4-pin 0.96 inch OLED screen - hardware IIC (I)