想到一個有趣的寫法, 來把物件上的提示弄得美觀一點.
直接把 Unity3D 的 Hotkey 拿來玩了.
Result:
Note.cs
using UnityEngine; namespace Kit { public class Note : MonoBehaviour { private enum eMessageType { None = 0, Info = 1, Warning = 2, Error = 3 } [SerializeField] eMessageType type = eMessageType.Info; [SerializeField] string note; } }
NoteEditor.cs
using UnityEngine; using UnityEditor; namespace Kit { [CustomEditor(typeof(Note))] public class NoteEditor : Editor { SerializedProperty noteProp, typeProp; void OnEnable() { noteProp = serializedObject.FindProperty("note"); typeProp = serializedObject.FindProperty("type"); } public override void OnInspectorGUI() { if (string.IsNullOrEmpty(noteProp.stringValue)) EditorGUILayout.HelpBox("Modify : Ctrl + Shift + A", MessageType.Info); else EditorGUILayout.HelpBox(noteProp.stringValue, (MessageType)typeProp.enumValueIndex); if (!((Component)target).gameObject.activeSelf) { EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(typeProp); noteProp.stringValue = EditorGUILayout.TextArea(noteProp.stringValue, GUILayout.MinHeight(100f), GUILayout.ExpandHeight(true)); if (EditorGUI.EndChangeCheck()) { serializedObject.ApplyModifiedProperties(); serializedObject.Update(); } } } } }