起因
Unity 的Asset 文件如果设置为 readonly 是不可以 Serialize 的,而且在编辑器中,有些属性不希望被修改,这篇文章主要解决上述的两个问题
运行时的 “readonly”
但是下面的做法会导致在Editor中显示为 <Test>k__BackingField
[CreateAssetMenu(fileName = "GameData.asset", menuName = "GameConfig/GameData")]
public class InitializeGameData : ScriptableObject {
[field: SerializeField]
public int Test { get; private set; }
}
修复Editor 显示问题
这里需要重新绘制Test属性的显示,Title用于修改List对象的显示名,EnumName用于修改属性的显示名,这里使用EnumName
using UnityEngine;
#if UNITY_EDITOR
using System;
using UnityEditor;
using System.Reflection;
using System.Text.RegularExpressions;
#endif
#if UNITY_EDITOR
[AttributeUsage(AttributeTargets.Field)]
#endif
public class TitleAttribute : PropertyAttribute {
public string title;
public string htmlColor;
public TitleAttribute(string title, string htmlColor = "#FFFFFF") {
this.title = title;
this.htmlColor = htmlColor;
}
}
#if UNITY_EDITOR
[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;
}
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;
}
}
}
#endif
#if UNITY_EDITOR
[AttributeUsage(AttributeTargets.Field)]
#endif
public class EnumNameAttribute : PropertyAttribute {
/// <summary> 枚举名称 </summary>
public string name;
public EnumNameAttribute(string name) {
this.name = name;
}
}
#if UNITY_EDITOR
[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;
}
}
#endif
实际使用如下
[field: SerializeField, EnumName("Test")]
public int Test { get; private set; }
Editor 模式下的 ReadOnly
这里使用Editor提供的API设置为Editor模式下不可修改
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
public class ReadOnlyAttribute : PropertyAttribute {
}
#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(ReadOnlyAttribute))]
public class ReadOnlyDrawer : PropertyDrawer {
public override float GetPropertyHeight(SerializedProperty property,
GUIContent label) {
return EditorGUI.GetPropertyHeight(property, label, true);
}
public override void OnGUI(Rect position,
SerializedProperty property,
GUIContent label) {
GUI.enabled = false;
EditorGUI.PropertyField(position, property, label, true);
GUI.enabled = true;
}
}
#endif
实际使用
[ReadOnly] public int Test;