Unity 根据枚举展示不同内容(可操作数组类型版)

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("卡牌说明"));
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值