EP 实验性框架
This commit is contained in:
@@ -1,69 +1,333 @@
|
||||
using Convention.Experimental.PublicType;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Convention.Experimental.Modules
|
||||
{
|
||||
public class ObjectPoolManager : PublicType.GameModule
|
||||
public class GameObjectPoolManager : GameModule
|
||||
{
|
||||
public interface IPawn
|
||||
/// <summary>
|
||||
/// <para><3E>̳иýӿڵ<D3BF>Component<6E><74><EFBFBD>ڻ<EFBFBD><DABB>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><see cref="Release"/></para>
|
||||
/// <para><b><see cref="Release"/><3E>н<EFBFBD>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD><see cref="Unspawn(GameObject)"/></b></para>
|
||||
/// </summary>
|
||||
public interface IPoolPawn
|
||||
{
|
||||
void Release();
|
||||
}
|
||||
|
||||
private readonly Dictionary<GameObject, Stack<GameObject>> ObjectPools = new();
|
||||
private readonly Dictionary<GameObject, GameObject> LeavePoolObjects = new();
|
||||
private readonly Dictionary<GameObject, Stack<GameObject>> GameObjectPools = new();
|
||||
private readonly Dictionary<GameObject, GameObject> LeaveGameObjectPoolObjects = new();
|
||||
private bool GameObjectPoolStatus = true;
|
||||
|
||||
/// <summary>
|
||||
/// <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϸ<EFBFBD><CFB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
/// </summary>
|
||||
/// <param name="prefab"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԥ<EFBFBD><D4A4><EFBFBD><EFBFBD></param>
|
||||
/// <returns></returns>
|
||||
public bool Contains(GameObject prefab)
|
||||
{
|
||||
return GameObjectPools.ContainsKey(prefab);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ȷ<><C8B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƕ<EFBFBD><EFBFBD><D7B5><EFBFBD>
|
||||
/// </summary>
|
||||
/// <exception cref="GameException"></exception>
|
||||
private void EnsureSafeStatus()
|
||||
{
|
||||
if (GameObjectPoolStatus == false)
|
||||
{
|
||||
throw new GameException("GameObjectPool is busy now.");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϸ<EFBFBD><CFB7><EFBFBD><EFBFBD>
|
||||
/// </summary>
|
||||
/// <param name="prefab"></param>
|
||||
/// <returns></returns>
|
||||
public GameObject Spawn(GameObject prefab)
|
||||
{
|
||||
if (!ObjectPools.ContainsKey(prefab))
|
||||
EnsureSafeStatus();
|
||||
lock (GameObjectPools)
|
||||
{
|
||||
ObjectPools[prefab] = new();
|
||||
if (!GameObjectPools.ContainsKey(prefab))
|
||||
{
|
||||
GameObjectPools[prefab] = new();
|
||||
}
|
||||
|
||||
var pool = GameObjectPools[prefab];
|
||||
GameObject instance;
|
||||
if (pool.Count > 0)
|
||||
{
|
||||
instance = pool.Pop();
|
||||
instance.SetActive(true);
|
||||
instance.transform.SetParent(null);
|
||||
}
|
||||
else
|
||||
{
|
||||
instance = GameObject.Instantiate(prefab);
|
||||
}
|
||||
|
||||
lock (LeaveGameObjectPoolObjects)
|
||||
{
|
||||
LeaveGameObjectPoolObjects[instance] = prefab;
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
var pool = ObjectPools[prefab];
|
||||
GameObject instance;
|
||||
if (pool.Count > 0)
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϸ<EFBFBD><CFB7><EFBFBD><EFBFBD>
|
||||
/// </summary>
|
||||
/// <param name="instance"></param>
|
||||
/// <exception cref="PublicType.GameException"></exception>
|
||||
public void Unspawn(GameObject instance)
|
||||
{
|
||||
EnsureSafeStatus();
|
||||
lock (LeaveGameObjectPoolObjects)
|
||||
{
|
||||
instance = pool.Pop();
|
||||
instance.SetActive(true);
|
||||
instance.transform.SetParent(null);
|
||||
if (LeaveGameObjectPoolObjects.TryGetValue(instance, out var prefab))
|
||||
{
|
||||
var releaser = instance.GetComponents<IPoolPawn>();
|
||||
GameObjectPoolStatus = false;
|
||||
foreach (var r in releaser)
|
||||
{
|
||||
r.Release();
|
||||
}
|
||||
GameObjectPoolStatus = true;
|
||||
instance.SetActive(false);
|
||||
instance.transform.SetParent(ConventionUtility.Singleton.transform);
|
||||
lock (GameObjectPools)
|
||||
{
|
||||
GameObjectPools[prefab].Push(instance);
|
||||
LeaveGameObjectPoolObjects.Remove(instance);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new PublicType.GameException("This object does not belong to any pool.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Clear(GameObject prefab)
|
||||
{
|
||||
EnsureSafeStatus();
|
||||
lock (GameObjectPools)
|
||||
{
|
||||
lock (LeaveGameObjectPoolObjects)
|
||||
{
|
||||
// <20><><EFBFBD>ٳ<EFBFBD><D9B3>ж<EFBFBD><D0B6><EFBFBD>
|
||||
if (GameObjectPools.TryGetValue(prefab, out var pool))
|
||||
{
|
||||
while (pool.Count > 0)
|
||||
{
|
||||
var instance = pool.Pop();
|
||||
GameObject.Destroy(instance);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new PublicType.GameException("This pool does not exist.");
|
||||
}
|
||||
// <20><><EFBFBD><EFBFBD>δ<EFBFBD><CEB4><EFBFBD>ն<EFBFBD><D5B6><EFBFBD>
|
||||
var unbackInstances = from item in LeaveGameObjectPoolObjects
|
||||
where item.Value == prefab
|
||||
select item.Key;
|
||||
foreach (var instance in unbackInstances)
|
||||
{
|
||||
LeaveGameObjectPoolObjects.Remove(instance);
|
||||
}
|
||||
foreach (var instance in unbackInstances)
|
||||
{
|
||||
GameObject.Destroy(instance);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class ObjectPoolManager
|
||||
{
|
||||
/// <summary>
|
||||
/// <para><3E>̳иýӿڵ<D3BF>Component<6E><74><EFBFBD>ڻ<EFBFBD><DABB>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><see cref="Release"/></para>
|
||||
/// <para><b><see cref="Release"/><3E>н<EFBFBD>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD><see cref="Reclaim(object)"/></b></para>
|
||||
/// </summary>
|
||||
public interface IPoolPawn
|
||||
{
|
||||
void Release();
|
||||
}
|
||||
|
||||
private readonly Dictionary<Type, object> ObjectPools = new();
|
||||
private readonly Dictionary<object, Type> LeaveObjectPoolObjects = new();
|
||||
|
||||
private bool GameObjectPoolStatus = true;
|
||||
private void EnsureSafeStatus()
|
||||
{
|
||||
if (GameObjectPoolStatus == false)
|
||||
{
|
||||
throw new GameException("ObjectPool is busy now.");
|
||||
}
|
||||
}
|
||||
|
||||
public bool Contains(Type type)
|
||||
{
|
||||
return ObjectPools.ContainsKey(type);
|
||||
}
|
||||
|
||||
public bool Contains<T>() where T : class, new()
|
||||
{
|
||||
return ObjectPools.ContainsKey(typeof(T));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD>Ķ<EFBFBD><C4B6><EFBFBD><F3B5BDB6><EFBFBD><EFBFBD><EFBFBD>
|
||||
/// </summary>
|
||||
/// <param name="type"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
|
||||
/// <param name="value">ʵ<><CAB5></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="GameException"></exception>
|
||||
public object Insert(Type type, object value)
|
||||
{
|
||||
if(LeaveObjectPoolObjects.ContainsKey(value))
|
||||
{
|
||||
throw new PublicType.GameException("This object is already registered in pool");
|
||||
}
|
||||
if (!ObjectPools.ContainsKey(type))
|
||||
{
|
||||
ObjectPools[type] = Utility.CreateStack(type);
|
||||
}
|
||||
var pool = ObjectPools[type];
|
||||
var methodInfo = pool.GetType().GetMethod("Push");
|
||||
methodInfo.Invoke(pool, new object[] { value });
|
||||
return value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <20><><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><F3B2A2B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
/// </summary>
|
||||
/// <typeparam name="T"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></typeparam>
|
||||
/// <param name="value">ʵ<><CAB5></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="PublicType.GameException"></exception>
|
||||
public T Insert<T>(T value) where T : class
|
||||
{
|
||||
if(LeaveObjectPoolObjects.ContainsKey(value))
|
||||
{
|
||||
throw new PublicType.GameException("This object is already registered in pool");
|
||||
}
|
||||
var type = typeof(T);
|
||||
if (!ObjectPools.ContainsKey(type))
|
||||
{
|
||||
ObjectPools[type] = new Stack<T>();
|
||||
}
|
||||
var pool = (Stack<T>)ObjectPools[type];
|
||||
pool.Push(value);
|
||||
return value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><F3B2A2B7><EFBFBD>ʵ<EFBFBD><CAB5>
|
||||
/// </summary>
|
||||
/// <param name="type"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
|
||||
/// <returns></returns>
|
||||
public object Summon(Type type)
|
||||
{
|
||||
if (!ObjectPools.ContainsKey(type))
|
||||
{
|
||||
ObjectPools[type] = Utility.CreateStack(type);
|
||||
}
|
||||
var pool = ObjectPools[type];
|
||||
var methodInfo = pool.GetType().GetMethod("Pop");
|
||||
object instance;
|
||||
var countProperty = pool.GetType().GetProperty("Count");
|
||||
var count = (int)countProperty.GetValue(pool);
|
||||
if (count > 0)
|
||||
{
|
||||
instance = methodInfo.Invoke(pool, null);
|
||||
}
|
||||
else
|
||||
{
|
||||
instance = GameObject.Instantiate(prefab);
|
||||
instance = Activator.CreateInstance(type);
|
||||
}
|
||||
LeavePoolObjects[instance] = prefab;
|
||||
LeaveObjectPoolObjects[instance] = type;
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void BackPool(GameObject instance)
|
||||
/// <summary>
|
||||
/// <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><F3B2A2B7><EFBFBD>ʵ<EFBFBD><CAB5>
|
||||
/// </summary>
|
||||
/// <typeparam name="T"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></typeparam>
|
||||
/// <returns></returns>
|
||||
public T Summon<T>() where T : class, new()
|
||||
{
|
||||
if(LeavePoolObjects.TryGetValue(instance,out var prefab))
|
||||
var type = typeof(T);
|
||||
if (!ObjectPools.ContainsKey(type))
|
||||
{
|
||||
var releaser = instance.GetComponents<IPawn>();
|
||||
foreach (var r in releaser)
|
||||
{
|
||||
r.Release();
|
||||
}
|
||||
instance.SetActive(false);
|
||||
instance.transform.SetParent(ConventionUtility.Singleton.transform);
|
||||
ObjectPools[prefab].Push(instance);
|
||||
LeavePoolObjects.Remove(instance);
|
||||
ObjectPools[type] = new Stack<T>();
|
||||
}
|
||||
var pool = (Stack<T>)ObjectPools[type];
|
||||
T instance;
|
||||
if (pool.Count > 0)
|
||||
{
|
||||
instance = pool.Pop();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new PublicType.GameException("This object does not belong to any pool.");
|
||||
instance = new T();
|
||||
}
|
||||
LeaveObjectPoolObjects[instance] = type;
|
||||
return instance;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <20><><EFBFBD>ն<EFBFBD><D5B6><EFBFBD>
|
||||
/// </summary>
|
||||
/// <param name="instance">ʵ<><CAB5></param>
|
||||
/// <exception cref="PublicType.GameException"></exception>
|
||||
public void Reclaim(object instance)
|
||||
{
|
||||
if (LeaveObjectPoolObjects.TryGetValue(instance, out var objType))
|
||||
{
|
||||
var pool = ObjectPools[objType];
|
||||
var methodInfo = pool.GetType().GetMethod("Push");
|
||||
if(instance is IPoolPawn pawn)
|
||||
{
|
||||
pawn.Release();
|
||||
}
|
||||
methodInfo.Invoke(pool, new object[] { instance });
|
||||
LeaveObjectPoolObjects.Remove(instance);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new GameException("This object does not belong to any pool.");
|
||||
}
|
||||
}
|
||||
|
||||
public void ClearPool(GameObject prefab)
|
||||
public void Clear(Type type)
|
||||
{
|
||||
if (ObjectPools.TryGetValue(prefab, out var pool))
|
||||
if (ObjectPools.TryGetValue(type, out var pool))
|
||||
{
|
||||
while (pool.Count > 0)
|
||||
{
|
||||
var instance = pool.Pop();
|
||||
GameObject.Destroy(instance);
|
||||
}
|
||||
var clearMethod = pool.GetType().GetMethod("Clear");
|
||||
clearMethod.Invoke(pool, null);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new PublicType.GameException("This pool does not exist.");
|
||||
}
|
||||
}
|
||||
|
||||
public void Clear<T>() where T : class, new()
|
||||
{
|
||||
var type = typeof(T);
|
||||
if (ObjectPools.TryGetValue(type, out var pool))
|
||||
{
|
||||
var clearMethod = pool.GetType().GetMethod("Clear");
|
||||
clearMethod.Invoke(pool, null);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user