Start with first scene script

Sometimes when working on a scene in Unity you do not necessarily want the play mode to start with that scene (which is the default behavior).

Instead, you may want it to start as it would in a build (aka the first scene is used).

This adds a simple on/off checkbox to the Tools menu. It is based on an idea on this unity forum thread. You can also find the source as a Gist on GitHub.

#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;

using UnityEditor.SceneManagement;

namespace Kamgam
{
	/// <summary>
	/// Start With First Scene<br />
	/// Sometimes when working on a scene in Unity you do not necessarily want the play mode
	/// to start with that scene (which is the default behavior). Instead, you may want it to
	/// start as it would in a build (aka the first scene is used).<br /><br />
	/// This adds a simple on/off checkbox to the Tools menu.
	/// </summary>
	/// <description>
	/// Based on an idea on this thread:
	/// http://forum.unity3d.com/threads/157502-Executing-first-scene-in-build-settings-when-pressing-play-button-in-editor
	/// </description>
	[InitializeOnLoad]
	public static class StartWithFirstScene
	{
		private const string NAME = "Tools/Start with first scene";

		static bool isEnabled
		{
			get
			{
				return EditorPrefs.GetBool(NAME, false);
			}
			set
			{
				EditorPrefs.SetBool(NAME, value);
				SceneListChanged();
			}
		}

		[MenuItem(NAME, false, 0)]
		static void LoadFirstScene()
		{
			isEnabled = !isEnabled;
		}

		[MenuItem(NAME, true, 0)]
		static bool ValidateLoadFirstScene()
		{
			Menu.SetChecked(NAME, isEnabled);
			return true;
		}

		static StartWithFirstScene()
		{
			SceneListChanged();

			EditorBuildSettings.sceneListChanged += SceneListChanged;
		}

		static void SceneListChanged()
		{
			if (!isEnabled)
			{
				EditorSceneManager.playModeStartScene = default;
				return;
			}
			//Ensure at least one build scene exist.
			if (EditorBuildSettings.scenes.Length == 0)
			{
				Debug.Log("No Scenes in Build Settings");
				isEnabled = false;
				return;
			}
			//Reference the first scene
			SceneAsset theScene = AssetDatabase.LoadAssetAtPath<SceneAsset>(EditorBuildSettings.scenes[0].path);
			// Set Play Mode scene to first scene defined in build settings.
			EditorSceneManager.playModeStartScene = theScene;
		}
	}
}
#endif

Asset Recommendations

Here are some assets that may be useful to you. If you get one of them then some of the money will go towards funding this project. Thank you.

Disclosure: This text may contain affiliate links, which means we may receive a commission if you click a link and purchase something that we have recommended. While clicking these links won't cost you any money, they will help fund this project! The links are created by Unity and Partnerize (Unity's affiliate partner).

Related Articles

Let the AI write the code

AIs have been all the rage for a while now. And rightfully so. Language AIs like ChatGPT are powerful tools and they are here to stay.
Read article ..