GameObjectPool.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using YooAsset;
  6. namespace UniFramework.Pooling
  7. {
  8. internal class GameObjectPool
  9. {
  10. private readonly GameObject _root;
  11. private readonly Queue<InstantiateOperation> _cacheOperations;
  12. private readonly bool _dontDestroy;
  13. private readonly int _initCapacity;
  14. private readonly int _maxCapacity;
  15. private readonly float _destroyTime;
  16. private float _lastRestoreRealTime = -1f;
  17. /// <summary>
  18. /// 资源句柄
  19. /// </summary>
  20. public AssetOperationHandle AssetHandle { private set; get; }
  21. /// <summary>
  22. /// 资源定位地址
  23. /// </summary>
  24. public string Location { private set; get; }
  25. /// <summary>
  26. /// 内部缓存总数
  27. /// </summary>
  28. public int CacheCount
  29. {
  30. get { return _cacheOperations.Count; }
  31. }
  32. /// <summary>
  33. /// 外部使用总数
  34. /// </summary>
  35. public int SpawnCount { private set; get; } = 0;
  36. /// <summary>
  37. /// 是否常驻不销毁
  38. /// </summary>
  39. public bool DontDestroy
  40. {
  41. get { return _dontDestroy; }
  42. }
  43. public GameObjectPool(GameObject poolingRoot, string location, bool dontDestroy, int initCapacity, int maxCapacity, float destroyTime)
  44. {
  45. _root = new GameObject(location);
  46. _root.transform.parent = poolingRoot.transform;
  47. Location = location;
  48. _dontDestroy = dontDestroy;
  49. _initCapacity = initCapacity;
  50. _maxCapacity = maxCapacity;
  51. _destroyTime = destroyTime;
  52. // 创建缓存池
  53. _cacheOperations = new Queue<InstantiateOperation>(initCapacity);
  54. }
  55. /// <summary>
  56. /// 创建对象池
  57. /// </summary>
  58. public void CreatePool(ResourcePackage package)
  59. {
  60. // 加载游戏对象
  61. AssetHandle = package.LoadAssetAsync<GameObject>(Location);
  62. // 创建初始对象
  63. for (int i = 0; i < _initCapacity; i++)
  64. {
  65. var operation = AssetHandle.InstantiateAsync(_root.transform);
  66. operation.Completed += Operation_Completed;
  67. _cacheOperations.Enqueue(operation);
  68. }
  69. }
  70. private void Operation_Completed(AsyncOperationBase obj)
  71. {
  72. if (obj.Status == EOperationStatus.Succeed)
  73. {
  74. var op = obj as InstantiateOperation;
  75. if (op.Result != null)
  76. op.Result.SetActive(false);
  77. }
  78. }
  79. /// <summary>
  80. /// 销毁游戏对象池
  81. /// </summary>
  82. public void DestroyPool()
  83. {
  84. // 卸载资源对象
  85. AssetHandle.Release();
  86. AssetHandle = null;
  87. // 销毁游戏对象
  88. GameObject.Destroy(_root);
  89. _cacheOperations.Clear();
  90. SpawnCount = 0;
  91. }
  92. /// <summary>
  93. /// 查询静默时间内是否可以销毁
  94. /// </summary>
  95. public bool CanAutoDestroy()
  96. {
  97. if (_dontDestroy)
  98. return false;
  99. if (_destroyTime < 0)
  100. return false;
  101. if (_lastRestoreRealTime > 0 && SpawnCount <= 0)
  102. return (Time.realtimeSinceStartup - _lastRestoreRealTime) > _destroyTime;
  103. else
  104. return false;
  105. }
  106. /// <summary>
  107. /// 游戏对象池是否已经销毁
  108. /// </summary>
  109. public bool IsDestroyed()
  110. {
  111. return AssetHandle == null;
  112. }
  113. /// <summary>
  114. /// 回收
  115. /// </summary>
  116. public void Restore(InstantiateOperation operation)
  117. {
  118. if (IsDestroyed())
  119. {
  120. DestroyInstantiateOperation(operation);
  121. return;
  122. }
  123. SpawnCount--;
  124. if (SpawnCount <= 0)
  125. _lastRestoreRealTime = Time.realtimeSinceStartup;
  126. // 如果外部逻辑销毁了游戏对象
  127. if (operation.Status == EOperationStatus.Succeed)
  128. {
  129. if (operation.Result == null)
  130. return;
  131. }
  132. // 如果缓存池还未满员
  133. if (_cacheOperations.Count < _maxCapacity)
  134. {
  135. SetRestoreGameObject(operation.Result);
  136. _cacheOperations.Enqueue(operation);
  137. }
  138. else
  139. {
  140. DestroyInstantiateOperation(operation);
  141. }
  142. }
  143. /// <summary>
  144. /// 丢弃
  145. /// </summary>
  146. public void Discard(InstantiateOperation operation)
  147. {
  148. if (IsDestroyed())
  149. {
  150. DestroyInstantiateOperation(operation);
  151. return;
  152. }
  153. SpawnCount--;
  154. if (SpawnCount <= 0)
  155. _lastRestoreRealTime = Time.realtimeSinceStartup;
  156. DestroyInstantiateOperation(operation);
  157. }
  158. /// <summary>
  159. /// 获取一个游戏对象
  160. /// </summary>
  161. public SpawnHandle Spawn(Transform parent, Vector3 position, Quaternion rotation, bool forceClone, params System.Object[] userDatas)
  162. {
  163. InstantiateOperation operation;
  164. if (forceClone == false && _cacheOperations.Count > 0)
  165. operation = _cacheOperations.Dequeue();
  166. else
  167. operation = AssetHandle.InstantiateAsync();
  168. SpawnCount++;
  169. SpawnHandle handle = new SpawnHandle(this, operation, parent, position, rotation, userDatas);
  170. YooAssets.StartOperation(handle);
  171. return handle;
  172. }
  173. private void DestroyInstantiateOperation(InstantiateOperation operation)
  174. {
  175. // 取消异步操作
  176. operation.Cancel();
  177. // 销毁游戏对象
  178. if (operation.Result != null)
  179. {
  180. GameObject.Destroy(operation.Result);
  181. }
  182. }
  183. private void SetRestoreGameObject(GameObject gameObj)
  184. {
  185. if (gameObj != null)
  186. {
  187. gameObj.SetActive(false);
  188. gameObj.transform.SetParent(_root.transform);
  189. gameObj.transform.SetPositionAndRotation(Vector3.zero, Quaternion.identity);
  190. }
  191. }
  192. }
  193. }