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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
| using UnityEngine; using UnityEditor;
[CanEditMultipleObjects] [CustomEditor(typeof(PlayerCharacter))] public class PlayerCharacterEditor : Editor { private SerializedProperty playerIdProp; private SerializedProperty playerNameProp; private SerializedProperty backStoryProp; private SerializedProperty healthProp; private SerializedProperty damageProp; private SerializedProperty weaponDamage1Prop; private SerializedProperty weaponDamage2Prop; private SerializedProperty shoeNameProp; private SerializedProperty shoeSizeProp; private SerializedProperty shoeTypeProp;
private bool showWeapons = false; private bool showEquipment = true;
private void OnEnable() { playerIdProp = serializedObject.FindProperty("playerId"); playerNameProp = serializedObject.FindProperty("playerName"); backStoryProp = serializedObject.FindProperty("backStory"); healthProp = serializedObject.FindProperty("health"); damageProp = serializedObject.FindProperty("damage"); weaponDamage1Prop = serializedObject.FindProperty("weaponDamage1"); weaponDamage2Prop = serializedObject.FindProperty("weaponDamage2"); shoeNameProp = serializedObject.FindProperty("shoeName"); shoeSizeProp = serializedObject.FindProperty("shoeSize"); shoeTypeProp = serializedObject.FindProperty("shoeType"); }
public override void OnInspectorGUI() { serializedObject.Update();
EditorGUILayout.LabelField("角色属性编辑器", EditorStyles.boldLabel); EditorGUILayout.Space();
EditorGUILayout.LabelField("=== 基本信息 ===", EditorStyles.boldLabel); EditorGUILayout.PropertyField(playerIdProp, new GUIContent("角色 ID")); EditorGUILayout.PropertyField(playerNameProp, new GUIContent("角色名称"));
EditorGUILayout.Space();
EditorGUILayout.LabelField("背景故事"); EditorGUILayout.PropertyField(backStoryProp, GUIContent.none, GUILayout.MinHeight(60));
EditorGUILayout.Space();
EditorGUILayout.LabelField("=== 战斗属性 ===", EditorStyles.boldLabel);
EditorGUILayout.Slider(healthProp, 0, 100, new GUIContent("生命值"));
float healthValue = healthProp.floatValue; Color healthColor = GetHealthColor(healthValue);
Color originalColor = GUI.color; GUI.color = healthColor;
Rect healthRect = GUILayoutUtility.GetRect(200, 20); EditorGUI.ProgressBar(healthRect, healthValue / 100f, $"生命值: {(int)healthValue}%");
GUI.color = originalColor; EditorGUILayout.Space();
EditorGUILayout.Slider(damageProp, 0, 20, new GUIContent("伤害值"));
float damageValue = damageProp.floatValue; if (damageValue < 5) { EditorGUILayout.HelpBox("伤害过低,可能无法击败敌人!", MessageType.Error); } else if (damageValue > 15) { EditorGUILayout.HelpBox("伤害过高,可能影响游戏平衡!", MessageType.Warning); }
EditorGUILayout.Space();
showWeapons = EditorGUILayout.Foldout(showWeapons, "武器", true); if (showWeapons) { EditorGUI.indentLevel++; EditorGUILayout.PropertyField(weaponDamage1Prop, new GUIContent("主武器伤害")); EditorGUILayout.PropertyField(weaponDamage2Prop, new GUIContent("副武器伤害")); EditorGUI.indentLevel--; }
EditorGUILayout.Space();
showEquipment = EditorGUILayout.Foldout(showEquipment, "装备", true); if (showEquipment) { EditorGUI.indentLevel++;
EditorGUILayout.BeginHorizontal(); { EditorGUILayout.LabelField("鞋子名称", GUILayout.MaxWidth(70)); EditorGUILayout.PropertyField(shoeNameProp, GUIContent.none); } EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal(); { EditorGUILayout.LabelField("鞋子尺寸", GUILayout.MaxWidth(70)); EditorGUILayout.PropertyField(shoeSizeProp, GUIContent.none, GUILayout.MaxWidth(100)); EditorGUILayout.LabelField("类型", GUILayout.MaxWidth(50)); EditorGUILayout.PropertyField(shoeTypeProp, GUIContent.none); } EditorGUILayout.EndHorizontal();
EditorGUI.indentLevel--; }
serializedObject.ApplyModifiedProperties(); }
private Color GetHealthColor(float health) { if (health < 20) return new Color(1f, 0.3f, 0.3f); else if (health < 50) return new Color(1f, 0.8f, 0.3f); else if (health < 80) return new Color(1f, 1f, 0.3f); else return new Color(0.3f, 0.8f, 0.3f); } }
|