Is there any way to hide the “Object picker” of an EditorGUILayout.ObjectField in Unity Isnpector?

梦想与她 提交于 2019-12-02 02:16:03

问题


I'm just asking if there is any possibility to hide the "Object Picker" (The little knob/menu next to an ObjectField) in a custom Inspector. I have some cases where changes are disabled (DisableGroup) and I would like to also hide the knob while the content can not be changed anyway.

Also to make things easier for users I think about making the field higher (EditorGUIUtility.SingleLineHeight * 2) -> the picker gets stretched as well what looks kind of shitty ^^

example:

using UnityEditor;
using UnityEngine;

public class Bla : MonoBehaviour {

    [CustomEditor(typeof(Bla))]
    public class BlaEditor : Editor
    {
        private AudioClip _clip;

        public override void OnInspectorGUI()
        {
            EditorGUI.BeginDisabledGroup(true);
            // do some magic to hide the object picker
            _clip = (AudioClip) EditorGUILayout.ObjectField("some label", _clip, typeof(AudioClip), false);
            EditorGUI.EndDisabledGroup();
        }
    }
}

I want to stick with an ObjectField rather than a simple Label for two reasons:

  • Even on a disabled `ObjectField| the "ping" functionality is still working. (If you click on it, the according asset gets highlighted in the Hierarchy.) This is not the case obviously with a label.
  • The user should not get confused with completely different looking controls but I rather only want to remove some unnecessary clutter.

回答1:


You might find a solution to hide the object picker by usage of stylesheets.

If all you want is just to display some reference, you can use a simple button basically styled as text field, adding an image and ping the object from code yourself.

using UnityEngine;

namespace Test
{
    public class TestBehaviour : MonoBehaviour
    {
        [SerializeField] private bool _audioEnabled;
        [SerializeField] private AudioClip _audioClip;
    }
}

editor:

using System.Reflection;
using UnityEditor;
using UnityEditor.Experimental.UIElements;
using UnityEngine;

namespace Test
{
    [CustomEditor(typeof(TestBehaviour))]
    public class TestBehaviourEditor : Editor
    {
        private SerializedProperty _clipProp;
        private SerializedProperty _audioEnabledProp;
        private ObjectField m_ObjectField;

        private const BindingFlags FIELD_BINDING_FLAGS = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;

        private void OnEnable()
        {
            _clipProp = serializedObject.FindProperty("_audioClip");
            _audioEnabledProp = serializedObject.FindProperty("_audioEnabled");
        }

        public override void OnInspectorGUI()
        {
            serializedObject.Update();

            EditorGUILayout.PropertyField(_audioEnabledProp);

            if(_audioEnabledProp.boolValue)
                EditorGUILayout.PropertyField(_clipProp);
            else
            {
                //TODO: calculate proper layout 

                var type = target.GetType().GetField(_clipProp.propertyPath, FIELD_BINDING_FLAGS).FieldType;
                var clip = _clipProp.objectReferenceValue;

                var guiContent = EditorGUIUtility.ObjectContent(clip, type);

                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("Fake ObjectField Button");

                var style = new GUIStyle("TextField");
                style.fixedHeight   = 16;
                style.imagePosition = clip ? ImagePosition.ImageLeft : ImagePosition.TextOnly;


                if (GUILayout.Button(guiContent, style ) && clip)
                    EditorGUIUtility.PingObject(clip);

                EditorGUILayout.EndHorizontal();
            }

            serializedObject.ApplyModifiedProperties();
        }
    }
}


来源:https://stackoverflow.com/questions/51397407/is-there-any-way-to-hide-the-object-picker-of-an-editorguilayout-objectfield-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!