ShanxiKnowledgeBase/SXElectricityFaultA&E/Assets/Scripts/ProcessMode/AnimationManager.cs

170 lines
5.6 KiB
C#

using System;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
namespace DefaultNamespace.ProcessMode
{
using System;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
namespace DefaultNamespace.ProcessMode
{
// public class TweenWrapper
// {
// public Tween Tween { get; private set; }
// public Action OnCompleteAction { get; private set; }
//
// public TweenWrapper(Tween tween, Action onCompleteAction)
// {
// Tween = tween;
// OnCompleteAction = onCompleteAction;
// }
//
// public void Play()
// {
// Tween.Restart();
// Tween.OnComplete(() => OnCompleteAction?.Invoke());
// }
//
// public void Complete(bool withCallbacks = false)
// {
// if (withCallbacks && OnCompleteAction != null)
// {
// OnCompleteAction.Invoke();
// }
// }
//
// public bool IsCompleted()
// {
// return Tween.IsComplete();
// }
//
// public void Reset()
// {
// Tween.Rewind();
// }
// }
public class AnimationManager : MonoBehaviour
{
private List<List<Tween>> animationGroups = new List<List<Tween>>();
private int currentGroupIndex = -1;
private int currentTweenIndex = -1;
private bool isPlaying = false;
public void AddAnimationGroup(List<Tween> group)
{
animationGroups.Add(group);
}
public void PlayAnimationGroup(int groupIndex)
{
Debug.Log($"尝试播放动画组 {groupIndex}");
if (groupIndex < 0 || groupIndex >= animationGroups.Count)
{
Debug.LogError("无效的组索引");
return;
}
Debug.Log("PlayAnimationGroup 调用的索引: " + groupIndex);
if (isPlaying)
{
Debug.Log("当前正在播放,将先完成当前组。");
CompleteCurrentGroup();
}
if (groupIndex == currentGroupIndex && animationGroups[currentGroupIndex][0].IsComplete())
{
Debug.Log("动画已完成,重新播放。");
ResetCurrentGroup();
}
currentGroupIndex = groupIndex;
currentTweenIndex = 0;
isPlaying = true;
PlayNextTween();
}
private void PlayNextTween()
{
if (currentGroupIndex < 0 || currentGroupIndex >= animationGroups.Count)
{
Debug.LogWarning("没有有效的组可播放。");
isPlaying = false;
return;
}
List<Tween> currentGroup = animationGroups[currentGroupIndex];
if (currentTweenIndex < 0 || currentTweenIndex >= currentGroup.Count)
{
Debug.LogWarning("当前组中没有有效的动画可播放。");
isPlaying = false;
return;
}
Tween tweenWrapper = currentGroup[currentTweenIndex];
Debug.Log($"正在播放组 {currentGroupIndex} 中的动画 {currentTweenIndex}");
tweenWrapper.OnComplete(OnTweenComplete).Play();
}
private void OnTweenComplete()
{
Debug.Log($"动画 {currentTweenIndex} 完成");
animationGroups[currentGroupIndex][currentTweenIndex].onComplete.Invoke();
currentTweenIndex++;
if (currentTweenIndex >= animationGroups[currentGroupIndex].Count)
{
Debug.Log("已完成当前组中的所有动画。");
isPlaying = false;
}
else
{
PlayNextTween();
}
}
private void CompleteCurrentGroup()
{
if (currentGroupIndex < 0 || currentGroupIndex >= animationGroups.Count)
{
Debug.LogWarning("没有有效的组可完成。");
return;
}
List<Tween> currentGroup = animationGroups[currentGroupIndex];
for (int i = currentTweenIndex; i < currentGroup.Count; i++)
{
Debug.Log($"正在完成组 {currentGroupIndex} 中的动画 {i}");
currentGroup[i].Complete(true);
// currentGroup[i].OnCompleteAction?.Invoke(); // 手动触发 OnCompleteAction
}
currentGroupIndex = -1;
currentTweenIndex = -1;
isPlaying = false;
}
private void ResetCurrentGroup()
{
if (currentGroupIndex < 0 || currentGroupIndex >= animationGroups.Count)
{
Debug.LogWarning("没有有效的组可重置。");
return;
}
List<Tween> currentGroup = animationGroups[currentGroupIndex];
foreach (var tweenWrapper in currentGroup)
{
tweenWrapper.Rewind();
}
}
}
}
}