【问题标题】:Displaying properties in a horizontal, pleasant way以水平、令人愉悦的方式显示属性
【发布时间】:2017-06-30 11:10:40
【问题描述】:

我有两个整数属性,想在一行中显示它们。

[CustomEditor(typeof(MazeConfiguration))]
public class MazeConfigurationEditor : Editor
{
    MazeConfiguration myTarget;

    public void OnEnable()
    {
        myTarget = (MazeConfiguration)target;
    }

    public override void OnInspectorGUI()
    {

        EditorGUILayout.BeginHorizontal();
        myTarget.Width = EditorGUILayout.IntField("Width", myTarget.Width);
        myTarget.Length = EditorGUILayout.IntField("Length", myTarget.Length);
        EditorGUILayout.EndHorizontal();
    }
}

但它看起来很宽。

如果我改变 Inspector 的宽度,它看起来像这样

所以我想删除标签与其输入字段之间的那些大空格,并在属性之间添加一些空格。

听说Property Drawer可以帮到我,所以我试了一下

public class MyIntAttribute : PropertyAttribute { }

    [CustomPropertyDrawer(typeof(MyIntAttribute))]
    public class MyIntDrawer : PropertyDrawer
    {
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            EditorGUI.BeginProperty(position, label, property);

            // position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);

            var rect = new Rect(position.x / 2f, position.y, position.width / 2f, position.height);

            EditorGUI.PropertyField(rect, property);

            EditorGUI.EndProperty();
        }
    }

但它不会将输入字段移近相应的标签,我只能更改输入字段的宽度。

如何去除标签和输入字段之间的空格,并在不同属性之间添加空格?

【问题讨论】:

    标签: c# unity3d editor unity-editor


    【解决方案1】:

    我只需要改变

    EditorGUIUtility.labelWidth
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-05
      • 1970-01-01
      • 1970-01-01
      • 2019-12-16
      • 2018-03-04
      • 1970-01-01
      • 1970-01-01
      • 2018-12-19
      相关资源
      最近更新 更多