BaseObject.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System.Collections.Generic;
  2. namespace DragonBones
  3. {
  4. /**
  5. * @language zh_CN
  6. * 基础对象。
  7. * @version DragonBones 4.5
  8. */
  9. abstract public class BaseObject
  10. {
  11. private static uint _hashCode = 0;
  12. private static uint _defaultMaxCount = 5000;
  13. private static readonly Dictionary<System.Type, uint> _maxCountMap = new Dictionary<System.Type, uint>();
  14. private static readonly Dictionary<System.Type, List<BaseObject>> _poolsMap = new Dictionary<System.Type, List<BaseObject>>();
  15. private static void _returnObject(BaseObject obj)
  16. {
  17. var classType = obj.GetType();
  18. var maxCount = _maxCountMap.ContainsKey(classType) ? _maxCountMap[classType] : _defaultMaxCount;
  19. var pool = _poolsMap.ContainsKey(classType) ? _poolsMap[classType] : _poolsMap[classType] = new List<BaseObject>();
  20. if (pool.Count < maxCount)
  21. {
  22. if (!pool.Contains(obj))
  23. {
  24. pool.Add(obj);
  25. }
  26. else
  27. {
  28. DragonBones.Assert(false, DragonBones.ARGUMENT_ERROR);
  29. }
  30. }
  31. }
  32. /**
  33. * @language zh_CN
  34. * 设置每种对象池的最大缓存数量。
  35. * @param classType 对象类型。
  36. * @param maxCount 最大缓存数量。 (设置为 0 则不缓存)
  37. * @version DragonBones 4.5
  38. */
  39. public static void SetMaxCount(System.Type classType, uint maxCount)
  40. {
  41. if (classType != null)
  42. {
  43. _maxCountMap[classType] = maxCount;
  44. if (_poolsMap.ContainsKey(classType))
  45. {
  46. var pool = _poolsMap[classType];
  47. if (pool.Count > maxCount)
  48. {
  49. DragonBones.ResizeList(pool, (int)maxCount, null);
  50. }
  51. }
  52. }
  53. else
  54. {
  55. _defaultMaxCount = maxCount;
  56. foreach (var pair in _poolsMap)
  57. {
  58. if (!_maxCountMap.ContainsKey(pair.Key))
  59. {
  60. continue;
  61. }
  62. _maxCountMap[pair.Key] = maxCount;
  63. var pool = _poolsMap[pair.Key];
  64. if (pool.Count > maxCount)
  65. {
  66. DragonBones.ResizeList(pool, (int)maxCount, null);
  67. }
  68. }
  69. }
  70. }
  71. /**
  72. * @language zh_CN
  73. * 清除对象池缓存的对象。
  74. * @param classType 对象类型。 (不设置则清除所有缓存)
  75. * @version DragonBones 4.5
  76. */
  77. public static void ClearPool(System.Type classType)
  78. {
  79. if (classType != null)
  80. {
  81. if (_poolsMap.ContainsKey(classType))
  82. {
  83. var pool = _poolsMap[classType];
  84. if (pool.Count > 0)
  85. {
  86. pool.Clear();
  87. }
  88. }
  89. }
  90. else
  91. {
  92. foreach (var pair in _poolsMap)
  93. {
  94. var pool = _poolsMap[pair.Key];
  95. if (pool.Count > 0)
  96. {
  97. pool.Clear();
  98. }
  99. }
  100. }
  101. }
  102. /**
  103. * @language zh_CN
  104. * 从对象池中创建指定对象。
  105. * @param objectConstructor 对象类。
  106. * @version DragonBones 4.5
  107. */
  108. public static T BorrowObject<T>() where T : BaseObject, new()
  109. {
  110. var type = typeof(T);
  111. var pool = _poolsMap.ContainsKey(type) ? _poolsMap[type] : null;
  112. if (pool != null && pool.Count > 0)
  113. {
  114. var index = pool.Count - 1;
  115. var obj = pool[index];
  116. pool.RemoveAt(index);
  117. return (T)obj;
  118. }
  119. else
  120. {
  121. var obj = new T();
  122. obj._onClear();
  123. return obj;
  124. }
  125. }
  126. /**
  127. * @language zh_CN
  128. * 对象的唯一标识。
  129. * @version DragonBones 4.5
  130. */
  131. public uint hashCode = _hashCode++;
  132. protected BaseObject()
  133. {
  134. }
  135. /**
  136. * @private
  137. */
  138. abstract protected void _onClear();
  139. /**
  140. * @language zh_CN
  141. * 清除数据并返还对象池。
  142. * @version DragonBones 4.5
  143. */
  144. public void ReturnToPool()
  145. {
  146. _onClear();
  147. _returnObject(this);
  148. }
  149. }
  150. }