【问题标题】:Unity PropertyAttribute, interfering with other PropertyAttributesunity PropertyAttribute,干扰其他 PropertyAttributes
【发布时间】:2021-12-27 01:52:41
【问题描述】:

我有一个属性

public class LockAttribute : PropertyAttribute { }

使用自定义抽屉脚本

[CustomPropertyDrawer(typeof(LockAttribute))]
public class LockAttributePropertyDrawer : PropertyDrawer 
{
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        EditorGUI.BeginDisabledGroup(Application.isPlaying);
        _= EditorGUI.PropertyField(position, property, label);
        EditorGUI.EndDisabledGroup();
    }
}

它的目的是在应用程序播放时禁用检查器字段。

如果我按这个顺序使用它

[SerializeField,Range(0,100),Lock] private int m_Resolution;

该字段永远不会禁用,当我交换 RangeLock 属性时,Range 属性无效。

有没有办法让两个属性都生效?

我尝试使用

Base.OnGUI(position, property, label);

而不是

EditorGUI.PropertyField(position, property, label);

但这样做会导致No GUI Implmented 出现在我的字段上`。

【问题讨论】:

    标签: c# unity3d unity-editor unity3d-editor


    【解决方案1】:

    看起来像 Unity 2020.3 中的“错误”。

    如果我只是颠倒顺序(Unity 2021.2.2f1),它对我有用:

    [SerializeField] 
    [Lock]
    [Range(0, 100)]
    private int m_Resolution;
    

    或者,您也可以添加order 参数,例如

    [SerializeField] 
    [Range(0, 100, order = 1)]
    [Lock(order = 0)]
    private int m_Resolution;
    

    虽然我宁愿直接让它们按正确的顺序排列^^


    我的一般猜测:原因是在这种情况下,您首先添加禁用组,然后让PropertyField 处理稍后添加的任何其他抽屉,内部可能不使用PropertyField,而是使用直接值抽屉。 但是,如果您首先使用Range,它已经用直接值字段(滑块)完全覆盖了抽屉,并且不会通过PropertyField 进一步转发绘图调用,因此不再处理以后的属性抽屉。


    长话短说:不幸的是,多个属性总是很棘手,其中大多数无法组合,或者至少在这种情况下,顺序很重要!

    例如,即使乍一看[Min] 属性不会改变int 字段的外观,您的[Lock] 仍然需要先行,否则它会被忽略。

    还有例如无论顺序如何,都无法将[Range][Min] 组合在一起。

    => 对于这些情况,您需要自己编写一个新的组合属性和抽屉!

    【讨论】:

    • 在我使用 2020.3.21f1 时,问题可能在我们两个版本之间的某个地方得到了解决
    • @RWolfe 刚刚证实,是的!那么似乎是一个“错误”。它也不适用于 Unity 2020.3.22f1!
    猜你喜欢
    • 1970-01-01
    • 2011-12-15
    • 2012-06-05
    • 2012-11-19
    • 2011-10-26
    • 2012-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多