DatabaseSceneProvider.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. using UnityEngine.SceneManagement;
  6. namespace YooAsset
  7. {
  8. internal sealed class DatabaseSceneProvider : ProviderBase
  9. {
  10. public readonly LoadSceneMode SceneMode;
  11. private readonly bool _suspendLoad;
  12. private AsyncOperation _asyncOperation;
  13. public DatabaseSceneProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo, LoadSceneMode sceneMode, bool suspendLoad) : base(manager, providerGUID, assetInfo)
  14. {
  15. SceneMode = sceneMode;
  16. SceneName = Path.GetFileNameWithoutExtension(assetInfo.AssetPath);
  17. _suspendLoad = suspendLoad;
  18. }
  19. internal override void InternalOnStart()
  20. {
  21. DebugBeginRecording();
  22. }
  23. internal override void InternalOnUpdate()
  24. {
  25. #if UNITY_EDITOR
  26. if (IsDone)
  27. return;
  28. if (_steps == ESteps.None)
  29. {
  30. _steps = ESteps.CheckBundle;
  31. }
  32. // 1. 检测资源包
  33. if (_steps == ESteps.CheckBundle)
  34. {
  35. if (IsWaitForAsyncComplete)
  36. {
  37. OwnerBundle.WaitForAsyncComplete();
  38. }
  39. if (OwnerBundle.IsDone() == false)
  40. return;
  41. if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed)
  42. {
  43. string error = OwnerBundle.LastError;
  44. InvokeCompletion(error, EOperationStatus.Failed);
  45. return;
  46. }
  47. _steps = ESteps.Loading;
  48. }
  49. // 2. 加载资源对象
  50. if (_steps == ESteps.Loading)
  51. {
  52. LoadSceneParameters loadSceneParameters = new LoadSceneParameters();
  53. loadSceneParameters.loadSceneMode = SceneMode;
  54. _asyncOperation = UnityEditor.SceneManagement.EditorSceneManager.LoadSceneAsyncInPlayMode(MainAssetInfo.AssetPath, loadSceneParameters);
  55. if (_asyncOperation != null)
  56. {
  57. _asyncOperation.allowSceneActivation = !_suspendLoad;
  58. _asyncOperation.priority = 100;
  59. SceneObject = SceneManager.GetSceneAt(SceneManager.sceneCount - 1);
  60. _steps = ESteps.Checking;
  61. }
  62. else
  63. {
  64. string error = $"Failed to load scene : {MainAssetInfo.AssetPath}";
  65. YooLogger.Error(error);
  66. InvokeCompletion(error, EOperationStatus.Failed);
  67. }
  68. }
  69. // 3. 检测加载结果
  70. if (_steps == ESteps.Checking)
  71. {
  72. Progress = _asyncOperation.progress;
  73. if (_asyncOperation.isDone)
  74. {
  75. if (SceneObject.IsValid())
  76. {
  77. InvokeCompletion(string.Empty, EOperationStatus.Succeed);
  78. }
  79. else
  80. {
  81. string error = $"The loaded scene is invalid : {MainAssetInfo.AssetPath}";
  82. YooLogger.Error(error);
  83. InvokeCompletion(error, EOperationStatus.Failed);
  84. }
  85. }
  86. }
  87. #endif
  88. }
  89. /// <summary>
  90. /// 解除场景加载挂起操作
  91. /// </summary>
  92. public bool UnSuspendLoad()
  93. {
  94. if (_asyncOperation == null)
  95. return false;
  96. _asyncOperation.allowSceneActivation = true;
  97. return true;
  98. }
  99. }
  100. }