Search Unity

Want a play button that runs from the beginning of the scene

Discussion in 'Editor & General Support' started by yuki2006, Oct 30, 2020.

  1. yuki2006

    yuki2006

    Joined:
    Oct 11, 2018
    Posts:
    11
    Currently, pressing the play button in the editor will play the currently open scene.

    I think that's fine.

    However, when I build and run it, it runs from the beginning of the scene in the build settings, so I want a button to play from the beginning of the scene to check it.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,727
  3. yuki2006

    yuki2006

    Joined:
    Oct 11, 2018
    Posts:
    11
    @Kurt-Dekker thx comment.

    umm...
    It's good to make an Editor script, but I want it to be bundled originally.

    It takes time to introduce it every time I create a Unity project.
    (Especially for beginners)
     
    User414322 likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,727
    Game development takes time.

    Good luck to you!
     
  5. User414322

    User414322

    Joined:
    Jul 9, 2019
    Posts:
    27
    Since this is now a top result in google, I thought I would share my script for this.

    Place it in a folder named Editor and remember to rename PlaymodeTargetScene.
    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEditor.SceneManagement;
    3. using UnityEngine.SceneManagement;
    4.  
    5. public class PlayFromScene : EditorWindow
    6. {
    7.     private static string PlaymodeTargetScene = "YOUR START-UP SCENE GOES HERE";
    8.     private static string CurrentScene;
    9.  
    10.     [MenuItem("Developer Tools/Play From Start")]
    11.     private static void PlayFromMainMenu()
    12.     {
    13.         if (EditorApplication.isPlaying) { EditorApplication.isPlaying = false; return; }
    14.         if (!EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo()) { return; } // Save current scene if it has unsaved changes
    15.  
    16.         // Remember currently active scene
    17.         CurrentScene = SceneManager.GetActiveScene().path;
    18.  
    19.         // Start playing from the main scene
    20.         if (EditorSceneManager.OpenScene(GetScenePath(PlaymodeTargetScene), OpenSceneMode.Single).IsValid())
    21.         {
    22.             EditorApplication.isPlaying = true;
    23.             EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
    24.         }
    25.         else { UnityEngine.Debug.LogError("Scene not found: " + PlaymodeTargetScene); }
    26.     }
    27.  
    28.     private static void OnPlayModeStateChanged(PlayModeStateChange state)
    29.     {
    30.         if (state == PlayModeStateChange.EnteredEditMode)
    31.         {
    32.             if (!string.IsNullOrEmpty(CurrentScene)) { EditorSceneManager.OpenScene(CurrentScene); }
    33.             EditorApplication.playModeStateChanged -= OnPlayModeStateChanged;
    34.         }
    35.     }
    36.  
    37.     private static string GetScenePath(string sceneName)
    38.     {
    39.         foreach (var scene in EditorBuildSettings.scenes)
    40.         {
    41.             if (scene.path.Contains(sceneName)) { return scene.path; }
    42.         }
    43.         return null;
    44.     }
    45. }
    46.  
    Be warned that the script is not fully functional. For some reason, debug.log and debug.logerror do not work at all (maybe because this is not running off monobehaviour?) and neither does the functionality to return to your previous scene after you exit play mode. If anyone in the future has any fixes, be my guest in posting them.