【问题标题】:Indentation messing up Nested Property Drawers缩进弄乱了嵌套的属性抽屉
【发布时间】:2018-04-12 07:39:00
【问题描述】:

我有一个名为 ToggledFloat 的类。它包装了一个浮点数,但还添加了一个布尔值,告诉它是否启用。

[System.Serializable]
public class ToggledFloat
{
    public float value;
    public bool enabled = false;
}

[System.Serializable]
public class ClassWIthNestedProp
{
    public ToggledFloat aTogFloat;
}

public class Test : MonoBehaviour
{
    public ClassWIthNestedProp eCA;
    public ToggledFloat tF;
}

我可以很容易地为此创建一个自定义属性抽屉,当编辑器在缩进级别“0”处绘制此属性时,它看起来是正确的。但是,当我查看嵌套在另一个属性中的 ToggledFloat 属性时,它们看起来是错误的。

我的 propertyDrawer 看起来像这样:

[CustomPropertyDrawer(typeof(ToggledFloat))]
public class ToggledPropertyEditor : PropertyDrawer
{

    public override float GetPropertyHeight (SerializedProperty property, GUIContent label)
    {
        var propVal = property.FindPropertyRelative("value");
        float contentUnfoldedHeight = EditorGUI.GetPropertyHeight (propVal, label);
        return contentUnfoldedHeight;
    }

    // Draw the property inside the given rect
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        int iL = EditorGUI.indentLevel;
        //EditorGUI.indentLevel = 0; //- Set this to "0" to make thing work but non-indented
        SerializedProperty valueProp = property.FindPropertyRelative("value");
        SerializedProperty enabledProp = property.FindPropertyRelative("enabled");

        //Label left of checkbox
        float labelWidth = GUI.skin.label.CalcSize(label).x;
        Rect labelRect = new Rect(0/*position.x*/, position.y, labelWidth, 16);
        EditorGUI.LabelField(labelRect, label, GUIContent.none);
        EditorGUI.DrawRect(labelRect, new Color(0,0,1,.1f));

        //Checkbox
        Rect enableRect = new Rect();
        enableRect.xMin = labelRect.xMax;
        enableRect.yMin = labelRect.yMin;
        enableRect.width = 16;
        enableRect.height = 16;
        EditorGUI.PropertyField(enableRect, enabledProp, GUIContent.none);
        EditorGUI.DrawRect(enableRect, new Color(0,1,0,.1f));

        //Value
        Rect valueRect = new Rect();
        valueRect.xMin = enableRect.xMax;
        valueRect.yMin = enableRect.yMin;
        valueRect.xMax = position.xMax;
        valueRect.yMax = position.yMax;

        EditorGUI.DrawRect(valueRect, new Color(1,0,0,.1f));
        bool enabled = GUI.enabled;
        GUI.enabled = enabledProp.boolValue;
        EditorGUI.PropertyField(valueRect, valueProp, new GUIContent(""), true);
        GUI.enabled = enabled;
        EditorGUI.indentLevel = iL;

    }
}

当检查器绘制类 Test 的实例时,它看起来像这样:

彩色矩形只是为了让我调试这些矩形的实际位置。奇怪的是文本标签从彩色矩形偏移,即使它们得到与参数相同的矩形。如果我想在我的检查器中使用彩色矩形,这当然只是一个问题 - 但真正的问题是,这个偏移问题似乎导致嵌套复选框不起作用。我无法单击嵌套属性上的复选框。

如果我随后显式设置 EditorGUI.IndenLevel = 0,那么彩色矩形和标签会重合,并且切换按钮可以正常工作 - 但我会松开自动缩进,我真的很想使用它。

谁能告诉我我在俯瞰什么

【问题讨论】:

    标签: unity3d unity3d-editor unity-editor


    【解决方案1】:

    DrawRect 方法不考虑缩进级别。您必须自己调整 rect 才能绘制缩进矩形。幸运的是,有一个函数可以获取意图的矩形。

    From UnityCsReference EditorGUI

    static void DrawIndentedRect(Rect rect, Color color)
    {
        EditorGUI.DrawRect(EditorGUI.IndentedRect(rect), color);
    }
    

    Captured Inspector IndentedRect

    但是它不能正常工作。

    implementation of IndentedRect开始,它减小了矩形的宽度。

    static void DrawIndentedRect(Rect rect, Color color)
    {
        // Indent per level is 15
        rect.x += EditorGUI.indentLevel * 15;
        EditorGUI.DrawRect(rect, color);
    }
    

    static void DrawIndentedRect(Rect rect, Color color)
    {
        var indentedRect = EditorGUI.IndentedRect(rect);
        indentedRect.width = rect.width;
        EditorGUI.DrawRect(indentedRect, color);
    }
    

    Captured Inspector DrawIndentedRect

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-01
      • 2014-02-13
      • 1970-01-01
      • 2014-10-22
      • 2021-09-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多