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> pool = new Dictionary>(); private static Dictionary> cache = new Dictionary>(); public static T TakeOut() 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 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() { args }); } static void AddPool(string name, GameEventArgs args) { if (pool.TryGetValue(name, out var list)) list.Add(args); else pool.Add(name, new List() { args }); } } }