SceneOperationHandle.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using UnityEngine.SceneManagement;
  2. namespace YooAsset
  3. {
  4. public class SceneOperationHandle : OperationHandleBase
  5. {
  6. private System.Action<SceneOperationHandle> _callback;
  7. internal string PackageName { set; get; }
  8. internal SceneOperationHandle(ProviderBase provider) : base(provider)
  9. {
  10. }
  11. internal override void InvokeCallback()
  12. {
  13. _callback?.Invoke(this);
  14. }
  15. /// <summary>
  16. /// 完成委托
  17. /// </summary>
  18. public event System.Action<SceneOperationHandle> Completed
  19. {
  20. add
  21. {
  22. if (IsValidWithWarning == false)
  23. throw new System.Exception($"{nameof(SceneOperationHandle)} is invalid");
  24. if (Provider.IsDone)
  25. value.Invoke(this);
  26. else
  27. _callback += value;
  28. }
  29. remove
  30. {
  31. if (IsValidWithWarning == false)
  32. throw new System.Exception($"{nameof(SceneOperationHandle)} is invalid");
  33. _callback -= value;
  34. }
  35. }
  36. /// <summary>
  37. /// 场景对象
  38. /// </summary>
  39. public Scene SceneObject
  40. {
  41. get
  42. {
  43. if (IsValidWithWarning == false)
  44. return new Scene();
  45. return Provider.SceneObject;
  46. }
  47. }
  48. /// <summary>
  49. /// 激活场景(当同时存在多个场景时用于切换激活场景)
  50. /// </summary>
  51. public bool ActivateScene()
  52. {
  53. if (IsValidWithWarning == false)
  54. return false;
  55. if (SceneObject.IsValid() && SceneObject.isLoaded)
  56. {
  57. return SceneManager.SetActiveScene(SceneObject);
  58. }
  59. else
  60. {
  61. YooLogger.Warning($"Scene is invalid or not loaded : {SceneObject.name}");
  62. return false;
  63. }
  64. }
  65. /// <summary>
  66. /// 解除场景加载挂起操作
  67. /// </summary>
  68. public bool UnSuspend()
  69. {
  70. if (IsValidWithWarning == false)
  71. return false;
  72. if (SceneObject.IsValid())
  73. {
  74. if (Provider is DatabaseSceneProvider)
  75. {
  76. var temp = Provider as DatabaseSceneProvider;
  77. return temp.UnSuspendLoad();
  78. }
  79. else if (Provider is BundledSceneProvider)
  80. {
  81. var temp = Provider as BundledSceneProvider;
  82. return temp.UnSuspendLoad();
  83. }
  84. else
  85. {
  86. throw new System.NotImplementedException();
  87. }
  88. }
  89. else
  90. {
  91. YooLogger.Warning($"Scene is invalid : {SceneObject.name}");
  92. return false;
  93. }
  94. }
  95. /// <summary>
  96. /// 是否为主场景
  97. /// </summary>
  98. public bool IsMainScene()
  99. {
  100. if (IsValidWithWarning == false)
  101. return false;
  102. if (Provider is DatabaseSceneProvider)
  103. {
  104. var temp = Provider as DatabaseSceneProvider;
  105. return temp.SceneMode == LoadSceneMode.Single;
  106. }
  107. else if (Provider is BundledSceneProvider)
  108. {
  109. var temp = Provider as BundledSceneProvider;
  110. return temp.SceneMode == LoadSceneMode.Single;
  111. }
  112. else
  113. {
  114. throw new System.NotImplementedException();
  115. }
  116. }
  117. /// <summary>
  118. /// 异步卸载子场景
  119. /// </summary>
  120. public UnloadSceneOperation UnloadAsync()
  121. {
  122. // 如果句柄无效
  123. if (IsValidWithWarning == false)
  124. {
  125. string error = $"{nameof(SceneOperationHandle)} is invalid.";
  126. var operation = new UnloadSceneOperation(error);
  127. OperationSystem.StartOperation(operation);
  128. return operation;
  129. }
  130. // 如果是主场景
  131. if (IsMainScene())
  132. {
  133. string error = $"Cannot unload main scene. Use {nameof(YooAssets.LoadSceneAsync)} method to change the main scene !";
  134. YooLogger.Error(error);
  135. var operation = new UnloadSceneOperation(error);
  136. OperationSystem.StartOperation(operation);
  137. return operation;
  138. }
  139. // 卸载子场景
  140. Scene sceneObject = SceneObject;
  141. Provider.Impl.UnloadSubScene(Provider);
  142. {
  143. var operation = new UnloadSceneOperation(sceneObject);
  144. OperationSystem.StartOperation(operation);
  145. return operation;
  146. }
  147. }
  148. }
  149. }