Scene.cs 4.4 KB

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