HAARFTE/Assets/DemoGame/GameScript/Hotfix/EventArgs/UIHideOrShowEventArgs.cs

67 lines
1.9 KiB
C#

using System;
using System.Collections.Generic;
namespace ZC
{
public class UIHideOrShowEventArgs : GameEventArgs
{
public static readonly int EventId = typeof(UIHideOrShowEventArgs).GetHashCode();
public override int Id => EventId;
public UIType UIType;
public UIHideOrShowEventArgs(UIType uiType)
{
UIType = uiType;
}
}
public static class GameEventPool
{
private static Dictionary<string, List<GameEventArgs>> pool = new Dictionary<string, List<GameEventArgs>>();
private static Dictionary<string, List<GameEventArgs>> cache = new Dictionary<string, List<GameEventArgs>>();
public static T TakeOut<T>() where T : GameEventArgs, new()
{
string name = nameof(T);
T args;
if (pool.TryGetValue(name, out var list) && list.Count > 0)
{
args = (T)list[0];
list.RemoveAt(0);
}
else
{
args = new T();
}
AddCache(name, args);
return args;
}
public static void Put<T>(T args) where T : GameEventArgs
{
string name = nameof(T);
if (cache.TryGetValue(name, out var list))
{
list.Remove(args);
AddPool(name, args);
}
else
{
throw new NullReferenceException();
}
}
static void AddCache(string name, GameEventArgs args)
{
if (cache.TryGetValue(name, out var list)) list.Add(args);
else cache.Add(name, new List<GameEventArgs>() { args });
}
static void AddPool(string name, GameEventArgs args)
{
if (pool.TryGetValue(name, out var list)) list.Add(args);
else pool.Add(name, new List<GameEventArgs>() { args });
}
}
}