Health/Assets/Scripts/UI/Component/GudieAnimationManager.cs

149 lines
3.9 KiB
C#
Raw Normal View History

2023-11-27 05:49:53 +00:00
using System;
using System.Collections;
2023-11-07 13:55:35 +00:00
using System.Collections.Generic;
using UnityEngine;
2023-11-27 18:00:13 +00:00
using UnityEngine.UIElements;
2023-11-07 13:55:35 +00:00
public class GudieAnimationManager : MonoBehaviour
2023-11-27 05:49:53 +00:00
{
2023-11-07 13:55:35 +00:00
private Animator _animator;
2023-11-10 08:12:37 +00:00
private string _currName;
2023-11-27 18:00:13 +00:00
private bool _isAnimationStartPlay;
2023-11-27 05:49:53 +00:00
private List<AvatarAction> _actionList = new List<AvatarAction>();
2023-11-27 18:00:13 +00:00
private List<string> _actionNameList = new List<string>();
private float _animationLength = 0;
public float AnimationLength
{
get
{
if (_animationLength == 0)
{
AnimationClip[] clips = Animator.runtimeAnimatorController.animationClips;
foreach (AnimationClip clip in clips)
{
if (_actionNameList.Contains(clip.name))
{
_animationLength += clip.length;
}
}
}
return _animationLength;
}
}
2023-11-27 05:49:53 +00:00
public Animator Animator
{
get
{
if (_animator == null)
{
_animator = transform.GetComponentInChildren<Animator>();
}
return _animator;
}
}
2023-11-07 13:55:35 +00:00
2023-11-27 18:00:13 +00:00
public List<string> ActionNameList
{
get
{
if (_actionNameList.Count == 0)
{
foreach (var item in Enum.GetNames(typeof(AvatarAction)))
{
_actionNameList.Add(item);
}
}
return _actionNameList;
}
}
2023-11-07 13:55:35 +00:00
private void Awake()
{
_animator = transform.GetComponentInChildren<Animator>();
2023-11-27 18:00:13 +00:00
_isAnimationStartPlay = false;
2023-11-07 13:55:35 +00:00
}
2023-11-27 18:00:13 +00:00
2023-11-07 13:55:35 +00:00
public void Play(string name)
{
2023-11-27 05:49:53 +00:00
Animator.CrossFade(name, 0.5f);
2023-11-10 08:12:37 +00:00
_currName = name;
}
2023-11-27 05:49:53 +00:00
public void PlayCurrentActionList(List<AvatarAction> actionList)
{
2023-11-27 18:00:13 +00:00
_isAnimationStartPlay = true;
2023-11-27 05:49:53 +00:00
_actionList = actionList;
2023-11-27 18:00:13 +00:00
AnimationClip[] clips = Animator.runtimeAnimatorController.animationClips;
_animationLength = 0;
foreach (AnimationClip clip in clips)
{
if (_actionNameList.Contains(clip.name))
{
_animationLength += clip.length;
}
}
2023-11-27 05:49:53 +00:00
Play(Enum.GetName(typeof(AvatarAction), _actionList[YogaManager.Instance.ActionIndex]));
}
2023-11-10 08:12:37 +00:00
private void Update()
{
if (string.IsNullOrEmpty(_currName))
return;
2023-11-27 18:00:13 +00:00
if (!_isAnimationStartPlay)
return;
var currName = GetCurrentAnimationName();
if (!ActionNameList.Contains(currName))
{
Play(ActionNameList[YogaManager.Instance.ActionIndex]);
return;
}
2023-11-10 08:12:37 +00:00
//当未达到指标且动画播放完毕时,重新播放
2023-11-27 18:00:13 +00:00
if (Animator.GetCurrentAnimatorStateInfo(0).normalizedTime >= 1.0f && GetCurrentAnimationName().Equals(Enum.GetName(typeof(AvatarAction), _actionList[YogaManager.Instance.ActionIndex])))
2023-11-10 08:12:37 +00:00
{
2023-11-27 05:49:53 +00:00
//如果列表动画index小于列表长度播放下一个动画
YogaManager.Instance.ActionIndex++;
//如果列表动画index大于等于列表长度说明动画播放完毕停止播放
if (YogaManager.Instance.ActionIndex >= YogaManager.Instance.LevelData.Actions.Count)
2023-11-10 08:12:37 +00:00
{
2023-11-27 05:49:53 +00:00
Stop();
return;
2023-11-10 08:12:37 +00:00
}
2023-11-27 05:49:53 +00:00
Play(Enum.GetName(typeof(AvatarAction), _actionList[YogaManager.Instance.ActionIndex]));
2023-11-10 08:12:37 +00:00
}
2023-11-07 13:55:35 +00:00
}
2023-11-27 18:00:13 +00:00
private string GetCurrentAnimationName()
{
if (Animator == null)
{
return string.Empty;
}
var clipInfo = Animator.GetCurrentAnimatorClipInfo(0);
if (clipInfo.Length > 0)
{
var name = clipInfo[0].clip.name;
return name;
}
return string.Empty;
}
2023-11-10 07:17:23 +00:00
public void Stop()
{
2023-11-27 05:49:53 +00:00
Animator.StopPlayback();
2023-11-10 07:17:23 +00:00
}
2023-11-07 13:55:35 +00:00
}