Skip to content

Instantly share code, notes, and snippets.

@momo-the-monster
Last active March 20, 2017 21:00
Show Gist options
  • Save momo-the-monster/ba4ac9eb47b69a783d5b08bf2c08e53b to your computer and use it in GitHub Desktop.
Save momo-the-monster/ba4ac9eb47b69a783d5b08bf2c08e53b to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEditor;
using UnityEditor.SceneManagement;
using System.Collections.Generic;
using System.Text;
namespace Helios
{
[InitializeOnLoad]
public class ShowSceneButtons : EditorWindow
{
// Stored Settings
static bool m_show = EditorPrefs.GetBool(string.Format("ShowSceneButtons.{0}_Show", PlayerSettings.productName), true);
static int m_buttonWidth = EditorPrefs.GetInt(string.Format("ShowSceneButtons.{0}_ButtonWidth", PlayerSettings.productName), 100);
// Internal variables
static Dictionary<string, string> sceneLookup;
static float lastCheck = 0;
#region Settings Window
[MenuItem("Helios/SceneButtons/Configure", false, 1)]
static void Init()
{
// Get existing open window or if none, make a new one:
ShowSceneButtons window = (ShowSceneButtons)GetWindow(typeof(ShowSceneButtons));
window.Show();
}
void OnGUI()
{
GUI.changed = false;
EditorGUILayout.BeginHorizontal();
m_show = EditorGUILayout.Toggle("Show Scene Buttons", m_show);
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal();
m_buttonWidth = EditorGUILayout.IntField("Button Width", m_buttonWidth);
EditorGUILayout.EndHorizontal();
if (GUI.changed)
{
EditorPrefs.SetBool(string.Format("ShowSceneButtons.{0}_Show", PlayerSettings.productName), m_show);
EditorPrefs.SetInt(string.Format("ShowSceneButtons.{0}_ButtonWidth", PlayerSettings.productName), m_buttonWidth);
}
}
#endregion
static ShowSceneButtons()
{
SceneView.onSceneGUIDelegate += OnScene;
EditorApplication.update += Update;
RefreshLookup();
}
private static void OnScene(SceneView sceneView)
{
if (m_show)
{
Handles.BeginGUI();
GUILayout.BeginArea(new Rect(5, 5, m_buttonWidth, 20 * sceneLookup.Count));
foreach (var item in sceneLookup)
{
if (GUILayout.Button(item.Key))
{
SafeLoad(item);
}
}
GUILayout.EndArea();
Handles.EndGUI();
}
}
public static void SafeLoad(KeyValuePair<string, string> item)
{
if (Application.isPlaying)
{
// Load Scene By name
UnityEngine.SceneManagement.SceneManager.LoadScene(item.Key);
}
else
{
// Offer to save first
EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo();
// Load Scene by path
EditorSceneManager.OpenScene(item.Value);
}
}
public void OnDestroy()
{
EditorApplication.update -= Update;
}
static void Update()
{
// Refresh Listing every 5 seconds
if (lastCheck == 0 || Time.realtimeSinceStartup - lastCheck > 5)
{
RefreshLookup();
lastCheck = Time.realtimeSinceStartup;
}
}
[MenuItem("Helios/SceneButtons/Refresh", false, 2)]
static void RefreshLookup()
{
if(sceneLookup != null)
{
sceneLookup.Clear();
}
else
{
sceneLookup = new Dictionary<string, string>();
}
foreach (UnityEditor.EditorBuildSettingsScene S in UnityEditor.EditorBuildSettings.scenes)
{
if (S.enabled)
{
string name = S.path.Substring(S.path.LastIndexOf('/') + 1);
name = name.Substring(0, name.Length - 6);
if (!sceneLookup.ContainsKey(name))
{
sceneLookup.Add(name, S.path);
}
else
{
Debug.LogErrorFormat("You have two scenes called {0}, skipping a button for the second one.", name);
}
}
}
}
[MenuItem("Helios/SceneButtons/GenerateMenuItems", false, 3)]
static void GenerateMenuItems()
{
if(sceneLookup != null)
{
string scriptFile = Application.dataPath + "/[Vendor]/Helios/Editor/SceneMenuItems.cs";
StringBuilder sb = new StringBuilder();
sb.AppendLine("// This class is Auto-Generated");
sb.AppendLine("using UnityEngine;");
sb.AppendLine("using UnityEditor;");
sb.AppendLine("using System.Collections.Generic;");
sb.AppendLine("");
sb.AppendLine("namespace Helios {");
sb.AppendLine(" public static class SceneMenuItems {");
sb.AppendLine("");
foreach (var item in sceneLookup)
{
sb.AppendLine(" [MenuItem(\"Helios/Scenes/" + item.Key + "\")]");
sb.AppendLine(" private static void LoadScene" + item.Key + "() {");
sb.AppendLine(string.Format(" ShowSceneButtons.SafeLoad(new KeyValuePair<string, string>(\"{0}\", \"{1}\"));", item.Key, item.Value));
sb.AppendLine(" }");
sb.AppendLine("");
}
sb.AppendLine("");
sb.AppendLine("}");
sb.AppendLine("}");
// writes the class and imports it so it is visible in the Project window
System.IO.File.Delete(scriptFile);
System.IO.File.WriteAllText(scriptFile, sb.ToString(), System.Text.Encoding.UTF8);
AssetDatabase.ImportAsset("Assets/[Vendor]/Helios/Editor/SceneMenuItems.cs");
}
else
{
Debug.LogError("Can't create menu items without any scenes. Try adding scenes to your Build Settings or Refreshing.");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment