From 3d83ae08379aa835f2dca84f6f05fc66859877cc Mon Sep 17 00:00:00 2001 From: ninemine <1371605831@qq.com> Date: Thu, 4 Dec 2025 17:51:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=87=86=E5=A4=87=E5=AF=B9=E7=B2=92=E5=AD=90?= =?UTF-8?q?=E6=95=88=E6=9E=9C=E7=AD=89=E7=94=9F=E6=88=90=E6=80=A7=E6=95=88?= =?UTF-8?q?=E6=9E=9C=E8=BF=9B=E8=A1=8C=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Framework/GameContent/GameObjectPool.cs | 48 +++++++++++++++++++ .../GameContent/GameObjectPool.cs.meta | 2 + 2 files changed, 50 insertions(+) create mode 100644 Assets/Scripts/Framework/GameContent/GameObjectPool.cs create mode 100644 Assets/Scripts/Framework/GameContent/GameObjectPool.cs.meta diff --git a/Assets/Scripts/Framework/GameContent/GameObjectPool.cs b/Assets/Scripts/Framework/GameContent/GameObjectPool.cs new file mode 100644 index 0000000..9730bb2 --- /dev/null +++ b/Assets/Scripts/Framework/GameContent/GameObjectPool.cs @@ -0,0 +1,48 @@ +using Convention; +using Convention.Experimental.Modules; +using System; +using System.Collections.Generic; +using static Convention.Experimental.Architecture; + +namespace Demo.Game +{ + public class GameObjectPool : MonoSingleton + { + private Dictionary, ScriptableObject> Pool = new(); + + public ScriptableObject Spawn(Func creater) + { + if (Pool.TryGetValue(creater, out var prefab) == false) + { + prefab = Pool[creater] = creater(); + prefab.gameObject.SetActive(false); + prefab.transform.SetParent(transform); + } + if (prefab is CameraObject) + { + return prefab; + } + else + { + var result = GetModule().Spawn(prefab.gameObject).GetComponent(); + result.gameObject.SetActive(true); + return result; + } + } + + public void Unspawn(ScriptableObject obj) + { + if (obj is CameraObject camera) + { + camera.gameObject.SetActive(false); + camera.transform.SetParent(transform); + } + else if (obj is RootObject) + return; + else + { + GetModule().Unspawn(obj.gameObject); + } + } + } +} diff --git a/Assets/Scripts/Framework/GameContent/GameObjectPool.cs.meta b/Assets/Scripts/Framework/GameContent/GameObjectPool.cs.meta new file mode 100644 index 0000000..327148d --- /dev/null +++ b/Assets/Scripts/Framework/GameContent/GameObjectPool.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 94ffe9340adcc294c9d67b9f0bb825eb \ No newline at end of file