Skip to content

Instantly share code, notes, and snippets.

@NeatWolf
Forked from partlyhuman/FindByGuid.cs
Created April 27, 2020 21:42
Show Gist options
  • Save NeatWolf/4e277988a7412c165a87f7c32232b22f to your computer and use it in GitHub Desktop.
Save NeatWolf/4e277988a7412c165a87f7c32232b22f 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