Unity编辑器拓展(二) Property Drawers

官方文档 PropertyDrawers
Unity编辑器拓展(一)
Unity编辑器拓展(二)
Unity编辑器拓展(三)

绘制属性 Property Drawers

绘制属性可用于脚本上的属性,在 Inspector 窗口中自定义某些控件的外观,或者控制特定的可序列化类的外观。

绘制属性有两种用途:1.为可序列化类的每个实例自定义 GUI。2.为脚本成员属性自定义 GUI。

为可序列化类的每个实例自定义 GUI

如果脚本中属性是自定义类,在 Inspector 中是不会显示的,如果需要显示,我们可用 Serializable 修饰。

示例1:

using System; // for Serializable

public enum IngredientUnit { Spoon, Cup, Bowl, Piece }

[Serializable]
public class Ingredient {
    public string name;
    public int amount = 1;
    public IngredientUnit unit;
}

public class GameRecipe : MonoBehaviour {
    public Ingredient potionResult;
    public Ingredient[] potionIngredients;

}

正确显示如下图所示:


我们可以使用 CustomPropertyDrawer 来自定义我们需要的显示效果。如示例2:

[CustomPropertyDrawer(typeof(Ingredient))]
public class IngredientDrawer : PropertyDrawer {
    
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {

        EditorGUI.BeginProperty(position, label, property); // 开始绘制属性

        var indent = EditorGUI.indentLevel; // 用来缓存修改之前的缩进值
        EditorGUI.indentLevel = 0;          // 修改缩进为 0,不缩进

        // 获取属性前值 label, 就是显示在此属性前面的那个名称 label
        position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label); 
        // 定义此属性的子框体区域
        var amountRect = new Rect(position.x, position.y, 30, position.height);
        var unitRect = new Rect(position.x + 35, position.y, 50, position.height);
        var nameRect = new Rect(position.x + 90, position.y, position.width - 90, position.height);

        // 绘制子属性:PropertyField参数依次是: 框体位置,对应属性,显示的label
        // 如果你要显示一个label,第三个参数: new GUIContent("xxx")
        EditorGUI.PropertyField(amountRect, property.FindPropertyRelative("amount"), GUIContent.none);
        EditorGUI.PropertyField(unitRect, property.FindPropertyRelative("unit"), GUIContent.none);
        EditorGUI.PropertyField(nameRect, property.FindPropertyRelative("name"), GUIContent.none);

        EditorGUI.indentLevel = indent;   // 恢复缩进

        EditorGUI.EndProperty();          // 完成绘制
    }
}

显示效果图:

为脚本成员属性自定义 GUI

使用 Property Attributes 来给脚本的成员属性自定义GUI。你可以使用内建的和自定义的。

内建属性有:

  • Range() 将一个值指定在一定的范围内,在Inspector面板中为其添加滑块
  • Multiline() 为一个string指定多行
  • Header() 为属性添加属性标题
  • Tooltip() 为属性添加提示语
  • Space() 添加指定行距
  • ContextMenu() 为脚本添加菜单到右上角工具按钮
  • TextArea() 添加TextArea

示例3:

public class GameRecipe : MonoBehaviour {

    public Ingredient potionResult;
    public Ingredient[] potionIngredients;

    [Header("Nick")]
    public string nickName;
    [Multiline(3)]
    public string description;
    [Space(20)]
    [Range(0f, 10f), Tooltip("this is weight")]
    public float weight;
    [TextArea]
    public string information;

    [ContextMenu("Show Infomation")]
    void ShowInfo() {
        Debug.Log(information);
    }
}

效果如图:

自定义 PropertyAttribute:1.派生 PropertyAttribute 并定义此类。2. 在使用 CustomPropertyDrawer 实现绘制。
这种实现和之前的示例2类似就不展示示例了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,053评论 3 119
  • 思路: 1.广告页加载思路。广告页的内容要实时显示,在无网络状态或者网速缓慢的情况下不能延迟加载,或者等到首页出现...
    流年啊奈我何阅读 4,940评论 0 1
  • 最后一次见你是昨天,距离今天才短短一天,可我觉得已经过了好久好久。你我之间仿佛隔着大片大片的海,遥不可及。 多害怕...
    汤水阅读 1,616评论 0 0
  • ◆★◆钛酸锂真密度测试仪,锌汞材料真密度测试仪,纳米碳酸钙真密度测试仪,镍钴酸锂真密度测试仪,氧化钠真密度测试仪◆...
    63999ee42c15阅读 1,828评论 0 0
  • [话筒]【曾艳芬安利日历の1114期】 怀了孩子,方知我得把全身最精粹的一切贡献给这个新的生命。在低等动物,新生命...
    曾艳芬微光站阅读 1,598评论 0 0

友情链接更多精彩内容