149 lines
3.9 KiB
C#
149 lines
3.9 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using UnityEngine.UIElements;
|
||
|
||
public class GudieAnimationManager : MonoBehaviour
|
||
{
|
||
private Animator _animator;
|
||
private string _currName;
|
||
private bool _isAnimationStartPlay;
|
||
private List<AvatarAction> _actionList = new List<AvatarAction>();
|
||
|
||
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;
|
||
}
|
||
}
|
||
|
||
public Animator Animator
|
||
{
|
||
get
|
||
{
|
||
if (_animator == null)
|
||
{
|
||
_animator = transform.GetComponentInChildren<Animator>();
|
||
}
|
||
return _animator;
|
||
}
|
||
}
|
||
|
||
public List<string> ActionNameList
|
||
{
|
||
get
|
||
{
|
||
if (_actionNameList.Count == 0)
|
||
{
|
||
foreach (var item in Enum.GetNames(typeof(AvatarAction)))
|
||
{
|
||
_actionNameList.Add(item);
|
||
}
|
||
}
|
||
return _actionNameList;
|
||
}
|
||
}
|
||
|
||
|
||
private void Awake()
|
||
{
|
||
_animator = transform.GetComponentInChildren<Animator>();
|
||
_isAnimationStartPlay = false;
|
||
}
|
||
|
||
|
||
|
||
public void Play(string name)
|
||
{
|
||
Animator.CrossFade(name, 0.5f);
|
||
_currName = name;
|
||
}
|
||
|
||
public void PlayCurrentActionList(List<AvatarAction> actionList)
|
||
{
|
||
_isAnimationStartPlay = true;
|
||
_actionList = actionList;
|
||
|
||
AnimationClip[] clips = Animator.runtimeAnimatorController.animationClips;
|
||
_animationLength = 0;
|
||
foreach (AnimationClip clip in clips)
|
||
{
|
||
if (_actionNameList.Contains(clip.name))
|
||
{
|
||
_animationLength += clip.length;
|
||
}
|
||
}
|
||
|
||
|
||
Play(Enum.GetName(typeof(AvatarAction), _actionList[YogaManager.Instance.ActionIndex]));
|
||
}
|
||
|
||
private void Update()
|
||
{
|
||
if (string.IsNullOrEmpty(_currName))
|
||
return;
|
||
|
||
if (!_isAnimationStartPlay)
|
||
return;
|
||
|
||
var currName = GetCurrentAnimationName();
|
||
if (!ActionNameList.Contains(currName))
|
||
{
|
||
Play(ActionNameList[YogaManager.Instance.ActionIndex]);
|
||
return;
|
||
}
|
||
|
||
//当未达到指标且动画播放完毕时,重新播放
|
||
if (Animator.GetCurrentAnimatorStateInfo(0).normalizedTime >= 1.0f && GetCurrentAnimationName().Equals(Enum.GetName(typeof(AvatarAction), _actionList[YogaManager.Instance.ActionIndex])))
|
||
{
|
||
//如果列表动画index小于列表长度,播放下一个动画
|
||
YogaManager.Instance.ActionIndex++;
|
||
//如果列表动画index大于等于列表长度,说明动画播放完毕,停止播放
|
||
if (YogaManager.Instance.ActionIndex >= YogaManager.Instance.LevelData.Actions.Count)
|
||
{
|
||
Stop();
|
||
return;
|
||
}
|
||
|
||
Play(Enum.GetName(typeof(AvatarAction), _actionList[YogaManager.Instance.ActionIndex]));
|
||
}
|
||
}
|
||
|
||
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;
|
||
}
|
||
|
||
public void Stop()
|
||
{
|
||
Animator.StopPlayback();
|
||
}
|
||
}
|