首页 > 其他分享 >CustomPropertyDrawer

CustomPropertyDrawer

时间:2023-08-18 12:44:29浏览次数:46  
标签:缩进 高度 EditorGUI 标题 EditorGUIUtility 绘制 CustomPropertyDrawer

Unity3D CustomPropertyDrawer 自定义属性绘制器

  • api文档

    • 该文档中的EditorGUI.BeginProperty()EditorGUI.EndProperty(),不好用
  • 参考案例:

    • 直接看Unity中你感兴趣的渲染方式的实现方式: Packages/com.unity.ugui/Editor/UI/PropertyDrawers/...
      • FontDataDrawer
      • SpriteStateDrawer
      • ColorBlockDrawer
      • NavigationDrawer
      • 也可以直接在Project视图搜索Drawer,搜索范围选择In Packagers
        Serach Drawer In Project
  • 需要做的事

    • 绘制
    • 重写GetPropertyHeight 返回真正的高度
      • 不要忘记标题占了1行
      • 考虑展开 / 收缩 的情况, 返回不同的高度,
        • 收缩时,高度=EditorGUIUtility.singleLineHeight
        • 展开时,高度=EditorGUIUtility.singleLineHeight + 标题以外所有内容的高度 + 行间距*(行数-1)
  • 基本常识:

    • OnGUI(Rect position, SerializedProperty property, GUIContent label)
      • position 是这个变量的起始位置
      • property 是这个变量的SerializedProperty对象
        • 通过这个对象,可以获取到变量的值 property.FindPropertyRelative(paramName).(intValue / stringValue / vector3Value / enumValueIndex / ...)
      • lable.text 是变量名称
    • 默认每行高度是: EditorGUIUtility.singleLineHeight
    • 默认行间距是: EditorGUIUtility.standardVerticalSpacing
    • 想要缩进/反缩进 1个单位, 可以使用: EditorGUI.indentLevel++EditorGUI.indentLevel--
    • 对于不需要自定义渲染方式的字段 使用 EditorGUI.PropertyField执行默认的渲染方案,
    • 所有要绘制的内容,推荐使用EditorGUI类, 而不是GUI
      • EditorGUI类的方法, 会自动处理缩进的问题
      • GUI类的方法, 不会自动处理缩进的问题
        • 比如 GUI.Button(rect, "↑")),不受缩进的影响,所有要额外把rect.x加上缩进的距离35比较合适
  • 小技巧

    • 绘制展开 / 折叠按钮
      • 使用EditorGUI.Foldout -> if (foldout = EditorGUI.Foldout(rect, foldout,$"{label.text} 更多内容:{}")
        • 标题行除了显示变量名称以外, 其实可以显示更多信息, 以便在没有展开的情况下就可以把 关键信息 显示到标题行的标题后面
  • ⚠️ 注意:

    • 必须十分清除, 自己的绘制方案, 占用了多少高度, 否则会出现绘制不全的情况
    • 绘制时
      • 关于layout周边的方法
        • EditorGUI.BeginProperty()EditorGUI.EndProperty包裹的范围里,不可以使用 layout 相关的方法, 否则会报错:ArgumentException: Getting control 1's position in a group with only 1 controls when doing repaint
        • EditorGUI.BeginProperty()外面, 执行layout相关的方法, 不生效不显示

标签:缩进,高度,EditorGUI,标题,EditorGUIUtility,绘制,CustomPropertyDrawer
From: https://www.cnblogs.com/NZQLA/p/17640194.html

相关文章