Scene.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Runtime.CompilerServices;
  5. namespace BansheeEngine
  6. {
  7. /** @addtogroup Scene
  8. * @{
  9. */
  10. /// <summary>
  11. /// Handles operations with the active scene (level).
  12. /// </summary>
  13. public static class Scene
  14. {
  15. /// <summary>
  16. /// Returns the name of the scene prefab. This is empty if scene hasn't been saved yet.
  17. /// </summary>
  18. public static string ActiveSceneName { get { return activeSceneName; } }
  19. /// <summary>
  20. /// Returns the UUID of the scene prefab. This is empty if scene hasn't been saved yet.
  21. /// </summary>
  22. internal static UUID ActiveSceneUUID { get { return activeSceneUUID; } }
  23. /// <summary>
  24. /// Checks is the loaded scene a generic scene object group, instead of an actual scene.
  25. /// <see cref="Prefab.IsScene"/>.
  26. /// </summary>
  27. internal static bool IsGenericPrefab { get { return isGenericPrefab; } }
  28. private static string activeSceneName = "Unnamed";
  29. private static UUID activeSceneUUID;
  30. private static bool isGenericPrefab = false;
  31. private static RRef<Prefab> activateOnLoadScene;
  32. /// <summary>
  33. /// Returns the root scene object for the current scene.
  34. /// </summary>
  35. public static SceneObject Root
  36. {
  37. get { return Internal_GetRoot(); }
  38. }
  39. /// <summary>
  40. /// Returns the main camera that controls the final render surface that is displayed to the user. If the current
  41. /// scene has no main camera null is returned.
  42. /// </summary>
  43. public static Camera Camera
  44. {
  45. get
  46. {
  47. SceneObject so = Internal_GetMainCameraSO();
  48. if (so == null)
  49. return null;
  50. return so.GetComponent<Camera>();
  51. }
  52. }
  53. /// <summary>
  54. /// Clears all scene objects from the current scene.
  55. /// </summary>
  56. public static void Clear()
  57. {
  58. Internal_ClearScene();
  59. activeSceneUUID = UUID.Empty;
  60. activeSceneName = "Unnamed";
  61. activateOnLoadScene = null;
  62. }
  63. /// <summary>
  64. /// Loads a new scene.
  65. /// </summary>
  66. /// <param name="path">Path to the prefab to load.</param>
  67. /// <returns>Prefab of the scene at the provided path.</returns>
  68. public static Prefab Load(string path)
  69. {
  70. Clear();
  71. Prefab scene = Resources.Load<Prefab>(path);
  72. SetActive(scene);
  73. return scene;
  74. }
  75. /// <summary>
  76. /// Loads a new scene asynchronously.
  77. /// </summary>
  78. /// <param name="path">Path to the prefab to load.</param>
  79. /// <returns>Handle to the prefab of the scene at the provided path.</returns>
  80. public static RRef<Prefab> LoadAsync(string path)
  81. {
  82. Clear();
  83. activateOnLoadScene = Resources.LoadAsync<Prefab>(path);
  84. if(activateOnLoadScene != null && activateOnLoadScene.IsLoaded)
  85. SetActive(activateOnLoadScene.Value);
  86. return activateOnLoadScene;
  87. }
  88. /// <summary>
  89. /// Sets the currently active scene to the provided scene.
  90. /// </summary>
  91. /// <param name="scene">Scene which to set as active.</param>
  92. internal static void SetActive(Prefab scene)
  93. {
  94. activateOnLoadScene = null;
  95. if (scene != null)
  96. {
  97. activeSceneUUID = scene.UUID;
  98. activeSceneName = scene.Name;
  99. isGenericPrefab = !scene.IsScene;
  100. Internal_SetActiveScene(scene.GetCachedPtr());
  101. }
  102. }
  103. /// <summary>
  104. /// Called once per frame by the runtime.
  105. /// </summary>
  106. private static void OnUpdate()
  107. {
  108. if (activateOnLoadScene != null && activateOnLoadScene.IsLoaded)
  109. SetActive(activateOnLoadScene.Value);
  110. }
  111. /// <summary>
  112. /// Wrapper around scene name static field because Mono has problems accessing static fields directly.
  113. /// </summary>
  114. /// <returns>Name of the currently active scene.</returns>
  115. private static string GetSceneName()
  116. {
  117. return activeSceneName;
  118. }
  119. /// <summary>
  120. /// Wrapper around scene name static field because Mono has problems accessing static fields directly.
  121. /// </summary>
  122. /// <param name="name">Name of the currently active scene.</param>
  123. private static void SetSceneName(string name)
  124. {
  125. activeSceneName = name;
  126. }
  127. /// <summary>
  128. /// Wrapper around scene UUID static field because Mono has problems accessing static fields directly.
  129. /// </summary>
  130. /// <returns>UUID of the currently active scene.</returns>
  131. private static UUID GetSceneUUID()
  132. {
  133. return activeSceneUUID;
  134. }
  135. /// <summary>
  136. /// Wrapper around scene UUID static field because Mono has problems accessing static fields directly.
  137. /// </summary>
  138. /// <param name="uuid">UUID of the currently active scene.</param>
  139. private static void SetSceneUUID(UUID uuid)
  140. {
  141. activeSceneUUID = uuid;
  142. }
  143. /// <summary>
  144. /// Wrapper around the isGenericPrefab static field because Mono has problems accessing static fields directly.
  145. /// </summary>
  146. /// <returns>True if the loaded scene a generic scene object group, instead of an actual scene.</returns>
  147. private static bool GetIsGenericPrefab()
  148. {
  149. return isGenericPrefab;
  150. }
  151. /// <summary>
  152. /// Wrapper around scene UUID static field because Mono has problems accessing static fields directly.
  153. /// </summary>
  154. /// <param name="isGenericPrefab">Determines is the loaded scene a generic scene object group, instead of an actual scene.
  155. /// </param>
  156. private static void SetIsGenericPrefab(bool isGenericPrefab)
  157. {
  158. Scene.isGenericPrefab = isGenericPrefab;
  159. }
  160. [MethodImpl(MethodImplOptions.InternalCall)]
  161. private static extern void Internal_SetActiveScene(IntPtr prefab);
  162. [MethodImpl(MethodImplOptions.InternalCall)]
  163. private static extern SceneObject Internal_GetRoot();
  164. [MethodImpl(MethodImplOptions.InternalCall)]
  165. private static extern void Internal_ClearScene();
  166. [MethodImpl(MethodImplOptions.InternalCall)]
  167. private static extern SceneObject Internal_GetMainCameraSO();
  168. }
  169. /** @} */
  170. }