Unity Scene Management Tips #1 — Play Mode Scene

Irfan Yigit Baysal
2 min readApr 15, 2023

--

Today I’ll demonstrate how to set the scene in Unity to load first when the play button is pressed. It will be really helpful if you like to use the multi-scene structure. We use EditorSceneManager.playModeStartScene method for this example.

Let’s first create EditorWindow script. I called as “StartSceneWindow” and set the desired scene path.

public class StartSceneWindow : EditorWindow
{
private const string ScenePath = "Assets/Core/Scenes/MainScene.unity";
}

You can find the scene path by right-clicking the selected scene and click copy path. I select main scene for this example as a start scene.

After that we can create our “OnGUI” method in order to visualize the scene and related methods.

private void OnGUI()
{
EditorSceneManager.playModeStartScene =
(SceneAsset)EditorGUILayout.ObjectField(new GUIContent("Start Scene"),
EditorSceneManager.playModeStartScene, typeof(SceneAsset),false);

}

Then, we can implement our scene selection method.

private static void SetPlayModeStartScene(string scenePath)
{
var desiredScene = AssetDatabase.LoadAssetAtPath<SceneAsset>(scenePath);
if (desiredScene == null)
Debug.Log("Could not find Scene " + scenePath);
else
EditorSceneManager.playModeStartScene = desiredScene;
}

Finally, create menu item for visualize this editor implementations. Note that your script name must match with “<StartSceneWindow>”.

[MenuItem("Baysz /Settings")]
private static void StartScene()
{
GetWindow<StartSceneWindow>();
}

You may use GUIButton or OnInspectorUpdate or other calculations you may wish to do in order to set the scene.

//Option 0 
private void OnGUI()
{
EditorSceneManager.playModeStartScene = (SceneAsset)EditorGUILayout.ObjectField(new GUIContent("Start Scene"),
EditorSceneManager.playModeStartScene, typeof(SceneAsset),false);

if (GUILayout.Button("Set as Start Scene"))
SetPlayModeStartScene(ScenePath);
}

// Option 1
private void OnInspectorUpdate()
{
SetPlayModeStartScene(ScenePath);
}

And now we have a scene that always loads when hit play in any scene!

Do not forget that if you want to do multiple scene structure, you may need to create some SceneManager operations like loading additive scene. You can get whole script in this link.

--

--

Irfan Yigit Baysal
Irfan Yigit Baysal

Written by Irfan Yigit Baysal

Unity Game Developer at Matchingham Games

Responses (1)