BundledScene.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using UnityEngine.SceneManagement;
  2. namespace VEngine
  3. {
  4. public class BundledScene : Scene
  5. {
  6. protected Dependencies dependencies;
  7. protected override void OnUpdate()
  8. {
  9. if (status == LoadableStatus.DependentLoading)
  10. UpdateDependencies();
  11. else if (status == LoadableStatus.Loading) UpdateLoading();
  12. }
  13. private void UpdateDependencies()
  14. {
  15. if (dependencies == null)
  16. {
  17. Finish("dependencies == null");
  18. return;
  19. }
  20. progress = dependencies.progress * 0.5f;
  21. if (!dependencies.isDone) return;
  22. var assetBundle = dependencies.assetBundle;
  23. if (assetBundle == null)
  24. {
  25. Finish("assetBundle == null");
  26. return;
  27. }
  28. operation = SceneManager.LoadSceneAsync(sceneName, loadSceneMode);
  29. status = LoadableStatus.Loading;
  30. }
  31. protected override void OnUnload()
  32. {
  33. base.OnUnload();
  34. if (dependencies != null)
  35. {
  36. dependencies.Unload();
  37. dependencies = null;
  38. }
  39. }
  40. protected override void OnLoad()
  41. {
  42. PrepareToLoad();
  43. dependencies = new Dependencies
  44. {
  45. pathOrURL = pathOrURL
  46. };
  47. dependencies.Load();
  48. status = LoadableStatus.DependentLoading;
  49. }
  50. internal static Scene Create(string assetPath, bool additive = false)
  51. {
  52. if (!Versions.Contains(assetPath))
  53. return new Scene
  54. {
  55. pathOrURL = assetPath,
  56. loadSceneMode = additive ? LoadSceneMode.Additive : LoadSceneMode.Single
  57. };
  58. return new BundledScene
  59. {
  60. pathOrURL = assetPath,
  61. loadSceneMode = additive ? LoadSceneMode.Additive : LoadSceneMode.Single
  62. };
  63. }
  64. }
  65. }