39 lines
1.4 KiB
C#
39 lines
1.4 KiB
C#
using UnityEngine;
|
|
using UnityEditor;
|
|
|
|
|
|
[CustomPropertyDrawer(typeof(CNNameAttribute))]
|
|
public class CNNameDrawer : PropertyDrawer
|
|
{
|
|
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
|
|
{
|
|
// 1. 获取特性中的中文名
|
|
CNNameAttribute attr = (CNNameAttribute)attribute;
|
|
|
|
// 2. 替换原本的 Label
|
|
GUIContent newLabel = new GUIContent(attr.Name, label.tooltip);
|
|
|
|
// 3. 绘制属性(使用中文名)
|
|
EditorGUI.PropertyField(position, property, newLabel, true);
|
|
|
|
// 4. 绘制变量原名(灰色小字,显示在 Label 区域的右侧)
|
|
// 计算 Label 的区域
|
|
float labelWidth = EditorGUIUtility.labelWidth;
|
|
Rect labelRect = new Rect(position.x, position.y, labelWidth, position.height);
|
|
|
|
// 设置小字样式
|
|
GUIStyle subStyle = new GUIStyle(EditorStyles.miniLabel);
|
|
subStyle.normal.textColor = new Color(0.5f, 0.5f, 0.5f, 0.6f); // 半透明灰色
|
|
subStyle.alignment = TextAnchor.MiddleRight; // 右对齐
|
|
subStyle.fontSize = 9;
|
|
subStyle.padding = new RectOffset(0, 2, 0, 0); //稍微留点边距
|
|
|
|
// 绘制变量名 (例如: "maxHP")
|
|
GUI.Label(labelRect, property.name, subStyle);
|
|
}
|
|
|
|
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
|
|
{
|
|
return EditorGUI.GetPropertyHeight(property, label);
|
|
}
|
|
} |