InstantiateOperation.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using UnityEngine;
  2. namespace YooAsset
  3. {
  4. public sealed class InstantiateOperation : AsyncOperationBase
  5. {
  6. private enum ESteps
  7. {
  8. None,
  9. Clone,
  10. Done,
  11. }
  12. private readonly AssetOperationHandle _handle;
  13. private readonly bool _setPositionAndRotation;
  14. private readonly Vector3 _position;
  15. private readonly Quaternion _rotation;
  16. private readonly Transform _parent;
  17. private readonly bool _worldPositionStays;
  18. private ESteps _steps = ESteps.None;
  19. /// <summary>
  20. /// 实例化的游戏对象
  21. /// </summary>
  22. public GameObject Result = null;
  23. internal InstantiateOperation(AssetOperationHandle handle, bool setPositionAndRotation, Vector3 position, Quaternion rotation, Transform parent, bool worldPositionStays)
  24. {
  25. _handle = handle;
  26. _setPositionAndRotation = setPositionAndRotation;
  27. _position = position;
  28. _rotation = rotation;
  29. _parent = parent;
  30. _worldPositionStays = worldPositionStays;
  31. }
  32. internal override void Start()
  33. {
  34. _steps = ESteps.Clone;
  35. }
  36. internal override void Update()
  37. {
  38. if (_steps == ESteps.None || _steps == ESteps.Done)
  39. return;
  40. if (_steps == ESteps.Clone)
  41. {
  42. if (_handle.IsValidWithWarning == false)
  43. {
  44. _steps = ESteps.Done;
  45. Status = EOperationStatus.Failed;
  46. Error = $"{nameof(AssetOperationHandle)} is invalid.";
  47. return;
  48. }
  49. if (_handle.IsDone == false)
  50. return;
  51. if (_handle.AssetObject == null)
  52. {
  53. _steps = ESteps.Done;
  54. Status = EOperationStatus.Failed;
  55. Error = $"{nameof(AssetOperationHandle.AssetObject)} is null.";
  56. return;
  57. }
  58. // 实例化游戏对象
  59. Result = InstantiateInternal(_handle.AssetObject, _setPositionAndRotation, _position, _rotation, _parent, _worldPositionStays);
  60. _steps = ESteps.Done;
  61. Status = EOperationStatus.Succeed;
  62. }
  63. }
  64. /// <summary>
  65. /// 取消实例化对象操作
  66. /// </summary>
  67. public void Cancel()
  68. {
  69. if (IsDone == false)
  70. {
  71. _steps = ESteps.Done;
  72. Status = EOperationStatus.Failed;
  73. Error = $"User cancelled !";
  74. }
  75. }
  76. /// <summary>
  77. /// 等待异步实例化结束
  78. /// </summary>
  79. public void WaitForAsyncComplete()
  80. {
  81. if (_steps == ESteps.Done)
  82. return;
  83. _handle.WaitForAsyncComplete();
  84. Update();
  85. }
  86. internal static GameObject InstantiateInternal(UnityEngine.Object assetObject, bool setPositionAndRotation, Vector3 position, Quaternion rotation, Transform parent, bool worldPositionStays)
  87. {
  88. if (assetObject == null)
  89. return null;
  90. if (setPositionAndRotation)
  91. {
  92. if (parent != null)
  93. {
  94. GameObject clone = UnityEngine.Object.Instantiate(assetObject as GameObject, position, rotation, parent);
  95. return clone;
  96. }
  97. else
  98. {
  99. GameObject clone = UnityEngine.Object.Instantiate(assetObject as GameObject, position, rotation);
  100. return clone;
  101. }
  102. }
  103. else
  104. {
  105. if (parent != null)
  106. {
  107. GameObject clone = UnityEngine.Object.Instantiate(assetObject as GameObject, parent, worldPositionStays);
  108. return clone;
  109. }
  110. else
  111. {
  112. GameObject clone = UnityEngine.Object.Instantiate(assetObject as GameObject);
  113. return clone;
  114. }
  115. }
  116. }
  117. }
  118. }