139 lines
4.7 KiB
C#
139 lines
4.7 KiB
C#
using System.Collections.Generic;
|
||
using DefaultNamespace.Anima;
|
||
using DG.Tweening;
|
||
using UnityEngine;
|
||
|
||
namespace DefaultNamespace.ProcessMode
|
||
{
|
||
public class AnimationManager : MonoBehaviour
|
||
{
|
||
// 使用字典存储动画组,以便根据名称查询
|
||
private Dictionary<string, Sequence> animationGroupsByName = new Dictionary<string, Sequence>();
|
||
private List<Sequence> animationGroups = new List<Sequence>(); // 存储动画组的列表
|
||
private int currentGroupIndex = -1; // 当前播放的动画组索引
|
||
private bool isPlaying = false; // 是否正在播放动画
|
||
|
||
// 添加动画组并指定组名,接受带回调的 Lambda 表达式
|
||
public void AddAnimationGroup(string groupName, System.Action<Sequence> sequenceCallback)
|
||
{
|
||
Sequence group = DOTween.Sequence(); // 创建一个新的Sequence
|
||
|
||
// 调用传入的 Lambda 表达式,允许用户定义序列的内容
|
||
sequenceCallback?.Invoke(group);
|
||
|
||
group.Pause(); // 初始状态暂停
|
||
animationGroups.Add(group); // 将Sequence添加到列表中
|
||
animationGroupsByName[groupName] = group; // 将Sequence添加到字典中,以组名为键
|
||
}
|
||
|
||
// 根据名称播放动画组
|
||
public void PlayAnimationGroup(string groupName)
|
||
{
|
||
// 检查是否存在该名称的动画组
|
||
if (!animationGroupsByName.ContainsKey(groupName))
|
||
{
|
||
Debug.LogError($"找不到动画组 '{groupName}'");
|
||
return;
|
||
}
|
||
|
||
// 如果正在播放其他动画组,先完成当前组
|
||
if (isPlaying)
|
||
{
|
||
Debug.Log("当前正在播放,将先完成当前组。");
|
||
CompleteCurrentGroup();
|
||
}
|
||
|
||
currentGroupIndex = -1; // 重置当前组索引(因为是按名称播放)
|
||
isPlaying = true; // 设置正在播放标志
|
||
PlayCurrentGroup(groupName); // 播放指定名称的动画组
|
||
}
|
||
|
||
// 根据索引播放动画组
|
||
public void PlayAnimationGroup(int groupIndex)
|
||
{
|
||
// 检查索引是否有效
|
||
if (groupIndex < 0 || groupIndex >= animationGroups.Count)
|
||
{
|
||
Debug.LogError($"无效的组索引: {groupIndex}");
|
||
return;
|
||
}
|
||
|
||
// 如果正在播放其他动画组,先完成当前组
|
||
if (isPlaying)
|
||
{
|
||
Debug.Log("当前正在播放,将先完成当前组。");
|
||
CompleteCurrentGroup();
|
||
}
|
||
|
||
currentGroupIndex = groupIndex; // 设置当前播放的组索引
|
||
isPlaying = true; // 设置正在播放标志
|
||
PlayCurrentGroup(); // 播放当前组
|
||
}
|
||
|
||
// 播放指定名称的动画组
|
||
private void PlayCurrentGroup(string groupName)
|
||
{
|
||
Sequence currentGroup = animationGroupsByName[groupName]; // 获取指定名称的动画组
|
||
Debug.Log($"正在播放动画组 '{groupName}'");
|
||
currentGroup.OnComplete(OnGroupComplete).Restart(); // 在完成时触发回调,并重新播放动画组
|
||
}
|
||
|
||
// 播放当前索引指定的动画组
|
||
private void PlayCurrentGroup()
|
||
{
|
||
Sequence currentGroup = animationGroups[currentGroupIndex]; // 获取当前索引的动画组
|
||
Debug.Log($"正在播放动画组 {currentGroupIndex}");
|
||
currentGroup.OnComplete(OnGroupComplete).Restart(); // 在完成时触发回调,并重新播放动画组
|
||
}
|
||
|
||
// 动画组播放完成时的回调
|
||
private void OnGroupComplete()
|
||
{
|
||
if (currentGroupIndex >= 0 && currentGroupIndex < animationGroups.Count)
|
||
{
|
||
Debug.Log($"动画组 {currentGroupIndex} 播放完成");
|
||
}
|
||
else
|
||
{
|
||
Debug.Log("动画组播放完成");
|
||
}
|
||
|
||
isPlaying = false; // 设置播放完成标志
|
||
}
|
||
|
||
// 完成当前正在播放的动画组
|
||
private void CompleteCurrentGroup()
|
||
{
|
||
if (currentGroupIndex >= 0 && currentGroupIndex < animationGroups.Count)
|
||
{
|
||
Sequence currentGroup = animationGroups[currentGroupIndex]; // 获取当前组
|
||
currentGroup.Complete(true); // 完成当前组的播放
|
||
|
||
currentGroupIndex = -1; // 重置当前组索引
|
||
}
|
||
else
|
||
{
|
||
Debug.LogWarning("没有有效的组可以完成。");
|
||
}
|
||
|
||
isPlaying = false; // 设置播放完成标志
|
||
}
|
||
|
||
// 重置当前正在播放的动画组
|
||
private void ResetCurrentGroup()
|
||
{
|
||
if (currentGroupIndex >= 0 && currentGroupIndex < animationGroups.Count)
|
||
{
|
||
Sequence currentGroup = animationGroups[currentGroupIndex]; // 获取当前组
|
||
currentGroup.ResetSequence(); // 重置当前组的播放状态
|
||
}
|
||
else
|
||
{
|
||
Debug.LogWarning("没有有效的组可以重置。");
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
|