Skip to content

Instantly share code, notes, and snippets.

@nicoplv
Last active June 20, 2018 13:14
Show Gist options
  • Save nicoplv/0ba7924abe82356d9bbcbf119c0a4c7f to your computer and use it in GitHub Desktop.
Save nicoplv/0ba7924abe82356d9bbcbf119c0a4c7f to your computer and use it in GitHub Desktop.
Editor script to make Play button always start a main scene in Unity 3D (works only with 2017.2 or +)
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
namespace SmartEditors
{
[InitializeOnLoad]
public class PlayFromScene : Editor
{
[MenuItem("Tools/Editor/Play From Scene/Set Main Scene", false, 0)]
public static void SetScene()
{
SceneAsset playModeStartScene = AssetDatabase.LoadAssetAtPath<SceneAsset>(EditorSceneManager.GetActiveScene().path);
if (playModeStartScene != null)
EditorSceneManager.playModeStartScene = playModeStartScene;
else
Debug.Log("Scene not saved!");
}
[MenuItem("Tools/Editor/Play From Scene/Unset Main Scene", false, 1)]
public static void UnsetScene()
{
EditorSceneManager.playModeStartScene = null;
}
[MenuItem("Tools/Editor/Play From Scene/Unset Main Scene", true, 1)]
public static bool UnsetSceneValidate()
{
return EditorSceneManager.playModeStartScene != null;
}
}
}
@dechowdev
Copy link

Ill test this on 2017.1 - Unless it have been confirmed not to work on that version ? :)

@nicoplv
Copy link
Author

nicoplv commented Dec 20, 2017

@Dechowmedia it's work only with 2017.2 or +
EditorSceneManager.playModeStartScene doesn't exist before

@dechowdev
Copy link

Actually, I tested it and it does work indeed :)
The API change was added in 2017.1

https://docs.unity3d.com/2017.1/Documentation/ScriptReference/SceneManagement.EditorSceneManager-playModeStartScene.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment