using UnityEditor; using UnityEngine; [CustomEditor(typeof(MonoBehaviour), true)] public class ScriptDescriptionEditor : Editor { private GUIStyle descriptionStyle; private void OnEnable() { descriptionStyle = new GUIStyle(EditorStyles.label) { wordWrap = true, alignment = TextAnchor.MiddleCenter, normal = { background = MakeTex(2, 2, new Color(0.1509434f, 0.1473834f, 0.1473834f, 1f)), textColor = Color.white }, padding = new RectOffset(10, 10, 10, 10), margin = new RectOffset(0, 0, 10, 10) }; } private Texture2D MakeTex(int width, int height, Color col) { Color[] pix = new Color[width * height]; for (int i = 0; i < pix.Length; i++) pix[i] = col; Texture2D result = new Texture2D(width, height); result.SetPixels(pix); result.Apply(); return result; } public override void OnInspectorGUI() { // 获取目标脚本的类型 var type = target.GetType(); // 获取 ScriptDescription 属性 var descriptionAttribute = (ScriptDescriptionAttribute)System.Attribute.GetCustomAttribute(type, typeof(ScriptDescriptionAttribute)); // 如果属性存在,显示描述 if (descriptionAttribute != null) { // EditorGUILayout.HelpBox(descriptionAttribute.Description, MessageType.Info); // EditorGUILayout.LabelField(descriptionAttribute.Description, EditorStyles.wordWrappedLabel); // EditorGUILayout.Space(); GUILayout.Label(descriptionAttribute.Description, descriptionStyle); } // 绘制默认的 Inspector DrawDefaultInspector(); } }