Skip to content

Instantly share code, notes, and snippets.

@partlyhuman
Last active May 27, 2021 01:22
Show Gist options
  • Save partlyhuman/83b2ca38624886c44b7ab1ecfe8a7c36 to your computer and use it in GitHub Desktop.
Save partlyhuman/83b2ca38624886c44b7ab1ecfe8a7c36 to your computer and use it in GitHub Desktop.
Unity Keyboard Scene Switcher
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEditor.SceneManagement;
namespace com.hololabs.editor
{
public static class GotoScene
{
[MenuItem("Utility/Go to Scene %#k")]
public static void MenuItem()
{
// check if scene has been modified
if(EditorSceneManager.GetActiveScene().isDirty)
{
// ask user if he wants to save scene, abort switch if cancel
if(!EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo())
return;
}
TextInputDialog.Prompt("Scene Switcher", "Scene name, partial ok, case insensitive:", DoGotoScene);
}
static void DoGotoScene(string searchTerm)
{
if (string.IsNullOrEmpty(searchTerm)) return;
List<string> matches = AssetDatabase.FindAssets("t:Scene " + searchTerm).Select(AssetDatabase.GUIDToAssetPath).ToList();
// See if there's an exact match - match full path to search term, case insensitive
var test = new Regex(string.Format(@"\b{0}.unity$", searchTerm), RegexOptions.Compiled | RegexOptions.IgnoreCase);
// if there's an exact match use it, otherwise use the first
string path = matches.Find(test.IsMatch) ?? matches.FirstOrDefault();
if (path != null) EditorSceneManager.OpenScene(path, OpenSceneMode.Single);
}
}
}
using System;
using UnityEditor;
using UnityEngine;
namespace com.hololabs
{
public class TextInputDialog : EditorWindow
{
const string INPUT_NAME = "TextInputDialog_TextField";
public string promptText = "Enter your input:";
public Action<string> callback;
void OnGUI()
{
Event e = Event.current;
if (e.type == EventType.keyUp && e.keyCode == KeyCode.Escape)
{
Close();
return;
}
EditorGUILayout.BeginVertical(GUIStyle.none, GUILayout.ExpandHeight(true), GUILayout.ExpandWidth(true));
EditorGUILayout.LabelField(promptText);
GUI.SetNextControlName(INPUT_NAME);
string inputText = EditorGUILayout.DelayedTextField("");
EditorGUILayout.EndVertical();
if (string.IsNullOrEmpty(inputText))
{
EditorGUI.FocusTextInControl(INPUT_NAME);
}
else
{
callback(inputText);
Close();
}
}
void OnLostFocus()
{
Close();
}
public static void Prompt(string title, string promptText, Action<string> callback)
{
var window = CreateInstance<TextInputDialog>();
window.minSize = new Vector2(300, 50);
window.maxSize = window.minSize;
window.titleContent = new GUIContent(title);
window.callback = callback;
window.promptText = promptText;
window.ShowUtility();
}
}
}
@GieziJo
Copy link

GieziJo commented May 14, 2019

Thanks a lot for you work:)
I added a small line of code before line 14 of GotoScene.cs which checks if you saved the scene before switching, preventing loss of work:

// check if scene has been modified
if(EditorSceneManager.GetActiveScene().isDirty)
{
	// ask user if he wants to save scene, abort switch if cancel
	if(!EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo())
		return;
}

Would be neat if you could add it to the gist :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment