unity 自定义特性,在Inspector面板显示自定义提示信息

本文介绍如何在Unity中创建自定义特性CustomLabelAttribute,并使用自定义的PropertyDrawer来实现界面元素的定制显示。通过这种方式可以增强编辑器的灵活性和易用性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、自定义特性

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 自定义特性
/// </summary>
public class CustomLabelAttribute : PropertyAttribute
{
    public string content;
    public string tooltip;
    public CustomLabelAttribute(string content,string tooltip = "")
    {
        this.content = content;
        this.tooltip = tooltip;
    }
}

2、定制显示方式

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Reflection;

[CustomPropertyDrawer(typeof(CustomLabelAttribute))]
public class CustomLabelDrawer : PropertyDrawer
{
    private GUIContent _label = null;

    public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    {
        float height = 0;
        if (property.isExpanded)
        {
            SerializedProperty iterator = property.Copy();
            int depth = iterator.depth;
            bool enterChildren = true;
            do
            {
                height += EditorGUI.GetPropertyHeight(iterator, null, true) + EditorGUIUtility.standardVerticalSpacing;
                enterChildren = false;
            }
            while (iterator.NextVisible(enterChildren) && (depth < iterator.depth));
        }
        else
        {
            height = EditorGUI.GetPropertyHeight(property, null, true);
        }
        return height;
    }

    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        if (_label == null)
        {
            string content = (attribute as CustomLabelAttribute).content;
            string tooltip = (attribute as CustomLabelAttribute).tooltip;
            _label = new GUIContent(content, tooltip);
        }

        if (!property.hasVisibleChildren)
        {
            EditorGUI.PropertyField(position, property, _label);
        }
        else
        {
            property.isExpanded = EditorGUI.Foldout(new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight), property.isExpanded, _label);

            if (property.isExpanded)
            {
                float offsetY = EditorGUIUtility.singleLineHeight;
                SerializedProperty iterator = property.Copy();
                int depth = iterator.depth;
                bool enterChildren = true;
                while (iterator.NextVisible(enterChildren) && (depth < iterator.depth))
                {
                    Rect childRect = new Rect(position.x, position.y + offsetY, position.width, EditorGUI.GetPropertyHeight(iterator, null, true));
                    EditorGUI.PropertyField(childRect, iterator, true);
                    offsetY += EditorGUI.GetPropertyHeight(iterator, null, true) + EditorGUIUtility.standardVerticalSpacing;
                    enterChildren = false;
                }
            }
            else
            {
                EditorGUI.PropertyField(position, property, _label);
            }
        }
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值