You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity自定义Lock PropertyAttribute与其他PropertyAttribute冲突如何解决

问题根源

Unity 的 PropertyDrawer 设计为同一字段同一时间仅会生效一个自定义绘制器,多个 PropertyAttribute 标记时,只有离字段定义最近的那个 Attribute 对应的 Drawer 会被调用,这就是调整属性顺序后其中一个功能失效的原因。
你之前调用base.OnGUI报错属于预期行为,PropertyDrawer 基类本身没有实现默认的 OnGUI 逻辑,调用后必然返回No GUI Implmented提示。

解决方案

不需要修改 Attribute 定义,只需要调整绘制器中的绘制方法,将普通的EditorGUI.PropertyField替换为EditorGUI.DefaultPropertyField即可。该方法会自动扫描字段上所有的属性标记,按顺序应用对应的绘制逻辑,可同时兼容 Lock、Range 等多个属性。

修改后的 LockAttribute 绘制器代码如下:

[CustomPropertyDrawer(typeof(LockAttribute))]
public class LockAttributePropertyDrawer : PropertyDrawer 
{
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        EditorGUI.BeginDisabledGroup(Application.isPlaying);
        // DefaultPropertyField 会自动处理同字段上其他 Attribute 的绘制逻辑
        EditorGUI.DefaultPropertyField(position, property, label);
        EditorGUI.EndDisabledGroup();
    }
}

修改完成后,无论属性标记顺序如何,Lock 的运行时禁用逻辑和 Range 的滑动条逻辑都可以正常生效。


内容的提问来源于stack exchange,提问作者user3915050

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 05:54:05