Skip to content

Instantly share code, notes, and snippets.

@partlyhuman
Last active November 21, 2023 14:53
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save partlyhuman/f27dd291f333cb82502017579b6fd7e9 to your computer and use it in GitHub Desktop.
Save partlyhuman/f27dd291f333cb82502017579b6fd7e9 to your computer and use it in GitHub Desktop.
Unity Find by GUID
using UnityEditor;
using UnityEngine;
namespace com.hololabs.editor
{
public class FindByGuid : EditorWindow
{
[MenuItem("Utility/Find Asset by Guid %&g")]
public static void DoFindByGuidMenu()
{
TextInputDialog.Prompt("GUID", "Find asset by Guid:", FindAssetByGuid);
}
static void FindAssetByGuid(string searchGuid)
{
string path = AssetDatabase.GUIDToAssetPath(searchGuid);
if (string.IsNullOrEmpty(path)) return;
var obj = AssetDatabase.LoadAssetAtPath<Object>(path);
if (obj == null) return;
Selection.activeObject = obj;
EditorGUIUtility.PingObject(obj);
}
}
}
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()
{
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