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