
示例.jpg
使用方法:

使用方法.png
主要3個文件:
- FieldLabelDrawer;
- EnumAttirbute;
- InspectorShowCN;
FieldLabelDrawer代碼:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;
//將屬性名顯示成中文
[CustomPropertyDrawer(typeof(InspectorShow))]
public class FieldLabelDrawer : PropertyDrawer
{
private InspectorShow FLAttribute
{
get { return (InspectorShow)attribute; }
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
//在這里重新繪制
EditorGUI.PropertyField(position, property, new GUIContent(FLAttribute.label), true);
}
}
//顯示顏色
[CustomPropertyDrawer(typeof(TitleAttribute))]
public class TitleAttributeDrawer : DecoratorDrawer
{
// 文本樣式
private GUIStyle style = new GUIStyle();
public override void OnGUI(Rect position)
{
// 獲取Attribute
TitleAttribute attr = (TitleAttribute)attribute;
// 轉換顏色
Color color = htmlToColor(attr.htmlColor);
// 重繪GUI
position = EditorGUI.IndentedRect(position);
style.normal.textColor = color;
GUI.Label(position, attr.title, style);
}
public override float GetHeight()
{
return base.GetHeight() - 4;
}
/// <summary> Html顏色轉換為Color </summary>
/// <param name="hex"></param>
/// <returns></returns>
private Color htmlToColor(string hex)
{
// 默認黑色
if (string.IsNullOrEmpty(hex)) return Color.black;
// 轉換顏色
hex = hex.ToLower();
if (hex.IndexOf("#") == 0 && hex.Length == 7)
{
int r = Convert.ToInt32(hex.Substring(1, 2), 16);
int g = Convert.ToInt32(hex.Substring(3, 2), 16);
int b = Convert.ToInt32(hex.Substring(5, 2), 16);
return new Color(r / 255f, g / 255f, b / 255f);
}
else if (hex == "red")
{
return Color.red;
}
else if (hex == "green")
{
return Color.green;
}
else if (hex == "blue")
{
return Color.blue;
}
else if (hex == "yellow")
{
return Color.yellow;
}
else if (hex == "black")
{
return Color.black;
}
else if (hex == "white")
{
return Color.white;
}
else if (hex == "cyan")
{
return Color.cyan;
}
else if (hex == "gray")
{
return Color.gray;
}
else if (hex == "grey")
{
return Color.grey;
}
else if (hex == "magenta")
{
return Color.magenta;
}
else
{
return Color.black;
}
}
}
//顯示中文枚舉
[CustomPropertyDrawer(typeof(EnumNameAttribute))]
public class EnumNameDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
// 替換屬性名稱
EnumNameAttribute rename = (EnumNameAttribute)attribute;
label.text = rename.name;
// 重繪GUI
bool isElement = Regex.IsMatch(property.displayName, "Element \\d+");
if (isElement) label.text = property.displayName;
if (property.propertyType == SerializedPropertyType.Enum)
{
drawEnum(position, property, label);
}
else
{
EditorGUI.PropertyField(position, property, label, true);
}
}
// 繪制枚舉類型
private void drawEnum(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginChangeCheck();
// 獲取枚舉相關屬性
Type type = fieldInfo.FieldType;
string[] names = property.enumNames;
string[] values = new string[names.Length];
while (type.IsArray) type = type.GetElementType();
// 獲取枚舉所對應的名稱
for (int i = 0; i < names.Length; i++)
{
FieldInfo info = type.GetField(names[i]);
EnumNameAttribute[] atts = (EnumNameAttribute[])info.GetCustomAttributes(typeof(EnumNameAttribute), false);
values[i] = atts.Length == 0 ? names[i] : atts[0].name;
}
// 重繪GUI
int index = EditorGUI.Popup(position, label.text, property.enumValueIndex, values);
if (EditorGUI.EndChangeCheck() && index != -1) property.enumValueIndex = index;
}
}
EnumAttirbute代碼:
using System;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
using System.Reflection;
using System.Text.RegularExpressions;
#endif
/// <summary>
/// 設置枚舉名稱
/// </summary>
#if UNITY_EDITOR
[AttributeUsage(AttributeTargets.Field)]
#endif
public class EnumAttirbute : PropertyAttribute
{
/// <summary>
/// 枚舉名稱
/// </summary>
public string name;
public EnumAttirbute(string name)
{
this.name = name;
}
}
#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(EnumAttirbute))]
public class EnumNameDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
// 替換屬性名稱
EnumAttirbute enumAttirbute = (EnumAttirbute)attribute;
label.text = enumAttirbute.name;
bool isElement = Regex.IsMatch(property.displayName, "Element \\d+");
if (isElement)
{
label.text = property.displayName;
}
if (property.propertyType == SerializedPropertyType.Enum)
{
DrawEnum(position, property, label);
}
else
{
EditorGUI.PropertyField(position, property, label, true);
}
}
/// <summary>
/// 重新繪制枚舉類型屬性
/// </summary>
/// <param name="position"></param>
/// <param name="property"></param>
/// <param name="label"></param>
private void DrawEnum(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginChangeCheck();
Type type = fieldInfo.FieldType;
string[] names = property.enumNames;
string[] values = new string[names.Length];
while (type.IsArray)
{
type = type.GetElementType();
}
for (int i = 0; i < names.Length; ++i)
{
FieldInfo info = type.GetField(names[i]);
EnumAttirbute[] enumAttributes = (EnumAttirbute[])info.GetCustomAttributes(typeof(EnumAttirbute), false);
values[i] = enumAttributes.Length == 0 ? names[i] : enumAttributes[0].name;
}
int index = EditorGUI.Popup(position, label.text, property.enumValueIndex, values);
if (EditorGUI.EndChangeCheck() && index != -1)
{
property.enumValueIndex = index;
}
}
}
#endif
InspectorShow代碼:
using System;
using UnityEngine;
//顯示中文
[AttributeUsage(AttributeTargets.Field)]
public class InspectorShow : PropertyAttribute
{
public string label; //要顯示的字符
/// <summary>
/// 屬性面板顯示中文
/// </summary>
/// <param name="label"></param>
public InspectorShow(string label)
{
this.label = label;
}
}
//顯示顏色
public class TitleAttribute : PropertyAttribute
{
/// <summary> 標題名稱 </summary>
public string title;
/// <summary> 標題顏色 </summary>
public string htmlColor;
/// <summary> 在屬性上方添加一個標題 </summary>
/// <param name="title">標題名稱</param>
/// <param name="htmlColor">標題顏色</param>
public TitleAttribute(string title, string htmlColor = "#FFFFFF")
{
this.title = title;
this.htmlColor = htmlColor;
}
}
//顯示枚舉名稱
[AttributeUsage(AttributeTargets.Field)]
public class EnumNameAttribute : PropertyAttribute
{
/// <summary> 枚舉名稱 </summary>
public string name;
public new int[] order = new int[0];
/// <summary>
/// 枚舉屬性面板顯示中文
/// </summary>
/// <param name="name"></param>
public EnumNameAttribute(string name)
{
this.name = name;
}
public EnumNameAttribute(string label, params int[] order)
{
this.name = label;
this.order = order;
}
}