Skip to content

Instantly share code, notes, and snippets.

@drZool
Last active May 7, 2019 13:12
Show Gist options
  • Select an option

  • Save drZool/f62cdc297919079a9430f090c329ae6c to your computer and use it in GitHub Desktop.

Select an option

Save drZool/f62cdc297919079a9430f090c329ae6c to your computer and use it in GitHub Desktop.
Scene Launcher
#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
using System.Collections.Generic;
using UnityEditor.SceneManagement;
public class SceneLauncherLite : EditorWindow
{
private List<SceneData> scenes = new List<SceneData> ();
string fallbackScenePath;
string playingScenePath;
[MenuItem ("Hello/Scene/Scene Launcher")]
public static void Init ()
{
EditorWindow.GetWindow<SceneLauncherLite> ("Scene Launcher");
}
static readonly float BUTTON_WIDTH = 50f;
static readonly Color COLOR_DEFAULT = Color.white;
static readonly Color COLOR_DISABLED = Color.grey;
static readonly Color COLOR_SELECTED = new Color (103f / 255f, 190f / 255f, 68f / 255f);
static readonly Color COLOR_POSITIVE = new Color (103f / 255f, 190f / 255f, 68f / 255f);
static readonly Color COLOR_NEEDS_SAVING = new Color (1f, 0f, 0f);
void OnEnable ()
{
EditorApplication.update += Update;
ReloadScenes ();
}
void OnDisable ()
{
EditorApplication.update -= Update;
}
Vector2 scrollPosition;
void OnGUI ()
{
scrollPosition = GUILayout.BeginScrollView (scrollPosition);
GUI.enabled = !EditorApplication.isPlaying;
GUI.backgroundColor = COLOR_DEFAULT;
GUILayout.BeginHorizontal ();
if (GUILayout.Button ("Refresh", GUILayout.ExpandWidth (true))) {
ReloadScenes ();
}
if (GUILayout.Button ("Open Build Settings", GUILayout.ExpandWidth (true))) {
EditorWindow.GetWindow (System.Type.GetType ("UnityEditor.BuildPlayerWindow,UnityEditor"));
}
if (GUILayout.Button("Player Settings", GUILayout.ExpandWidth(true)))
{
#if UNITY_2018_3_OR_NEWER
Selection.activeObject = Unsupported.GetSerializedAssetInterfaceSingleton("PlayerSettings");
//SettingsService.OpenProjectSettings("Project/Player"); //This also works, gives the popupwindow, not inspector
#else
EditorApplication.ExecuteMenuItem("Edit/Project Settings/Player");
#endif
}
// GUI.backgroundColor = COLOR_POSITIVE;
//
// if (GUILayout.Button("Play current scene", GUILayout.ExpandWidth(true)))
// {
// fallbackScenePath = null;
// playingScenePath = null;
// EditorApplication.isPlaying = true;
// }
GUILayout.EndHorizontal ();
for (int i = 0; i < scenes.Count; i++) {
SceneData sceneData = scenes [i];
if (true) {
//bool isActiveScene = (EditorSceneManager.GetActiveScene ().path == scene.path);
var scene = EditorSceneManager.GetSceneByPath (sceneData.path);
GUI.backgroundColor = COLOR_POSITIVE;
if (scene.isLoaded) {
GUI.backgroundColor = COLOR_SELECTED;
} else {
GUI.backgroundColor = COLOR_DEFAULT;
}
if (!sceneData.enabled) {
GUI.backgroundColor = COLOR_DISABLED;
}
GUILayout.BeginHorizontal (GUI.skin.box);
if (GUILayout.Button ("▶", GUILayout.Width (24))) {
fallbackScenePath = EditorSceneManager.GetActiveScene ().path;
playingScenePath = sceneData.path;
if (EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo ()) {
EditorSceneManager.OpenScene (sceneData.path);
EditorApplication.isPlaying = true;
}
}
//GUILayout.Label(scene.path.Substring(scene.path.IndexOf('/') + 1), GUI.skin.label);
GUILayout.Label (sceneData.name, GUI.skin.label);
if (scene.isLoaded) {
if (scene.isDirty) {
GUI.backgroundColor = COLOR_NEEDS_SAVING;
}
if (GUILayout.Button ("Unload", GUILayout.Width (BUTTON_WIDTH))) {
if (scene.isDirty)
EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo ();
EditorSceneManager.UnloadSceneAsync (scene);
}
} else {
if (GUILayout.Button ("Open", GUILayout.Width (BUTTON_WIDTH))) {
if (EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo ()) {
EditorSceneManager.OpenScene (sceneData.path);
}
}
if (GUILayout.Button ("Add", GUILayout.Width (BUTTON_WIDTH))) {
EditorSceneManager.OpenScene (sceneData.path, OpenSceneMode.Additive);
}
}
GUI.backgroundColor = COLOR_DEFAULT;
if (GUILayout.Button ("Find", GUILayout.Width (BUTTON_WIDTH))) {
Selection.activeObject = AssetDatabase.LoadAssetAtPath<Object> (sceneData.path);
EditorGUIUtility.PingObject (Selection.activeObject);
}
GUILayout.EndHorizontal ();
}
}
GUILayout.EndScrollView ();
}
void Update ()
{
if (EditorApplication.isPlaying) {
if (EditorSceneManager.GetActiveScene ().path == playingScenePath) {
playingScenePath = null;
Repaint ();
}
} else {
if (!string.IsNullOrEmpty (fallbackScenePath) && string.IsNullOrEmpty (playingScenePath)) {
EditorSceneManager.OpenScene (fallbackScenePath);
fallbackScenePath = null;
Repaint ();
}
}
}
void ReloadScenes ()
{
scenes.Clear ();
foreach (EditorBuildSettingsScene scene in EditorBuildSettings.scenes) {
scenes.Add (new SceneData () {
path = scene.path,
name = scene.path.Substring (scene.path.LastIndexOf ('/') + 1).Replace (".unity", ""),
enabled = scene.enabled
});
}
//scenes.Sort((a, b) => string.Compare(a.name, b.name));
}
class SceneData
{
public string path;
public string name;
public bool enabled;
public string formattedName {
get {
var result = name;
if (!enabled)
result += " (Disabled)";
return result;
}
}
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment