mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
Simpale GameObject Pool
Simpale GameObject Pool
This commit is contained in:
3
Assets/TEngine/Scripts/Runtime/GameObjectHelper.meta
Normal file
3
Assets/TEngine/Scripts/Runtime/GameObjectHelper.meta
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: ba7d4c23c5874d29badd7e7b01e9914c
|
||||||
|
timeCreated: 1664246673
|
@@ -0,0 +1,112 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace TEngine.Runtime
|
||||||
|
{
|
||||||
|
public class GameObjectHelper : UnitySingleton<GameObjectHelper>
|
||||||
|
{
|
||||||
|
private readonly Dictionary<string, PoolData> _poolDictionary = new Dictionary<string, PoolData>();
|
||||||
|
|
||||||
|
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<GameObject> callback = null)
|
||||||
|
{
|
||||||
|
GameObject ret = null;
|
||||||
|
|
||||||
|
if (_poolDictionary.ContainsKey(objectName) && _poolDictionary[objectName].Count > 0)
|
||||||
|
{
|
||||||
|
ret = _poolDictionary[objectName].Get();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ret = UnityEngine.Object.Instantiate(Resources.Load<GameObject>(objectName));
|
||||||
|
ret.name = objectName;
|
||||||
|
}
|
||||||
|
|
||||||
|
callback?.Invoke(ret);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void GetAsync(string objectName, Action<GameObject> callback)
|
||||||
|
{
|
||||||
|
if (_poolDictionary.ContainsKey(objectName) && _poolDictionary[objectName].Count > 0)
|
||||||
|
{
|
||||||
|
callback(_poolDictionary[objectName].Get());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
TResources.LoadAsync<GameObject>(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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 8f9f638c21714da6869cccb78bbf2742
|
||||||
|
timeCreated: 1664246753
|
92
Assets/TEngine/Scripts/Runtime/GameObjectHelper/PoolData.cs
Normal file
92
Assets/TEngine/Scripts/Runtime/GameObjectHelper/PoolData.cs
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace TEngine.Runtime
|
||||||
|
{
|
||||||
|
public class PoolData
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 父节点
|
||||||
|
/// </summary>
|
||||||
|
public readonly GameObject ParentObject;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 数据结构栈
|
||||||
|
/// </summary>
|
||||||
|
private readonly Stack<GameObject> _stack;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 容量 Capacity
|
||||||
|
/// </summary>
|
||||||
|
public static int Capacity = 100;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 当前对象池的数目 Count
|
||||||
|
/// </summary>
|
||||||
|
public int Count => _stack.Count;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 构造PoolData
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="gameObject">GameObject</param>
|
||||||
|
/// <param name="rootObject">对象池根物体</param>
|
||||||
|
/// <param name="capacity">最大数目</param>
|
||||||
|
public PoolData(GameObject gameObject, GameObject rootObject,int capacity = 100)
|
||||||
|
{
|
||||||
|
ParentObject = new GameObject(gameObject.name);
|
||||||
|
|
||||||
|
ParentObject.transform.parent = rootObject.transform;
|
||||||
|
|
||||||
|
_stack = new Stack<GameObject>();
|
||||||
|
|
||||||
|
Capacity = capacity;
|
||||||
|
|
||||||
|
Push(gameObject);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 从对象池获取 (弹出栈)
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public GameObject Get()
|
||||||
|
{
|
||||||
|
GameObject obj = null;
|
||||||
|
|
||||||
|
obj = _stack.Pop();
|
||||||
|
|
||||||
|
obj.SetActive(true);
|
||||||
|
|
||||||
|
obj.transform.parent = null;
|
||||||
|
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 压入对象池 (压入栈)
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="obj">GameObject</param>
|
||||||
|
/// <exception cref="Exception">throw new Exception($"TEngine Pool:{obj.name} Over Max Count:{Count}");</exception>
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 释放池
|
||||||
|
/// </summary>
|
||||||
|
public void Release()
|
||||||
|
{
|
||||||
|
UnityEngine.Object.Destroy(ParentObject);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: cafbcd53186b4d49a078c7fda02da00e
|
||||||
|
timeCreated: 1664249234
|
Reference in New Issue
Block a user