1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- 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);
- }
- public void Clear()
- {
- _viewPool.Clear();
- }
- }
- }
|