Scene.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using UnityEngine;
  6. using UnityEngine.SceneManagement;
  7. namespace VEngine
  8. {
  9. public class Scene : Loadable, IEnumerator
  10. {
  11. internal static readonly List<Scene> Unused = new List<Scene>();
  12. public static Action<Scene> onSceneUnloaded;
  13. public static Action<Scene> onSceneLoaded;
  14. internal readonly List<Scene> additives = new List<Scene>();
  15. public Action<Scene> completed;
  16. protected string sceneName;
  17. public Action<Scene> updated;
  18. public AsyncOperation operation { get; protected set; }
  19. public static Scene main { get; private set; }
  20. public static Scene current { get; private set; }
  21. protected Scene parent { get; set; }
  22. protected internal LoadSceneMode loadSceneMode { get; set; }
  23. public bool MoveNext()
  24. {
  25. return !isDone;
  26. }
  27. public void Reset()
  28. {
  29. }
  30. public object Current => null;
  31. public static Scene LoadAsync(string assetPath, Action<Scene> completed = null, bool additive = false)
  32. {
  33. if (string.IsNullOrEmpty(assetPath)) throw new ArgumentNullException(nameof(assetPath));
  34. var scene = Versions.CreateScene(assetPath, additive);
  35. if (completed != null) scene.completed += completed;
  36. current = scene;
  37. scene.Load();
  38. return scene;
  39. }
  40. public static Scene LoadAdditiveAsync(string assetPath, Action<Scene> completed = null)
  41. {
  42. return LoadAsync(assetPath, completed, true);
  43. }
  44. protected override void OnUpdate()
  45. {
  46. if (status == LoadableStatus.Loading)
  47. {
  48. UpdateLoading();
  49. if (updated != null) updated(this);
  50. }
  51. }
  52. protected void UpdateLoading()
  53. {
  54. if (operation == null)
  55. {
  56. Finish("operation == null");
  57. return;
  58. }
  59. progress = 0.5f + operation.progress * 0.5f;
  60. if (operation.allowSceneActivation)
  61. {
  62. if (!operation.isDone) return;
  63. }
  64. else
  65. {
  66. // https://docs.unity3d.com/ScriptReference/AsyncOperation-allowSceneActivation.html
  67. if (operation.progress < 0.9f) return;
  68. }
  69. Finish();
  70. }
  71. protected override void OnLoad()
  72. {
  73. PrepareToLoad();
  74. operation = SceneManager.LoadSceneAsync(sceneName, loadSceneMode);
  75. }
  76. protected void PrepareToLoad()
  77. {
  78. sceneName = Path.GetFileNameWithoutExtension(pathOrURL);
  79. if (loadSceneMode == LoadSceneMode.Single)
  80. {
  81. if (main != null)
  82. {
  83. main.Release();
  84. main = null;
  85. }
  86. main = this;
  87. }
  88. else
  89. {
  90. if (main != null)
  91. {
  92. main.additives.Add(this);
  93. parent = main;
  94. }
  95. }
  96. }
  97. protected override void OnUnused()
  98. {
  99. completed = null;
  100. Unused.Add(this);
  101. }
  102. protected override void OnUnload()
  103. {
  104. if (loadSceneMode == LoadSceneMode.Additive)
  105. {
  106. if (main != null) main.additives.Remove(this);
  107. if (parent != null && string.IsNullOrEmpty(error))
  108. SceneManager.UnloadSceneAsync(sceneName);
  109. parent = null;
  110. }
  111. else
  112. {
  113. foreach (var item in additives)
  114. {
  115. item.Release();
  116. item.parent = null;
  117. }
  118. additives.Clear();
  119. }
  120. if (onSceneUnloaded != null) onSceneUnloaded.Invoke(this);
  121. }
  122. protected override void OnComplete()
  123. {
  124. if (onSceneLoaded != null) onSceneLoaded.Invoke(this);
  125. if (completed == null) return;
  126. var saved = completed;
  127. if (completed != null) completed(this);
  128. completed -= saved;
  129. }
  130. public static void UpdateScenes()
  131. {
  132. if (current == null || !current.isDone) return;
  133. for (var index = 0; index < Unused.Count; index++)
  134. {
  135. var item = Unused[index];
  136. if (Updater.Instance.busy) break;
  137. if (!item.isDone) continue;
  138. Unused.RemoveAt(index);
  139. index--;
  140. item.Unload();
  141. }
  142. }
  143. }
  144. }