Skip to content

Instantly share code, notes, and snippets.

@unity3d-kr
Last active November 13, 2023 10:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unity3d-kr/d22b6a6f379947a9e6fdea3ef48df19f to your computer and use it in GitHub Desktop.
Save unity3d-kr/d22b6a6f379947a9e6fdea3ef48df19f to your computer and use it in GitHub Desktop.
EditorGUI.ObjectField without object picker
internal static class ObjectFieldEx
{
private static readonly int s_ObjectFieldHash = "s_ObjectFieldHash".GetHashCode();
internal const float kSingleLineHeight = 18f;
private const float kIndentPerLevel = 15;
public static Object ObjectField(Object obj, Type objType, params GUILayoutOption[] options)
{
Rect r = EditorGUILayout.GetControlRect(true, kSingleLineHeight, options);
return ObjectField(r, obj, objType);
}
public static Object ObjectField(Rect position, Object obj, Type objType)
{
int id = GUIUtility.GetControlID(s_ObjectFieldHash, FocusType.Keyboard, position);
return DoObjectField(IndentedRect(position), IndentedRect(position), id, obj, objType);
}
internal static Object DoObjectField(Rect position, Rect dropRect, int id, Object obj, System.Type objType)
{
Event evt = Event.current;
EventType eventType = evt.type;
// special case test, so we continue to ping/select objects with the object field disabled
if (!GUI.enabled && (Event.current.rawType == EventType.MouseDown))
eventType = Event.current.rawType;
bool hasThumbnail = EditorGUIUtility.HasObjectThumbnail(objType);
Vector2 oldIconSize = EditorGUIUtility.GetIconSize();
EditorGUIUtility.SetIconSize(new Vector2(12, 12)); // Have to be this small to fit inside a single
switch (eventType)
{
case EventType.MouseDown:
// Ignore right clicks
if (Event.current.button != 0)
break;
if (position.Contains(Event.current.mousePosition))
{
EditorGUIUtility.editingTextField = false;
Object actualTargetObject = obj;
Component com = actualTargetObject as Component;
if (com)
actualTargetObject = com.gameObject;
// One click shows where the referenced object is, or pops up a preview
if (Event.current.clickCount == 1)
{
GUIUtility.keyboardControl = id;
EditorGUIUtility.PingObject(actualTargetObject);
evt.Use();
}
// Double click opens the asset in external app or changes selection to referenced object
else if (Event.current.clickCount == 2)
{
if (actualTargetObject)
{
AssetDatabase.OpenAsset(actualTargetObject);
GUIUtility.ExitGUI();
}
evt.Use();
}
}
break;
case EventType.Repaint:
GUIContent temp = EditorGUIUtility.ObjectContent(obj, objType);
temp.tooltip = obj.name;
style.Draw(position, temp, id, DragAndDrop.activeControlID == id, position.Contains(Event.current.mousePosition));
break;
}
EditorGUIUtility.SetIconSize(oldIconSize);
return obj;
}
internal static GUIStyle _style;
internal static GUIStyle style
{
get
{
if (_style == null)
{
_style = new GUIStyle(EditorStyles.textField);
_style.imagePosition = ImagePosition.ImageLeft;
}
return _style;
}
}
internal static float indent => EditorGUI.indentLevel * kIndentPerLevel;
// Apply the indentLevel to a control rect
public static Rect IndentedRect(Rect source)
{
float x = indent;
return new Rect(source.x + x, source.y, source.width - x, source.height);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment