196 lines
7.2 KiB
C#
196 lines
7.2 KiB
C#
using UnityEngine;
|
||
using UnityEditor;
|
||
using System.Collections.Generic;
|
||
using System.Linq; // 用于排序
|
||
using DG.Tweening;
|
||
|
||
[CustomEditor(typeof(DoweenAnimator))]
|
||
public class DoweenAnimatorEditor : Editor
|
||
{
|
||
private List<bool> foldouts = new List<bool>(); // 存储折叠状态
|
||
string[] OnenableOpens = new string[] { "空" , "一唤醒就播放"};
|
||
public override void OnInspectorGUI()
|
||
{
|
||
// 获取目标脚本的引用
|
||
DoweenAnimator myScript = (DoweenAnimator)target;
|
||
|
||
EditorGUILayout.Space();
|
||
|
||
// 初始化折叠列表的大小
|
||
EnsureFoldoutListSize(myScript);
|
||
|
||
// 标记是否需要删除某个效果
|
||
int deleteIndex = -1;
|
||
bool isAnimationEffect = false; // 用于判断删除的是哪种效果
|
||
|
||
// 合并 animationEffects 和 shakeEffects 并按 ListIndex 排序
|
||
List<BaseEffect> sortedEffects = new List<BaseEffect>();
|
||
sortedEffects.AddRange(myScript.animationEffects);
|
||
sortedEffects.AddRange(myScript.shakeEffects);
|
||
sortedEffects = sortedEffects.OrderBy(effect => effect.ListIndex).ToList(); // 按 ListIndex 排序
|
||
|
||
// 遍历排序后的效果列表
|
||
for (int i = 0; i < sortedEffects.Count; i++)
|
||
{
|
||
BaseEffect effect = sortedEffects[i];
|
||
|
||
// 折叠框
|
||
foldouts[i] = EditorGUILayout.Foldout(foldouts[i], $"动画序列{effect.ListIndex}");
|
||
|
||
if (foldouts[i])
|
||
{
|
||
EditorGUILayout.BeginVertical(GUI.skin.box);
|
||
|
||
// 处理 AnimationEffect
|
||
if (effect is AnimationEffect animationEffect)
|
||
{
|
||
EditorGUILayout.LabelField("Animation Effect", EditorStyles.boldLabel);
|
||
animationEffect.animationType = (AnimationEffect.AnimationType)EditorGUILayout.EnumPopup("动画类型", animationEffect.animationType);
|
||
animationEffect.targetValue = EditorGUILayout.Vector3Field("目标值", animationEffect.targetValue);
|
||
animationEffect.duration = EditorGUILayout.FloatField("持续时间", animationEffect.duration);
|
||
animationEffect.easeType = (Ease)EditorGUILayout.EnumPopup("缓动类型", animationEffect.easeType);
|
||
}
|
||
// 处理 ShakeEffect
|
||
else if (effect is ShakeEffect shakeEffect)
|
||
{
|
||
EditorGUILayout.LabelField("Shake Effect", EditorStyles.boldLabel);
|
||
shakeEffect.shakeType = (ShakeEffect.ShakeType)EditorGUILayout.EnumPopup("抖动类型", shakeEffect.shakeType);
|
||
shakeEffect.strength = EditorGUILayout.Vector3Field("抖动强度", shakeEffect.strength);
|
||
shakeEffect.vibrato = EditorGUILayout.IntField("抖动次数", shakeEffect.vibrato);
|
||
shakeEffect.randomness = EditorGUILayout.FloatField("随机性", shakeEffect.randomness);
|
||
shakeEffect.duration = EditorGUILayout.FloatField("持续时间", shakeEffect.duration);
|
||
}
|
||
|
||
// 允许用户更改 ListIndex
|
||
effect.ListIndex = EditorGUILayout.IntField("排序索引", effect.ListIndex);
|
||
|
||
if (GUILayout.Button("删除此效果"))
|
||
{
|
||
deleteIndex = i;
|
||
isAnimationEffect = effect is AnimationEffect;
|
||
}
|
||
|
||
EditorGUILayout.EndVertical();
|
||
}
|
||
|
||
// 标记动画需要更新
|
||
myScript.animationSequenceNeedsUpdate = true;
|
||
}
|
||
|
||
// 处理删除效果
|
||
if (deleteIndex >= 0)
|
||
{
|
||
BaseEffect effectToDelete = sortedEffects[deleteIndex];
|
||
if (isAnimationEffect)
|
||
{
|
||
myScript.animationEffects.Remove((AnimationEffect)effectToDelete);
|
||
}
|
||
else
|
||
{
|
||
myScript.shakeEffects.Remove((ShakeEffect)effectToDelete);
|
||
}
|
||
foldouts.RemoveAt(deleteIndex); // 同时移除折叠状态
|
||
}
|
||
|
||
EditorGUILayout.Space();
|
||
|
||
// 创建横排布局
|
||
EditorGUILayout.BeginHorizontal();
|
||
|
||
// 记录旧的 loopEntireSequence 值
|
||
bool previousLoopEntireSequence = myScript.loopEntireSequence;
|
||
|
||
// 在同一行上显示 "是否循环整个动画序列" 和 "自定义循环类型" (自定义名称)
|
||
myScript.loopEntireSequence = EditorGUILayout.Toggle("循环整个动画", myScript.loopEntireSequence);
|
||
|
||
// 如果 loopEntireSequence 改变了,标记需要更新动画序列
|
||
if (myScript.loopEntireSequence != previousLoopEntireSequence)
|
||
{
|
||
myScript.animationSequenceNeedsUpdate = true;
|
||
Debug.Log("循环整个动画选项已更改,标记动画序列需要更新");
|
||
}
|
||
|
||
// 通过自定义的中文名称显示 LoopType
|
||
LoopType selectedLoopType = myScript.sequenceLoopType;
|
||
string[] loopTypeOptions = new string[] { "重新开始", "往返", "叠加" };
|
||
int selectedIndex = EditorGUILayout.Popup("自定义循环类型", (int)selectedLoopType, loopTypeOptions);
|
||
|
||
// 如果 LoopType 改变了,标记需要更新动画序列
|
||
if ((LoopType)selectedIndex != myScript.sequenceLoopType)
|
||
{
|
||
myScript.sequenceLoopType = (LoopType)selectedIndex;
|
||
myScript.animationSequenceNeedsUpdate = true;
|
||
Debug.Log("循环类型已更改,标记动画序列需要更新");
|
||
}
|
||
|
||
EditorGUILayout.EndHorizontal();
|
||
|
||
EditorGUILayout.BeginHorizontal();
|
||
myScript.OnenableOpen = EditorGUILayout.Popup(new GUIContent("动画模式"), myScript.OnenableOpen, OnenableOpens);
|
||
if (GUILayout.Button("复位"))
|
||
{
|
||
myScript.RestartAnimations();
|
||
}
|
||
EditorGUILayout.EndHorizontal();
|
||
EditorGUILayout.Space();
|
||
|
||
EditorGUILayout.BeginHorizontal();
|
||
// 添加新动画的按钮
|
||
if (GUILayout.Button("添加基础动画"))
|
||
{
|
||
AddAnimationEffect(myScript);
|
||
}
|
||
|
||
// 添加新抖动效果的按钮
|
||
if (GUILayout.Button("添加抖动"))
|
||
{
|
||
AddShakeEffect(myScript);
|
||
}
|
||
EditorGUILayout.EndHorizontal();
|
||
|
||
EditorGUILayout.BeginHorizontal();
|
||
// 添加播放动画的按钮
|
||
if (GUILayout.Button("播放"))
|
||
{
|
||
myScript.PlayAnimations();
|
||
}
|
||
|
||
// 添加暂停动画的按钮
|
||
if (GUILayout.Button("暂停"))
|
||
{
|
||
myScript.StopAnimations();
|
||
}
|
||
EditorGUILayout.EndHorizontal();
|
||
|
||
EditorUtility.SetDirty(myScript); // 标记为已修改,确保Unity保存修改
|
||
}
|
||
|
||
// 添加新的 AnimationEffect
|
||
private void AddAnimationEffect(DoweenAnimator script)
|
||
{
|
||
AnimationEffect newEffect = new AnimationEffect();
|
||
script.animationEffects.Add(newEffect);
|
||
foldouts.Add(true); // 默认展开新的效果
|
||
script.animationSequenceNeedsUpdate = true; // 标记序列需要更新
|
||
}
|
||
|
||
// 添加新的 ShakeEffect
|
||
private void AddShakeEffect(DoweenAnimator script)
|
||
{
|
||
ShakeEffect newEffect = new ShakeEffect();
|
||
script.shakeEffects.Add(newEffect);
|
||
foldouts.Add(true); // 默认展开新的效果
|
||
script.animationSequenceNeedsUpdate = true; // 标记序列需要更新
|
||
}
|
||
|
||
// 确保折叠列表的大小与效果列表匹配
|
||
private void EnsureFoldoutListSize(DoweenAnimator script)
|
||
{
|
||
int totalEffects = script.animationEffects.Count + script.shakeEffects.Count;
|
||
while (foldouts.Count < totalEffects)
|
||
{
|
||
foldouts.Add(true); // 默认展开
|
||
}
|
||
}
|
||
}
|