Scene.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 string ActiveSceneUUID { get { return activeSceneUUID; } }
  22. private static string activeSceneName = "Unnamed";
  23. private static string activeSceneUUID = "";
  24. /// <summary>
  25. /// Returns the root scene object for the current scene.
  26. /// </summary>
  27. public static SceneObject Root
  28. {
  29. get { return Internal_GetRoot(); }
  30. }
  31. /// <summary>
  32. /// Returns the main camera that controls the final render surface that is displayed to the user. If the current
  33. /// scene has no main camera null is returned.
  34. /// </summary>
  35. public static Camera Camera
  36. {
  37. get
  38. {
  39. SceneObject so = Internal_GetMainCameraSO();
  40. if (so == null)
  41. return null;
  42. return so.GetComponent<Camera>();
  43. }
  44. }
  45. /// <summary>
  46. /// Clears all scene objects from the current scene.
  47. /// </summary>
  48. public static void Clear()
  49. {
  50. Internal_ClearScene();
  51. activeSceneUUID = null;
  52. activeSceneName = "Unnamed";
  53. }
  54. /// <summary>
  55. /// Loads a new scene.
  56. /// </summary>
  57. /// <param name="path">Path to the prefab to load.</param>
  58. public static void Load(string path)
  59. {
  60. Clear();
  61. Prefab scene = Internal_LoadScene(path);
  62. SetActive(scene);
  63. }
  64. /// <summary>
  65. /// Sets the currently active scene to the provided scene.
  66. /// </summary>
  67. /// <param name="scene">Scene which to set as active.</param>
  68. internal static void SetActive(Prefab scene)
  69. {
  70. if (scene != null)
  71. {
  72. activeSceneUUID = scene.UUID;
  73. activeSceneName = scene.Name;
  74. }
  75. }
  76. /// <summary>
  77. /// Wrapper around scene name static field because Mono has problems accessing static fields directly.
  78. /// </summary>
  79. /// <returns>Name of the currently active scene.</returns>
  80. private static string GetSceneName()
  81. {
  82. return activeSceneName;
  83. }
  84. /// <summary>
  85. /// Wrapper around scene name static field because Mono has problems accessing static fields directly.
  86. /// </summary>
  87. /// <param name="name">Name of the currently active scene.</param>
  88. private static void SetSceneName(string name)
  89. {
  90. activeSceneName = name;
  91. }
  92. /// <summary>
  93. /// Wrapper around scene UUID static field because Mono has problems accessing static fields directly.
  94. /// </summary>
  95. /// <returns>UUID of the currently active scene.</returns>
  96. private static string GetSceneUUID()
  97. {
  98. return activeSceneUUID;
  99. }
  100. /// <summary>
  101. /// Wrapper around scene UUID static field because Mono has problems accessing static fields directly.
  102. /// </summary>
  103. /// <param name="uuid">UUID of the currently active scene.</param>
  104. private static void SetSceneUUID(string uuid)
  105. {
  106. activeSceneUUID = uuid;
  107. }
  108. [MethodImpl(MethodImplOptions.InternalCall)]
  109. private static extern Prefab Internal_LoadScene(string path);
  110. [MethodImpl(MethodImplOptions.InternalCall)]
  111. private static extern SceneObject Internal_GetRoot();
  112. [MethodImpl(MethodImplOptions.InternalCall)]
  113. private static extern void Internal_ClearScene();
  114. [MethodImpl(MethodImplOptions.InternalCall)]
  115. private static extern SceneObject Internal_GetMainCameraSO();
  116. }
  117. /** @} */
  118. }