public static T ObjectField<T>(string label, Object obj, GUIStyle style_label, GUIStyle style_obj, GUIStyle style_obj_button, bool allow_scene_object)
where T : Object
{
Rect rect = GUILayoutUtility.GetRect(GUIContent.none, style_obj);
T t_obj = obj as T;
t_obj = CustomEditorGUI.ObjectField<T>(rect, label, t_obj, style_label, style_obj, style_obj_button, allow_scene_object);
return t_obj;
}
public static T ObjectField<T>(Rect position, string label, Object obj, GUIStyle style_label, GUIStyle style_obj, GUIStyle style_obj_button, bool allow_scene_object)
where T : Object
{
if(style_obj_button == null)
{
obj = ObjectField<T>(position, label, obj, style_label, style_obj, allow_scene_object);
return obj as T;
}
Event evt = Event.current;
EventType evt_type = Event.current.type;
int margin = style_obj.margin.left;
int label_width = 150;
int obj_width = (int)position.width - label_width - margin;
Rect rect_label = new Rect(position) { width = label_width };
Rect rect_obj = new Rect(position) { x = position.width - obj_width, width = obj_width };
int obj_field_outline = 1;
int button_width = (int)position.height;
int button_height = (int)position.height - obj_field_outline * 2;
Rect rect_button = new Rect(position) { x = position.width - button_width - obj_field_outline, y = position.y + obj_field_outline, width = button_width, height = button_height };
GUIContent obj_content = EditorGUIUtility.ObjectContent(obj, typeof(T));
int id_obj = GUIUtility.GetControlID(FocusType.Keyboard);
int id_picker = GUIUtility.GetControlID(FocusType.Passive);
if (evt.commandName == "ObjectSelectorUpdated" && id_picker == EditorGUIUtility.GetObjectPickerControlID())
{
obj = EditorGUIUtility.GetObjectPickerObject();
HandleUtility.Repaint();
}
Vector2 mouse_pos = evt.mousePosition;
bool flg_focused = false;
bool flg_on = false;
bool flg_hover = false;
bool flg_button_hover = false;
if (GUIUtility.keyboardControl == id_obj) flg_focused = true;
if (GUIUtility.hotControl == id_obj) flg_on = true;
if (rect_obj.Contains(mouse_pos)) flg_hover = true;
if (rect_button.Contains(mouse_pos)) flg_button_hover = true;
switch (evt_type)
{
case EventType.DragUpdated:
case EventType.DragPerform:
if (rect_obj.Contains(mouse_pos))
{
if (DragAndDrop.objectReferences.Length == 1) DragAndDrop.AcceptDrag();
if (DragAndDrop.objectReferences[0] is T) DragAndDrop.visualMode = DragAndDropVisualMode.Generic;
DragAndDrop.activeControlID = id_obj;
GUIUtility.hotControl = id_obj;
}
else if (GUIUtility.hotControl == id_obj)
{
GUIUtility.hotControl = 0;
}
break;
case EventType.DragExited:
if (rect_obj.Contains(mouse_pos))
{
T drag_obj = DragAndDrop.objectReferences[0] as T;
if (drag_obj != null)
{
obj = drag_obj;
HandleUtility.Repaint();
}
GUIUtility.hotControl = 0;
}
break;
case EventType.MouseDown:
if (evt.button != 0) break;
if (rect_label.Contains(mouse_pos) || rect_obj.Contains(mouse_pos))
{
GUIUtility.keyboardControl = id_obj;
HandleUtility.Repaint();
}
if (rect_button.Contains(mouse_pos))
{
EditorGUIUtility.ShowObjectPicker<T>(obj, allow_scene_object, "", id_picker);
}
else if(rect_obj.Contains(mouse_pos))
{
EditorGUIUtility.PingObject(obj);
}
break;
case EventType.Repaint:
style_label.Draw(rect_label, new GUIContent(label), false, false, false, flg_focused);
style_obj.Draw(rect_obj, obj_content, flg_hover, false, flg_on, flg_focused);
style_obj_button.Draw(rect_button, GUIContent.none, flg_button_hover, false, false, false);
break;
}
return obj as T;
}
コメントを書く