Scene.cs 5.7 KB

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