using UnityEngine; using SK.Framework; public class Example : MonoBehaviour { //子弹预制体 [SerializeField] private GameObject bulletPrefab; private void Start() { MonoObjectPool.CreateBy(() => { var instance = Instantiate(bulletPrefab); instance.transform.SetParent(transform); instance.transform....
using UnityEngine; public class PoolManager : BaseManager<PoolManager> { // 存储动可重用的GameObject。 public List<GameObject> _objectList = new List<GameObject>(); // 对象池大小 private int _capacity = 5; GameObject go; protected override void Awake() { base.Awake(); InitPoolObj(); } /...
UnityEngine.Pool 状态机 简单写法 分块的写法 更进一步 对状态机的思考 单例 命令模式 Command Pattern 观察者模式 Prototype 原型模式 Strategy Visitor Façade Adapter Decorator Dependency Injection 依赖注入 后记 一口气写完最近了解的所有设计模式, 内容参考自两本书, 一本叫<Level Up Your Code With Game Pr...
usingSystem;usingSystem.Collections.Generic;usingUnityEngine;usingUnityEngine.Events;publicclassObjectPool<T>{privatereadonlyStack<T> stack =newStack<T>();privatereadonlyFunc<T>actionOnNew;privatereadonlyUnityAction<T>actionOnGet;privatereadonlyUnityAction<T>actionOnRelease;publicintCountAll {get;private...
using UnityEngine; /// <summary> /// 所有需要使用对象池机制的对象的基类 /// </summary> public class PooledObject : MonoBehaviour { // 归属的池 public ObjectPool Pool { get; set; } // 场景中某个具体的池(不可序列化) [System.NonSerialized] ...
using System.Collections; using System.Collections.Generic; using UnityEngine; [System.Serializable] public class ObjectPoolItem { public GameObject objectToPool; public int amountToPool; public bool shouldExpand = true; } public class ObjectPooler : MonoBehaviour { public static ObjectPooler ...
从Unity2021开始,引入了UnityEngine.Pool,这里我们简单介绍下Unity内置的对象池。 Unity对象池 下面这个是官方的对象池 using System; using System.Collections.Generic; namespace UnityEngine.Pool { // // 摘要: // A stack based Pool.IObjectPool_1. public class ObjectPool<T> : IDisposable, IObjectPoo...
using UnityEngine; public class BulletsPool : MonoBehaviour { public static BulletsPool bulletsPoolInstance; //子弹池单例 public GameObject bulletObj; //子弹perfabs public int pooledAmount = 5; //子弹池初始大小 public bool lockPoolSize = false; //是否锁定子弹池大小 ...
PoolObject.cs 用于延迟回收对象 usingSystem.Collections;usingUnityEngine;usingUtilty;publicclassPoolObject:MonoBehaviour{privatevoidOnEnable(){StartCoroutine(DelayRececle(2));}//延迟回收协程IEnumeratorDelayRececle(floatinterval){//等待几秒yieldreturnnewWaitForSeconds(interval);//回收当前对象ObjectPool.GetInstanc...
//设置对象池的最大缓存数量 ObjectPool.SetMaxCacheCount<Person>(100); 4.释放对象池 释放对象池不仅是释放池中的对象,对象池本身也会被释放。 //释放对象池 ObjectPool.Release<Person>(); 二、MonoObjectPool 以一个Bullet子弹类为例,它挂载于子弹的Prefab预制体上 using UnityEngine; using SK.Framework;...