Unity editor 操作
代码如下:
using UnityEngine;
using UnityEditor;
using System;
using System.Collections;
using System.Collections.Generic;
[CustomEditor(typeof(CardAI))]//关联脚本
[CanEditMultipleObjects]
public class EffectInspector : Editor
{
private struct EffectInfo {
public SerializedProperty
effectType,
targetType,
effectRange,
damageArgs,
healArgs,
restoreArgs,
buffArgs,
debuffArgs,
dispelArgs,
collectArgs,
stealArgs;
public EffectInfo(SerializedProperty effect) {
this.effectType = effect.FindPropertyRelative("effectType");
this.targetType = effect.FindPropertyRelative("targetType");
this.effectRange = effect.FindPropertyRelative("effectRange");
this.damageArgs = effect.FindPropertyRelative("damageArgs");
this.healArgs = effect.FindPropertyRelative("healArgs");
this.restoreArgs = effect.FindPropertyRelative("restoreArgs");
this.buffArgs = effect.FindPropertyRelative("buffArgs");
this.debuffArgs = effect.FindPropertyRelative("debuffArgs");
this.dispelArgs = effect.FindPropertyRelative("dispelArgs");
this.collectArgs = effect.FindPropertyRelative("collectArgs");
this.stealArgs = effect.FindPropertyRelative("stealArgs");
}
}
private SerializedObject targetIns;//序列化
private SerializedProperty id,name,type,info,icon;//一定显示的属性
private List<EffectInfo> effectList = new List<EffectInfo>();
private SerializedProperty effects;
private bool foldout;//特效列表是否展开?
private List<bool> singleEffectFoldOut = new List<bool>();//单个特效参数表是否展开?
private CardAI cardAi;
private int effectNum;//效果数量
void OnEnable()
{
targetIns = new SerializedObject(target);
cardAi = target as CardAI;
InitSafeProps();
foreach (SerializedProperty prop in effects) {
if (prop.name != "size") {
effectList.Add(new EffectInfo(prop));
}
}
}
private void InitSafeProps() {
//初始化安全属性列表
id = targetIns.FindProperty("id");
name = targetIns.FindProperty("name");
type = targetIns.FindProperty("type");
info = targetIns.FindProperty("info");
icon = targetIns.FindProperty("icon");
effects = targetIns.FindProperty("effects");
}
public override void OnInspectorGUI()
{
//刷新页面
targetIns.Update();
//更新安全列表
UpdateSafeProps();
//更新Effects列表
foldout = EditorGUILayout.Foldout(foldout, "效果列表");
if (foldout)
{
//展开
if (Selection.activeTransform) {
effectNum = EditorGUILayout.IntField("效果数量", effectNum);
if (effectNum != 0)
{
//循环生成对应个数的特效参数表
for (int i = 0; i < effectNum; i++)
{
//新建一个特效
Effect effect;
//原类中是否存在该特效?
if (cardAi.effects.Count>i)
//存在则取原值
effect = cardAi.effects[i];
else{
//不存在就新建并放入
effect = new Effect();
cardAi.effects.Add(effect);
}
//检测特效参数表是否展开的列表增加一个长度
while (singleEffectFoldOut.Count <= i)
singleEffectFoldOut.Add(false);
//检测该特效参数表是否展开?
singleEffectFoldOut[i] = EditorGUILayout.Foldout(singleEffectFoldOut[i], "效果 " + (i + 1) + ":");
if (singleEffectFoldOut[i])
{
//特效参数表展开
effect.effectType = (Effect.EffectType)EditorGUILayout.EnumPopup("特效类型:", effect.effectType);
effect.targetType = (Effect.TargetType)EditorGUILayout.EnumPopup("目标类型:", effect.targetType);
effect.effectRange = EditorGUILayout.FloatField("效果范围:",effect.effectRange);
//EditorGUILayout.PropertyField((SerializedProperty)effectList[i].GetType().GetProperty(effect.effectType.ToString()));
switch (effect.effectType.ToString()) {
case "Damage":
EditorGUILayout.PropertyField(effectList[i].damageArgs,new GUIContent("伤害参数:"));
break;
case "Heal":
EditorGUILayout.PropertyField(effectList[i].healArgs,new GUIContent("治疗参数:"));
break;
case "Restore":
EditorGUILayout.PropertyField(effectList[i].restoreArgs, new GUIContent("恢复参数:"));
break;
case "Buff":
EditorGUILayout.PropertyField(effectList[i].buffArgs, new GUIContent("增益参数:"));
break;
case "Debuff":
EditorGUILayout.PropertyField(effectList[i].debuffArgs, new GUIContent("减弱参数:"));
break;
case "Dispel":
EditorGUILayout.PropertyField(effectList[i].dispelArgs, new GUIContent("驱散参数:"));
break;
case "Collect":
EditorGUILayout.PropertyField(effectList[i].collectArgs, new GUIContent("采集参数:"));
break;
case "Steal":
EditorGUILayout.PropertyField(effectList[i].stealArgs, new GUIContent("偷窃参数:"));
break;
}
}
}
}
else {
//清除列表
singleEffectFoldOut.Clear();
}
}
}
targetIns.ApplyModifiedProperties();
}
private void UpdateSafeProps() {
EditorGUILayout.PropertyField(id,new GUIContent("卡牌ID"));
EditorGUILayout.PropertyField(name, new GUIContent("卡牌名称"));
EditorGUILayout.PropertyField(type, new GUIContent("卡牌类型"));
EditorGUILayout.PropertyField(icon, new GUIContent("卡牌图标"));
EditorGUILayout.PropertyField(info, new GUIContent("卡牌说明"));
}
}