This commit is contained in:
parent
4366914e60
commit
ee2bdaacaf
|
@ -0,0 +1,170 @@
|
|||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: fd131cf52c804b2ea7cd1989dc2fedd7
|
||||
timeCreated: 1718763357
|
|
@ -0,0 +1,51 @@
|
|||
using System.Collections.Generic;
|
||||
using DefaultNamespace.ProcessMode;
|
||||
using DefaultNamespace.ProcessMode.DefaultNamespace.ProcessMode;
|
||||
using DG.Tweening;
|
||||
using UnityEngine;
|
||||
|
||||
namespace DefaultNamespace
|
||||
{
|
||||
public class TestAnimations : MonoBehaviour
|
||||
{
|
||||
private AnimationManager animationManager;
|
||||
|
||||
public GameObject[] ggg;
|
||||
|
||||
void Start()
|
||||
{
|
||||
animationManager = gameObject.AddComponent<AnimationManager>();
|
||||
|
||||
List<Tween> group3 = new List<Tween>
|
||||
{
|
||||
transform.DOMove(new Vector3(7, 2, 0), 1).SetAutoKill(false).Pause().OnComplete(() => Debug.Log("3_1")),
|
||||
transform.DOMove(new Vector3(7, 2, 0), 1).SetAutoKill(false).Pause().OnComplete(() => Debug.Log("3_1")),
|
||||
transform.DOMove(new Vector3(7, 2, 0), 1).SetAutoKill(false).Pause().OnComplete(() => Debug.Log("3_1")),
|
||||
};
|
||||
animationManager.AddAnimationGroup(group3);
|
||||
|
||||
Debug.Log("开始播放第一个动画组");
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (Input.GetKeyDown(KeyCode.A))
|
||||
{
|
||||
Debug.Log("按下A");
|
||||
animationManager.PlayAnimationGroup(0);
|
||||
}
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.B))
|
||||
{
|
||||
Debug.Log("按下B");
|
||||
animationManager.PlayAnimationGroup(1);
|
||||
}
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.C))
|
||||
{
|
||||
Debug.Log("按下空格键");
|
||||
animationManager.PlayAnimationGroup(2);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 3ada1c4df57d4cb3ad8027691b47e949
|
||||
timeCreated: 1718763747
|
Loading…
Reference in New Issue