框架中将对象池划分为两种,一种是通过new运算符创建对象的对象池,另一种是对象类继承自MonoBehaviour,需要自定义创建方法的对象池,我们将它们分别称为ObjectPool、MonoObjectPool。 为需要实现对象池管理的对象类继承IPoolable接口,接口中包含bool类型字段IsRecycled,用于标记该对象是否已经回收,以及OnRecycled方法,用于实现...
ObjectPool使用了一个Stack堆(last-in-first-out (LIFO))后进先出的结构,当我们需要新建(Get)一个新的Object的时候先从Stack的堆顶取出第一个,当使用结束的时候将其释放掉(Release),也就是将其重新放回Stack堆里面。原理其实很简单,当然里面增加了放进和放出时候的Action,可以用来传入回调函数,分别在取出和释放...
Unity官方对象池C#脚本在 Visual Scripting中的使用,主要是Get() 和 Release()的功能实现. 对象池管理C#脚本,挂载在空的Game Object上,只负责最简单的创建和释放/销毁等核心功能. Pool.Get()功能C#脚本参照官方案例""Custom C# nodes""修改,挂载在Player上, Visual Scripting的模糊查找器中,直接调用即可. Pool.R...
2.DictionaryPool<T0,T1> 字典池,继承自CollectionPool 3.GenericPool< T0 > 通用池,ObjectPool的静态实现,启用了集合检查,以确保不会重复回收。非线程安全 4.HashSetPool< T0> 哈希集池,CollectionPool<T0,T1>的HashSet版本。 5.LinkedPool< T0> 链表池,IObjectPool的链表版本,池子内的对象是以链表形式保存...
//释放对象池 ObjectPool.Release<Person>(); 二、MonoObjectPool 以一个Bullet子弹类为例,它挂载于子弹的Prefab预制体上 using UnityEngine; using SK.Framework; public class Bullet : MonoBehaviour, IPoolable { public bool IsRecycled { get; set; } public void OnRecycled() { gameObject.SetActive(false...
public class ClassObjectPool:IDisposable { //类对象 在池中的 常驻数量 public Dictionary<int, byte> ClassObjectCount { private set; get; } //类对象池缓存字典,key是hash_code,value是该类型的缓存队列 public Dictionary<int, Queue<object>> m_dicClassObjectPool; ...
public class ObjectPool<T> : IDisposable, IObjectPool<T> where T : class { internal readonly List<T> m_List; private readonly Func<T> m_CreateFunc; private readonly Action<T> m_ActionOnGet; private readonly Action<T> m_ActionOnRelease; private readonly Action<T> m_ActionOnDestroy;...
Debug.LogError("Internal error. Trying to destroy object that is already released to pool."); }if(actionOnRelease !=null) { actionOnRelease.Invoke(element); } stack.Push(element);if(stack.Count >CountAll) { CountAll=stack.Count;
public ObjectPool<T0>(Func<T> createFunc, Action<T> actionOnGet, Action<T> actionOnRelease, Action<T> actionOnDestroy, bool collectionCheck, int defaultCapacity, int maxSize); 参数简介如下: createFunc:对象实例化实现; actionOnGet:出池回调; actionOnRelease...
GetGet an instance from the pool. If the pool is empty then a new instance will be created. ReleaseReturns the instance back to the pool. Did you find this page useful? Please give it a rating: Report a problem on this page