Last active
May 27, 2021 01:22
-
-
Save partlyhuman/83b2ca38624886c44b7ab1ecfe8a7c36 to your computer and use it in GitHub Desktop.
Unity Keyboard Scene Switcher
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.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); | |
} | |
} | |
} |
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 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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:Would be neat if you could add it to the gist :)