SpawnHandle.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using UnityEngine;
  2. using YooAsset;
  3. namespace UniFramework.Pooling
  4. {
  5. public sealed class SpawnHandle : GameAsyncOperation
  6. {
  7. private enum ESteps
  8. {
  9. None,
  10. Waiting,
  11. Done,
  12. }
  13. private readonly GameObjectPool _pool;
  14. private InstantiateOperation _operation;
  15. private readonly Transform _parent;
  16. private readonly Vector3 _position;
  17. private readonly Quaternion _rotation;
  18. private ESteps _steps = ESteps.None;
  19. /// <summary>
  20. /// 实例化的游戏对象
  21. /// </summary>
  22. public GameObject GameObj
  23. {
  24. get
  25. {
  26. if (_operation == null)
  27. {
  28. UniLogger.Warning("The spawn handle is invalid !");
  29. return null;
  30. }
  31. return _operation.Result;
  32. }
  33. }
  34. /// <summary>
  35. /// 用户自定义数据集
  36. /// </summary>
  37. public System.Object[] UserDatas { private set; get; }
  38. private SpawnHandle()
  39. {
  40. }
  41. internal SpawnHandle(GameObjectPool pool, InstantiateOperation operation, Transform parent, Vector3 position, Quaternion rotation, params System.Object[] userDatas)
  42. {
  43. _pool = pool;
  44. _operation = operation;
  45. _parent = parent;
  46. _position = position;
  47. _rotation = rotation;
  48. UserDatas = userDatas;
  49. }
  50. protected override void OnStart()
  51. {
  52. _steps = ESteps.Waiting;
  53. }
  54. protected override void OnUpdate()
  55. {
  56. if (_steps == ESteps.None || _steps == ESteps.Done)
  57. return;
  58. if (_steps == ESteps.Waiting)
  59. {
  60. if (_operation.IsDone == false)
  61. return;
  62. if (_operation.Status != EOperationStatus.Succeed)
  63. {
  64. _steps = ESteps.Done;
  65. Status = EOperationStatus.Failed;
  66. Error = _operation.Error;
  67. return;
  68. }
  69. if (_operation.Result == null)
  70. {
  71. _steps = ESteps.Done;
  72. Status = EOperationStatus.Failed;
  73. Error = $"Clone game object is null.";
  74. return;
  75. }
  76. // 设置参数
  77. _operation.Result.transform.SetParent(_parent);
  78. _operation.Result.transform.SetPositionAndRotation(_position, _rotation);
  79. _operation.Result.SetActive(true);
  80. _steps = ESteps.Done;
  81. Status = EOperationStatus.Succeed;
  82. }
  83. }
  84. /// <summary>
  85. /// 回收
  86. /// </summary>
  87. public void Restore()
  88. {
  89. if (_operation != null)
  90. {
  91. ClearCompletedCallback();
  92. CancelHandle();
  93. _pool.Restore(_operation);
  94. _operation = null;
  95. }
  96. }
  97. /// <summary>
  98. /// 丢弃
  99. /// </summary>
  100. public void Discard()
  101. {
  102. if (_operation != null)
  103. {
  104. ClearCompletedCallback();
  105. CancelHandle();
  106. _pool.Discard(_operation);
  107. _operation = null;
  108. }
  109. }
  110. /// <summary>
  111. /// 等待异步实例化结束
  112. /// </summary>
  113. public void WaitForAsyncComplete()
  114. {
  115. if (_operation != null)
  116. {
  117. if (_steps == ESteps.Done)
  118. return;
  119. _operation.WaitForAsyncComplete();
  120. OnUpdate();
  121. }
  122. }
  123. private void CancelHandle()
  124. {
  125. if (IsDone == false)
  126. {
  127. _steps = ESteps.Done;
  128. Status = EOperationStatus.Failed;
  129. Error = $"User cancelled !";
  130. }
  131. }
  132. }
  133. }