Created
November 4, 2021 20:38
-
-
Save LaserKaspar/f0e17a67220d760f97eeaef68f506305 to your computer and use it in GitHub Desktop.
(UnityEngine) Window for quickly switching between scenes added to your build settings.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using UnityEngine; | |
using UnityEditor; | |
using UnityEditor.SceneManagement; | |
using UnityEngine.SceneManagement; | |
public class SceneSwitcher : EditorWindow | |
{ | |
[MenuItem("Tools/SceneSwitcher")] | |
public static void ShowWindow() | |
{ | |
GetWindow<SceneSwitcher>().minSize = new Vector2(100, 25); | |
} | |
[SerializeField] private static Scene activeScene; | |
void OnSceneSelected(object scene) | |
{ | |
EditorSceneManager.SaveOpenScenes(); | |
activeScene = EditorSceneManager.OpenScene(((string) scene)); | |
} | |
private void OnGUI() | |
{ | |
activeScene = EditorSceneManager.GetActiveScene(); | |
if (GUILayout.Button("Select Scene")) | |
{ | |
// create the menu and add items to it | |
GenericMenu menu = new GenericMenu(); | |
for (int i = 0; i < SceneManager.sceneCountInBuildSettings; i++) | |
{ | |
String scenePath = SceneUtility.GetScenePathByBuildIndex(i); | |
String sceneName = scenePath.Substring(0, scenePath.Length - 6).Substring(scenePath.LastIndexOf('/') + 1); | |
menu.AddItem(new GUIContent(sceneName), scenePath.Equals(activeScene.path), OnSceneSelected, scenePath); | |
} | |
// display the menu | |
menu.ShowAsContext(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment