12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- using System;
- using System.Collections.Generic;
- namespace XGame.Framework.Map
- {
- internal class MapAssetPool
- {
- private Dictionary<Type, Stack<object>> _viewPool = new Dictionary<Type, Stack<object>>();
- /// <summary>
- /// 获取ReusableObject (需与Recycle成对)
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <returns></returns>
- public TEntityView Acquire<TEntityView>() where TEntityView : class, IEntityView, new()
- {
- Type type = typeof(TEntityView);
- if (_viewPool.ContainsKey(type))
- {
- var stack = _viewPool[type];
- if (stack.Count > 0)
- return stack.Pop() as TEntityView;
- }
- return Activator.CreateInstance<TEntityView>();
- }
- ///// <summary>
- ///// 回收 (需与Acquire成对)
- ///// </summary>
- ///// <param name="obj"></param>
- //public void Recycle<TEntityView>(TEntityView obj) where TEntityView : class, IEntityView, new()
- //{
- // Recycle(obj);
- //}
- /// <summary>
- /// 回收 (需与Acquire成对)
- /// </summary>
- /// <param name="obj"></param>
- public void Recycle(IEntityView obj)
- {
- if (obj == null) return;
- var type = obj.GetType();
- if (!_viewPool.TryGetValue(type, out var stack))
- {
- stack = new Stack<object>();
- _viewPool.Add(type, stack);
- }
- obj.Clear();
- stack.Push(obj);
- }
- }
- }
|