DatabaseSceneProvider.cs 2.5 KB

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