首页 > 解决方案 > Unity Editor:如何调整检查器属性字段的大小和空间值字段?

问题描述

我正在尝试实现我的第一个统一编辑器代码来制作一个自定义数组属性抽屉,其中包含缩进的命名条目以及缩进和动态调整大小的值字段。

我使用以下简单的 git 解决方案作为我的代码的基础,它允许在检查器中设置数组的标签:HERE

替换 gitHub 解决方案中显示的示例,我将此枚举用作我的数组元素名称容器:

[System.Serializable]
public enum HealthNames
{
    General,
    Head,
    Body,
    RightArm,
    LeftArm,
    Rightleg,
    leftLeg,
}

并将其设置在 monobehaviour 类中的数组上:

[ LabeledArray( typeof( HealthNames ) ) ]
public int[] m_aHealth = new int[ Enum.GetNames( typeof( HealthNames ) ).Length ];

我在 try 语句的开头和结尾添加了EditorGUI.indentLevel++;andEditorGUI.indentLevel--;来缩进数组元素的标签,使它们从 size 属性中脱颖而出。

在此处输入图像描述

从那里开始,我已经搜索了在元素的值字段上添加缩进或从 size 属性的值字段中删除它的方法。但使用 EditorGUI 没有找到答案

我还希望能够动态调整所有值字段的大小,但同样,仅使用 EditorGUI 没有答案。无法EditorStyle.AnyField.WordWrap = true;在属性字段上使用。将 a传递PropertyField给 a IntField,使用 aEditorStyles.NumberField并预先将其 wrodwrapping 设置为 true 无效。

我还发现了EditorGUILayout几年前的少数几个,但由于该解决方案是从地面构建的,因此它们不起作用EditorGUI

希望您对此事有所启发

标签: unity3dunity-editor

解决方案


如果我理解正确,您希望标签值字段缩进。

我认为它可以像例如

private const int INDENT = 15;

public override void OnGUI(Rect rect, SerializedProperty property, GUIContent label)
{
    EditorGUI.BeginProperty(rect, label, property);
    var fieldsRect = new Rect(rect.x + INDENT, rect.y, rect.width - INDENT, rect.height);
    try
    {
        var path = property.propertyPath;
        int pos = int.Parse(path.Split('[').LastOrDefault().TrimEnd(']'));
        EditorGUI.PropertyField(fieldRect, property, new GUIContent(ObjectNames.NicifyVariableName(((LabeledArrayAttribute)attribute).names[pos])), true);
    }
    catch
    {
        EditorGUI.PropertyField(fieldRect, property, label, true);
    }
    EditorGUI.EndProperty();
}

推荐阅读