SceneCommands.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using UnityEngine;
  2. using UnityEngine.SceneManagement;
  3. namespace IngameDebugConsole.Commands
  4. {
  5. public class SceneCommands
  6. {
  7. [ConsoleMethod( "scene.load", "Loads a scene" ), UnityEngine.Scripting.Preserve]
  8. public static void LoadScene( string sceneName )
  9. {
  10. LoadSceneInternal( sceneName, false, LoadSceneMode.Single );
  11. }
  12. [ConsoleMethod( "scene.load", "Loads a scene" ), UnityEngine.Scripting.Preserve]
  13. public static void LoadScene( string sceneName, LoadSceneMode mode )
  14. {
  15. LoadSceneInternal( sceneName, false, mode );
  16. }
  17. [ConsoleMethod( "scene.loadasync", "Loads a scene asynchronously" ), UnityEngine.Scripting.Preserve]
  18. public static void LoadSceneAsync( string sceneName )
  19. {
  20. LoadSceneInternal( sceneName, true, LoadSceneMode.Single );
  21. }
  22. [ConsoleMethod( "scene.loadasync", "Loads a scene asynchronously" ), UnityEngine.Scripting.Preserve]
  23. public static void LoadSceneAsync( string sceneName, LoadSceneMode mode )
  24. {
  25. LoadSceneInternal( sceneName, true, mode );
  26. }
  27. private static void LoadSceneInternal( string sceneName, bool isAsync, LoadSceneMode mode )
  28. {
  29. if( SceneManager.GetSceneByName( sceneName ).IsValid() )
  30. {
  31. Debug.Log( "Scene " + sceneName + " is already loaded" );
  32. return;
  33. }
  34. if( isAsync )
  35. SceneManager.LoadSceneAsync( sceneName, mode );
  36. else
  37. SceneManager.LoadScene( sceneName, mode );
  38. }
  39. [ConsoleMethod( "scene.unload", "Unloads a scene" ), UnityEngine.Scripting.Preserve]
  40. public static void UnloadScene( string sceneName )
  41. {
  42. SceneManager.UnloadSceneAsync( sceneName );
  43. }
  44. [ConsoleMethod( "scene.restart", "Restarts the active scene" ), UnityEngine.Scripting.Preserve]
  45. public static void RestartScene()
  46. {
  47. SceneManager.LoadScene( SceneManager.GetActiveScene().name, LoadSceneMode.Single );
  48. }
  49. }
  50. }