1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| using UnityEngine; using UnityEditor;
[CustomPropertyDrawer(typeof(PersonInfo))] public class PersonInfoDrawer : PropertyDrawer { private Rect nameRect; private Rect genderRect; private Rect ageRect; private Rect descRect;
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { using (new EditorGUI.PropertyScope(position, label, property)) { SerializedProperty nameProp = property.FindPropertyRelative("name"); SerializedProperty genderProp = property.FindPropertyRelative("gender"); SerializedProperty ageProp = property.FindPropertyRelative("age"); SerializedProperty descProp = property.FindPropertyRelative("description");
float lineHeight = EditorGUIUtility.singleLineHeight; float spacing = EditorGUIUtility.standardVerticalSpacing;
nameRect = new Rect(position.x, position.y, position.width, lineHeight);
float halfWidth = position.width / 2f - spacing / 2f; genderRect = new Rect(position.x, position.y + lineHeight + spacing, halfWidth, lineHeight); ageRect = new Rect(position.x + halfWidth + spacing, position.y + lineHeight + spacing, halfWidth, lineHeight);
float descY = position.y + (lineHeight + spacing) * 2; float descHeight = position.height - (lineHeight + spacing) * 2; descRect = new Rect(position.x, descY, position.width, descHeight);
EditorGUI.PropertyField(nameRect, nameProp, new GUIContent("姓名"));
EditorGUI.PropertyField(genderRect, genderProp, new GUIContent("性别")); EditorGUI.PropertyField(ageRect, ageProp, new GUIContent("年龄"));
EditorGUI.PropertyField(descRect, descProp, new GUIContent("描述")); } }
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { int lines = 4; return EditorGUIUtility.singleLineHeight * lines + EditorGUIUtility.standardVerticalSpacing * (lines - 1); } }
|