Unity--EditorGUI.ObjectField实现

记录下,以后可以有个参考

绘制UI使用的是style.Draw(position, gUIContent, id, DragAndDrop.activeControlID == id);

这个style其实就是EditorStyles.objectField

有ObjectSelector.get.Show(obj, objType, property, allowSceneObjects);的

码块是后面那个圈圈的点击事件,点击后弹出对象选择面板

拖拽处理在EventType.DragUpdated,EventType.DragPerform的case块代码中

 
public static Object ObjectField(Rect position, GUIContent label, Object obj, Type objType, bool allowSceneObjects)
{
    int controlID = GUIUtility.GetControlID(EditorGUI.s_ObjectFieldHash, FocusType.Keyboard, position);
    position = EditorGUI.PrefixLabel(position, controlID, label);
    if (EditorGUIUtility.HasObjectThumbnail(objType) && position.height > 16f)
    {
        float num = Mathf.Min(position.width, position.height);
        position.height = num;
        position.xMin = position.xMax - num;
    }
    return EditorGUI.DoObjectField(position, position, controlID, obj, objType, null, null, allowSceneObjects);
}
 
internal static Object DoObjectField(Rect position, Rect dropRect, int id, Object obj, Type objType, SerializedProperty property, EditorGUI.ObjectFieldValidator validator, bool allowSceneObjects)
{
    return EditorGUI.DoObjectField(position, dropRect, id, obj, objType, property, validator, allowSceneObjects, EditorStyles.objectField);
}
 
internal static Object DoObjectField(Rect position, Rect dropRect, int id, Object obj, Type objType, SerializedProperty property, EditorGUI.ObjectFieldValidator validator, bool allowSceneObjects, GUIStyle style)
{
    if (validator == null)
    {
        if (EditorGUI.<>f__mg$cache5 == null)
        {
            EditorGUI.<>f__mg$cache5 = new EditorGUI.ObjectFieldValidator(EditorGUI.ValidateObjectFieldAssignment);
        }
        validator = EditorGUI.<>f__mg$cache5;
    }
    Event current = Event.current;
    EventType eventType = current.type;
    if (!GUI.enabled && GUIClip.enabled && Event.current.rawType == EventType.MouseDown)
    {
        eventType = Event.current.rawType;
    }
    bool flag = EditorGUIUtility.HasObjectThumbnail(objType);
    EditorGUI.ObjectFieldVisualType objectFieldVisualType = EditorGUI.ObjectFieldVisualType.IconAndText;
    if (flag && position.height <= 18f && position.width <= 32f)
    {
        objectFieldVisualType = EditorGUI.ObjectFieldVisualType.MiniPreview;
    }
    else
    {
        if (flag && position.height > 16f)
        {
            objectFieldVisualType = EditorGUI.ObjectFieldVisualType.LargePreview;
        }
    }
    Vector2 iconSize = EditorGUIUtility.GetIconSize();
    if (objectFieldVisualType == EditorGUI.ObjectFieldVisualType.IconAndText)
    {
        EditorGUIUtility.SetIconSize(new Vector2(12f, 12f));
    }
    else
    {
        if (objectFieldVisualType == EditorGUI.ObjectFieldVisualType.LargePreview)
        {
            EditorGUIUtility.SetIconSize(new Vector2(64f, 64f));
        }
    }
    Object result;
    switch (eventType)
    {
    case EventType.KeyDown:
        if (GUIUtility.keyboardControl == id)
        {
            if (current.keyCode == KeyCode.Backspace || current.keyCode == KeyCode.Delete)
            {
                if (property != null)
                {
                    property.objectReferenceValue = null;
                }
                else
                {
                    obj = null;
                }
                GUI.changed = true;
                current.Use();
            }
            if (current.MainActionKeyForControl(id))
            {
                ObjectSelector.get.Show(obj, objType, property, allowSceneObjects);
                ObjectSelector.get.objectSelectorID = id;
                current.Use();
                GUIUtility.ExitGUI();
            }
        }
        goto IL_664;
    case EventType.KeyUp:
    case EventType.ScrollWheel:
    case EventType.Layout:
        IL_119:
        if (eventType != EventType.ExecuteCommand)
        {
            if (eventType == EventType.DragExited)
            {
                if (GUI.enabled)
                {
                    HandleUtility.Repaint();
                }
                goto IL_664;
            }
            if (eventType != EventType.MouseDown)
            {
                goto IL_664;
            }
            if (Event.current.button != 0)
            {
                goto IL_664;
            }
            if (position.Contains(Event.current.mousePosition))
            {
                Rect rect;
                switch (objectFieldVisualType)
                {
                case EditorGUI.ObjectFieldVisualType.IconAndText:
                case EditorGUI.ObjectFieldVisualType.MiniPreview:
                    rect = new Rect(position.xMax - 15f, position.y, 15f, position.height);
                    break;
                case EditorGUI.ObjectFieldVisualType.LargePreview:
                    rect = new Rect(position.xMax - 36f, position.yMax - 14f, 36f, 14f);
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
                }
                EditorGUIUtility.editingTextField = false;
                if (rect.Contains(Event.current.mousePosition))
                {
                    if (GUI.enabled)
                    {
                        GUIUtility.keyboardControl = id;
                        ObjectSelector.get.Show(obj, objType, property, allowSceneObjects);
                        ObjectSelector.get.objectSelectorID = id;
                        current.Use();
                        GUIUtility.ExitGUI();
                    }
                }
                else
                {
                    Object @object = (property == null) ? obj : property.objectReferenceValue;
                    Component component = @object as Component;
                    if (component)
                    {
                        @object = component.gameObject;
                    }
                    if (EditorGUI.showMixedValue)
                    {
                        @object = null;
                    }
                    if (Event.current.clickCount == 1)
                    {
                        GUIUtility.keyboardControl = id;
                        EditorGUI.PingObjectOrShowPreviewOnClick(@object, position);
                        current.Use();
                    }
                    else
                    {
                        if (Event.current.clickCount == 2)
                        {
                            if (@object)
                            {
                                AssetDatabase.OpenAsset(@object);
                                GUIUtility.ExitGUI();
                            }
                            current.Use();
                        }
                    }
                }
            }
            goto IL_664;
        }
        else
        {
            string commandName = current.commandName;
            if (commandName == "ObjectSelectorUpdated" && ObjectSelector.get.objectSelectorID == id && GUIUtility.keyboardControl == id && (property == null || !property.isScript))
            {
                result = EditorGUI.AssignSelectedObject(property, validator, objType, current);
                return result;
            }
            if (!(commandName == "ObjectSelectorClosed") || ObjectSelector.get.objectSelectorID != id || GUIUtility.keyboardControl != id || property == null || !property.isScript)
            {
                goto IL_664;
            }
            if (ObjectSelector.get.GetInstanceID() == 0)
            {
                current.Use();
                goto IL_664;
            }
            result = EditorGUI.AssignSelectedObject(property, validator, objType, current);
            return result;
        }
        break;
    case EventType.Repaint:
    {
        GUIContent gUIContent;
        if (EditorGUI.showMixedValue)
        {
            gUIContent = EditorGUI.s_MixedValueContent;
        }
        else
        {
            if (property != null)
            {
                gUIContent = EditorGUIUtility.TempContent(property.objectReferenceStringValue, AssetPreview.GetMiniThumbnail(property.objectReferenceValue));
                obj = property.objectReferenceValue;
                if (obj != null)
                {
                    Object[] references = new Object[]
                    {
                        obj
                    };
                    if (EditorSceneManager.preventCrossSceneReferences && EditorGUI.CheckForCrossSceneReferencing(obj, property.serializedObject.targetObject))
                    {
                        if (!EditorApplication.isPlaying)
                        {
                            gUIContent = EditorGUI.s_SceneMismatch;
                        }
                        else
                        {
                            gUIContent.text += string.Format(" ({0})", EditorGUI.GetGameObjectFromObject(obj).scene.name);
                        }
                    }
                    else
                    {
                        if (validator(references, objType, property, EditorGUI.ObjectFieldValidatorOptions.ExactObjectTypeValidation) == null)
                        {
                            gUIContent = EditorGUI.s_TypeMismatch;
                        }
                    }
                }
            }
            else
            {
                gUIContent = EditorGUIUtility.ObjectContent(obj, objType);
            }
        }
        switch (objectFieldVisualType)
        {
        case EditorGUI.ObjectFieldVisualType.IconAndText:
            EditorGUI.BeginHandleMixedValueContentColor();
            style.Draw(position, gUIContent, id, DragAndDrop.activeControlID == id);
            EditorGUI.EndHandleMixedValueContentColor();
            break;
        case EditorGUI.ObjectFieldVisualType.LargePreview:
            EditorGUI.DrawObjectFieldLargeThumb(position, id, obj, gUIContent);
            break;
        case EditorGUI.ObjectFieldVisualType.MiniPreview:
            EditorGUI.DrawObjectFieldMiniThumb(position, id, obj, gUIContent);
            break;
        default:
            throw new ArgumentOutOfRangeException();
        }
        goto IL_664;
    }
    case EventType.DragUpdated:
    case EventType.DragPerform:
        if (dropRect.Contains(Event.current.mousePosition) && GUI.enabled)
        {
            Object[] objectReferences = DragAndDrop.objectReferences;
            Object object2 = validator(objectReferences, objType, property, EditorGUI.ObjectFieldValidatorOptions.None);
            if (object2 != null)
            {
                if (!allowSceneObjects && !EditorUtility.IsPersistent(object2))
                {
                    object2 = null;
                }
            }
            if (object2 != null)
            {
                DragAndDrop.visualMode = DragAndDropVisualMode.Generic;
                if (eventType == EventType.DragPerform)
                {
                    if (property != null)
                    {
                        property.objectReferenceValue = object2;
                    }
                    else
                    {
                        obj = object2;
                    }
                    GUI.changed = true;
                    DragAndDrop.AcceptDrag();
                    DragAndDrop.activeControlID = 0;
                }
                else
                {
                    DragAndDrop.activeControlID = id;
                }
                Event.current.Use();
            }
        }
        goto IL_664;
    }
    goto IL_119;
    IL_664:
    EditorGUIUtility.SetIconSize(iconSize);
    result = obj;
    return result;
}
 
--------------------- 
作者:zlQ_ 
来源:CSDN 
原文:https://blog.csdn.net/zlQQhs/article/details/83383646 
版权声明:本文为博主原创文章,转载请附上博文链接!

### 回答1: Unity-debugging-2018.x.zip是一个Unity版本相关的调试工具包,其中包含了诸如埋点工具和调试插件等功能丰富的工具。这些工具可以帮助Unity开发者在开发自己的游戏时快速、准确地定位和修复代码中的问题,提高了游戏的开发效率和质量。 这个工具包中最值得注意的一点是它的兼容性。它可以与Unity 2018中的许多不同版本一起使用,这意味着无论开发者使用哪个具体版本的Unity,都可以使用这个工具包进行调试。这为开发过程中遇到的问题提供了更为广泛和全面的支持,从而更好地满足了不同开发者的需求和要求。 另外,这个工具包还有一个很不错的特色,就是它的易用性。它提供了直观和易于操作的界面,即使是那些对调试工具很不熟悉的开发者也可以使用它。开发者可以通过它直接在Unity编辑器中观察代码执行过程中的变化,非常方便。 总的来说,Unity-debugging-2018.x.zip是一个非常实用和友好的Unity调试工具包,可以帮助Unity开发者更快速、高效地开发自己的游戏。 ### 回答2: unity-debugging-2018.x.zip是Unity引擎中用于调试的工具包。在程序开发的过程中,会出现各种各样的问题,而调试是解决这些问题的重要手段之一。Unity-debugging-2018.x.zip提供了一系列工具和功能,帮助程序员定位和解决问题。 Unity-debugging-2018.x.zip中包含了各种调试工具,例如调试器、内存分析器、性能分析器等等。这些工具可以帮助开发者监控程序运行的状态,包括内存使用、CPU使用、函数运行时间等等。通过这些信息,开发者可以找到程序中可能存在的性能问题,并对其进行优化。 同时,Unity-debugging-2018.x.zip还提供了调试器,帮助开发者调试程序。开发者可以在调试器中设置断点,一步一步地执行程序,查看变量和函数调用的情况。通过调试器,开发者可以快速定位程序中的错误,减少排错的时间。 总之,Unity-debugging-2018.x.zip是Unity开发中不可或缺的工具包。它可以帮助开发者定位和解决问题,提高程序的稳定性和性能,为游戏开发提供强有力的支持。 ### 回答3: unity-debugging-2018.x.zip是一个用于Unity引擎调试的文件。Unity是一款流行的游戏开发引擎,但在游戏开发过程中难免会遇到各种问题,例如程序崩溃、游戏运行异常等等。此时就需要进行调试。Unity-debugging-2018.x.zip文件中包含了一系列调试工具,可用于分析和诊断Unity游戏/应用程序的问题。其中包括了Unity自带的Profiler(性能分析器)、Debug.Log、断点调试、MonoDevelop等工具,这些工具可以帮助开发者查找问题所在,快速调试程序。一个好的调试工具不仅能帮助开发者快速找到问题,还能提高开发效率,使开发工作更加顺利。总之,Unity-debugging-2018.x.zip文件是Unity调试工具的集合,为开发者解决问题提供了极大的帮助,也是一个值得推广和使用的工具。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值