BundledSceneProvider.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 BundledSceneProvider : ProviderBase
  9. {
  10. public readonly LoadSceneMode SceneMode;
  11. private readonly bool _suspendLoad;
  12. private AsyncOperation _asyncOperation;
  13. public BundledSceneProvider(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 (IsDone)
  26. return;
  27. if (_steps == ESteps.None)
  28. {
  29. _steps = ESteps.CheckBundle;
  30. }
  31. // 1. 检测资源包
  32. if (_steps == ESteps.CheckBundle)
  33. {
  34. if (DependBundles.IsDone() == false)
  35. return;
  36. if (OwnerBundle.IsDone() == false)
  37. return;
  38. if (DependBundles.IsSucceed() == false)
  39. {
  40. string error = DependBundles.GetLastError();
  41. InvokeCompletion(error, EOperationStatus.Failed);
  42. return;
  43. }
  44. if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed)
  45. {
  46. string error = OwnerBundle.LastError;
  47. InvokeCompletion(error, EOperationStatus.Failed);
  48. return;
  49. }
  50. _steps = ESteps.Loading;
  51. }
  52. // 2. 加载场景
  53. if (_steps == ESteps.Loading)
  54. {
  55. // 注意:如果场景不存在则返回NULL
  56. _asyncOperation = SceneManager.LoadSceneAsync(MainAssetInfo.AssetPath, SceneMode);
  57. if (_asyncOperation != null)
  58. {
  59. _asyncOperation.allowSceneActivation = !_suspendLoad;
  60. _asyncOperation.priority = 100;
  61. SceneObject = SceneManager.GetSceneAt(SceneManager.sceneCount - 1);
  62. _steps = ESteps.Checking;
  63. }
  64. else
  65. {
  66. string error = $"Failed to load scene : {MainAssetInfo.AssetPath}";
  67. YooLogger.Error(error);
  68. InvokeCompletion(error, EOperationStatus.Failed);
  69. }
  70. }
  71. // 3. 检测加载结果
  72. if (_steps == ESteps.Checking)
  73. {
  74. Progress = _asyncOperation.progress;
  75. if (_asyncOperation.isDone)
  76. {
  77. if (SceneObject.IsValid())
  78. {
  79. InvokeCompletion(string.Empty, EOperationStatus.Succeed);
  80. }
  81. else
  82. {
  83. string error = $"The load scene is invalid : {MainAssetInfo.AssetPath}";
  84. YooLogger.Error(error);
  85. InvokeCompletion(error, EOperationStatus.Failed);
  86. }
  87. }
  88. }
  89. }
  90. /// <summary>
  91. /// 解除场景加载挂起操作
  92. /// </summary>
  93. public bool UnSuspendLoad()
  94. {
  95. if (_asyncOperation == null)
  96. return false;
  97. _asyncOperation.allowSceneActivation = true;
  98. return true;
  99. }
  100. }
  101. }