Unity Custom Property Label 自定义属性显示
代码
Test.cs
1
2
3
4
5
6
7
8public class Test : MonoBehaviour
{
// 使用方式
[Label("数量")]
public int num;
[Label("名字")]
public string name;
}LabelAttribute.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class LabelAttribute : PropertyAttribute
{
public string Name { get; }
public LabelAttribute(string name)
{
Name = name;
}
}
#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(LabelAttribute))]
public class LabelAttributeDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
// label显示名字
if (attribute is LabelAttribute attr && attr.Name.Length > 0)
{
label.text = attr.Name;
}
// 属性
EditorGUI.PropertyField(position, property, label);
}
}
#endif
参考
Unity Custom Property Label 自定义属性显示
https://automask.github.io/wild/2022/06/20/lab/S_Unity_Custom_Attribute/