using UnityEngine; using SK.Framework; public class Example : MonoBehaviour { //子弹预制体 [SerializeField] private GameObject bulletPrefab; private void Start() { MonoObjectPool.CreateBy(() => { var instance = Ins
实现一个对象池脚本ObjectPool.cs,利用序列化的游戏对象预制体来快速生成和回收大量相同类型的物体。 using System.Collections.Generic; using UnityEngine; /// <summary> /// 此类实现了对象池模式,用于创建和管理大量的游戏对象 /// </summary> public class ObjectPool { private readonly GameObject prefab; ...
UnityEngine.Pool 状态机 简单写法 分块的写法 更进一步 对状态机的思考 单例 命令模式 Command Pattern 观察者模式 Prototype 原型模式 Strategy Visitor Façade Adapter Decorator Dependency Injection 依赖注入 后记 一口气写完最近了解的所有设计模式, 内容参考自两本书, 一本叫<Level Up Your Code With Game Pr...
从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...
【Unity项目优化宝典】对象池的使用ObjectPool 一:对象池的理解 1.核心思想 是创建一个池子,池子里面一开始有预存有一定数量的对象,当你需要使用对象时直接从池子里取就可以,如果池子里面预存的数量不够就通过池子创建新的对象拿来用。当对象使用结束后不直接删除,而是归还给池子,如果池子里对象总数大于预存数量的...
using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; public class ObjectPool<T> { private readonly Stack<T> stack = new Stack<T>(); private readonly Func<T> actionOnNew; private readonly UnityAction<T> actionOnGet; private readonly UnityAction<T> ...
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—— 对象池 新建一个脚本ObjectPool继承自泛型基类MonoSingleton《ObjectPool》 是以ObjectPool就会是一个单例类 usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;///<summary>///对象池(重复调用/使用的游戏物体) 子弹,技能,导弹,敌人///</summary>publicclassObjectPool:MonoSingle...
最近在Unity2021,发现了新的命名空间UnityEngine.Pool,这套API最早出现的地方应该是UGUI。后面大概是用到这套API的Package太多,干脆变成默认的API,可喜可贺。 初阶使用 所有类如下图所示 使用很简单,所有类API风格分为两种: 手动Get,手动Release; var list=ListPool<MyType>.Get();// Do Something with list.Lis...