diff --git a/Assets/TEngine/Scripts/Runtime/GameObjectHelper.meta b/Assets/TEngine/Scripts/Runtime/GameObjectHelper.meta new file mode 100644 index 00000000..0b9edc6d --- /dev/null +++ b/Assets/TEngine/Scripts/Runtime/GameObjectHelper.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: ba7d4c23c5874d29badd7e7b01e9914c +timeCreated: 1664246673 \ No newline at end of file diff --git a/Assets/TEngine/Scripts/Runtime/GameObjectHelper/GameObjectHelper.cs b/Assets/TEngine/Scripts/Runtime/GameObjectHelper/GameObjectHelper.cs new file mode 100644 index 00000000..92c5cc95 --- /dev/null +++ b/Assets/TEngine/Scripts/Runtime/GameObjectHelper/GameObjectHelper.cs @@ -0,0 +1,112 @@ +using System; +using System.Collections.Generic; +using UnityEngine; + +namespace TEngine.Runtime +{ + public class GameObjectHelper : UnitySingleton + { + private readonly Dictionary _poolDictionary = new Dictionary(); + + public void RegisterPool(string objectName, GameObject ret, int capacity = 100) + { + if (_poolDictionary.ContainsKey(objectName)) + { + Log.Fatal($"Repeated RegisterPool: {objectName}"); + } + else + { + _poolDictionary.Add(objectName, new PoolData(ret, this.gameObject, capacity)); + } + } + + public void RegisterPool(GameObject ret, int capacity = 100) + { + if (_poolDictionary.ContainsKey(ret.name)) + { + Log.Fatal($"Repeated RegisterPool: {ret.name}"); + } + else + { + _poolDictionary.Add(ret.name, new PoolData(ret, this.gameObject, capacity)); + } + } + + public GameObject Get(string objectName, Action callback = null) + { + GameObject ret = null; + + if (_poolDictionary.ContainsKey(objectName) && _poolDictionary[objectName].Count > 0) + { + ret = _poolDictionary[objectName].Get(); + } + else + { + ret = UnityEngine.Object.Instantiate(Resources.Load(objectName)); + ret.name = objectName; + } + + callback?.Invoke(ret); + + return ret; + } + + public void GetAsync(string objectName, Action callback) + { + if (_poolDictionary.ContainsKey(objectName) && _poolDictionary[objectName].Count > 0) + { + callback(_poolDictionary[objectName].Get()); + } + else + { + TResources.LoadAsync(objectName, ret => + { + ret.name = objectName; + + callback(ret); + }); + } + } + + public void Push(string objectName, GameObject ret) + { + if (_poolDictionary.ContainsKey(objectName)) + { + _poolDictionary[objectName].Push(ret); + } + else + { + _poolDictionary.Add(objectName, new PoolData(ret, this.gameObject)); + } + } + + public void Push(GameObject ret) + { + if (_poolDictionary.ContainsKey(ret.name)) + { + _poolDictionary[ret.name].Push(ret); + } + else + { + _poolDictionary.Add(ret.name, new PoolData(ret, this.gameObject)); + } + } + + public void Release(string objectName) + { + if (_poolDictionary.ContainsKey(objectName)) + { + var poolData = _poolDictionary[objectName]; + + poolData.Release(); + + _poolDictionary.Remove(objectName); + } + } + + public void Clear() + { + _poolDictionary.Clear(); + } + } +} \ No newline at end of file diff --git a/Assets/TEngine/Scripts/Runtime/GameObjectHelper/GameObjectHelper.cs.meta b/Assets/TEngine/Scripts/Runtime/GameObjectHelper/GameObjectHelper.cs.meta new file mode 100644 index 00000000..471573f6 --- /dev/null +++ b/Assets/TEngine/Scripts/Runtime/GameObjectHelper/GameObjectHelper.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 8f9f638c21714da6869cccb78bbf2742 +timeCreated: 1664246753 \ No newline at end of file diff --git a/Assets/TEngine/Scripts/Runtime/GameObjectHelper/PoolData.cs b/Assets/TEngine/Scripts/Runtime/GameObjectHelper/PoolData.cs new file mode 100644 index 00000000..06815a98 --- /dev/null +++ b/Assets/TEngine/Scripts/Runtime/GameObjectHelper/PoolData.cs @@ -0,0 +1,92 @@ +using System; +using System.Collections.Generic; +using UnityEngine; + +namespace TEngine.Runtime +{ + public class PoolData + { + /// + /// 父节点 + /// + public readonly GameObject ParentObject; + + /// + /// 数据结构栈 + /// + private readonly Stack _stack; + + /// + /// 容量 Capacity + /// + public static int Capacity = 100; + + /// + /// 当前对象池的数目 Count + /// + public int Count => _stack.Count; + + /// + /// 构造PoolData + /// + /// GameObject + /// 对象池根物体 + /// 最大数目 + public PoolData(GameObject gameObject, GameObject rootObject,int capacity = 100) + { + ParentObject = new GameObject(gameObject.name); + + ParentObject.transform.parent = rootObject.transform; + + _stack = new Stack(); + + Capacity = capacity; + + Push(gameObject); + } + + /// + /// 从对象池获取 (弹出栈) + /// + /// + public GameObject Get() + { + GameObject obj = null; + + obj = _stack.Pop(); + + obj.SetActive(true); + + obj.transform.parent = null; + + return obj; + } + + /// + /// 压入对象池 (压入栈) + /// + /// GameObject + /// throw new Exception($"TEngine Pool:{obj.name} Over Max Count:{Count}"); + public void Push(GameObject obj) + { + if (Count > Capacity) + { + throw new Exception($"TEngine Pool:{obj.name} Over Max Count:{Count}"); + } + + obj.SetActive(false); + + _stack.Push(obj); + + obj.transform.parent = ParentObject.transform; + } + + /// + /// 释放池 + /// + public void Release() + { + UnityEngine.Object.Destroy(ParentObject); + } + } +} \ No newline at end of file diff --git a/Assets/TEngine/Scripts/Runtime/GameObjectHelper/PoolData.cs.meta b/Assets/TEngine/Scripts/Runtime/GameObjectHelper/PoolData.cs.meta new file mode 100644 index 00000000..be2f0e00 --- /dev/null +++ b/Assets/TEngine/Scripts/Runtime/GameObjectHelper/PoolData.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: cafbcd53186b4d49a078c7fda02da00e +timeCreated: 1664249234 \ No newline at end of file