BundledSceneProvider.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 string _sceneName;
  12. private readonly bool _suspendLoad;
  13. private readonly int _priority;
  14. private AsyncOperation _asyncOperation;
  15. public BundledSceneProvider(AssetSystemImpl impl, string providerGUID, AssetInfo assetInfo, LoadSceneMode sceneMode, bool suspendLoad, int priority) : base(impl, providerGUID, assetInfo)
  16. {
  17. SceneMode = sceneMode;
  18. _sceneName = Path.GetFileNameWithoutExtension(assetInfo.AssetPath);
  19. _suspendLoad = suspendLoad;
  20. _priority = priority;
  21. }
  22. public override void Update()
  23. {
  24. DebugBeginRecording();
  25. if (IsDone)
  26. return;
  27. if (Status == EStatus.None)
  28. {
  29. Status = EStatus.CheckBundle;
  30. }
  31. // 1. 检测资源包
  32. if (Status == EStatus.CheckBundle)
  33. {
  34. if (DependBundles.IsDone() == false)
  35. return;
  36. if (OwnerBundle.IsDone() == false)
  37. return;
  38. if (DependBundles.IsSucceed() == false)
  39. {
  40. Status = EStatus.Failed;
  41. LastError = DependBundles.GetLastError();
  42. InvokeCompletion();
  43. return;
  44. }
  45. if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed)
  46. {
  47. Status = EStatus.Failed;
  48. LastError = OwnerBundle.LastError;
  49. InvokeCompletion();
  50. return;
  51. }
  52. Status = EStatus.Loading;
  53. }
  54. // 2. 加载场景
  55. if (Status == EStatus.Loading)
  56. {
  57. // 注意:如果场景不存在则返回NULL
  58. _asyncOperation = SceneManager.LoadSceneAsync(MainAssetInfo.AssetPath, SceneMode);
  59. if (_asyncOperation != null)
  60. {
  61. _asyncOperation.allowSceneActivation = !_suspendLoad;
  62. _asyncOperation.priority = _priority;
  63. SceneObject = SceneManager.GetSceneAt(SceneManager.sceneCount - 1);
  64. Status = EStatus.Checking;
  65. }
  66. else
  67. {
  68. Status = EStatus.Failed;
  69. LastError = $"Failed to load scene : {_sceneName}";
  70. YooLogger.Error(LastError);
  71. InvokeCompletion();
  72. }
  73. }
  74. // 3. 检测加载结果
  75. if (Status == EStatus.Checking)
  76. {
  77. Progress = _asyncOperation.progress;
  78. if (_asyncOperation.isDone)
  79. {
  80. Status = SceneObject.IsValid() ? EStatus.Succeed : EStatus.Failed;
  81. if (Status == EStatus.Failed)
  82. {
  83. LastError = $"The load scene is invalid : {MainAssetInfo.AssetPath}";
  84. YooLogger.Error(LastError);
  85. }
  86. InvokeCompletion();
  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. }