GameObjectPool.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. foreach(var t in _cacheOperations)
  90. {
  91. DestroyInstantiateOperation(t);
  92. }
  93. _cacheOperations.Clear();
  94. SpawnCount = 0;
  95. }
  96. /// <summary>
  97. /// 查询静默时间内是否可以销毁
  98. /// </summary>
  99. public bool CanAutoDestroy()
  100. {
  101. if (_dontDestroy)
  102. return false;
  103. if (_destroyTime < 0)
  104. return false;
  105. if (_lastRestoreRealTime > 0 && SpawnCount <= 0)
  106. return (Time.realtimeSinceStartup - _lastRestoreRealTime) > _destroyTime;
  107. else
  108. return false;
  109. }
  110. /// <summary>
  111. /// 游戏对象池是否已经销毁
  112. /// </summary>
  113. public bool IsDestroyed()
  114. {
  115. return AssetHandle == null;
  116. }
  117. /// <summary>
  118. /// 回收
  119. /// </summary>
  120. public void Restore(InstantiateOperation operation)
  121. {
  122. if (IsDestroyed())
  123. {
  124. DestroyInstantiateOperation(operation);
  125. return;
  126. }
  127. SpawnCount--;
  128. if (SpawnCount <= 0)
  129. _lastRestoreRealTime = Time.realtimeSinceStartup;
  130. // 如果外部逻辑销毁了游戏对象
  131. if (operation.Status == EOperationStatus.Succeed)
  132. {
  133. if (operation.Result == null)
  134. return;
  135. }
  136. // 如果缓存池还未满员
  137. if (_cacheOperations.Count < _maxCapacity)
  138. {
  139. SetRestoreGameObject(operation.Result);
  140. _cacheOperations.Enqueue(operation);
  141. }
  142. else
  143. {
  144. DestroyInstantiateOperation(operation);
  145. }
  146. }
  147. /// <summary>
  148. /// 丢弃
  149. /// </summary>
  150. public void Discard(InstantiateOperation operation)
  151. {
  152. if (IsDestroyed())
  153. {
  154. DestroyInstantiateOperation(operation);
  155. return;
  156. }
  157. SpawnCount--;
  158. if (SpawnCount <= 0)
  159. _lastRestoreRealTime = Time.realtimeSinceStartup;
  160. DestroyInstantiateOperation(operation);
  161. }
  162. /// <summary>
  163. /// 获取一个游戏对象
  164. /// </summary>
  165. public SpawnHandle Spawn(Transform parent, Vector3 position, Quaternion rotation, bool forceClone, params System.Object[] userDatas)
  166. {
  167. InstantiateOperation operation;
  168. if (forceClone == false && _cacheOperations.Count > 0)
  169. operation = _cacheOperations.Dequeue();
  170. else
  171. operation = AssetHandle.InstantiateAsync();
  172. SpawnCount++;
  173. SpawnHandle handle = new SpawnHandle(this, operation, parent, position, rotation, userDatas);
  174. YooAssets.StartOperation(handle);
  175. return handle;
  176. }
  177. private void DestroyInstantiateOperation(InstantiateOperation operation)
  178. {
  179. // 取消异步操作
  180. operation.Cancel();
  181. // 销毁游戏对象
  182. if (operation.Result != null)
  183. {
  184. GameObject.Destroy(operation.Result);
  185. }
  186. }
  187. private void SetRestoreGameObject(GameObject gameObj)
  188. {
  189. if (gameObj != null)
  190. {
  191. gameObj.transform.SetParent(_root.transform, false);
  192. //gameObj.transform.SetPositionAndRotation(Vector3.zero, Quaternion.identity);
  193. gameObj.SetActive(false);
  194. }
  195. }
  196. }
  197. }