BundledSceneProvider.cs 2.6 KB

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