它没有获得动画剪辑长度,而是动画片状态长度。 我等了5秒但是我想等到每个州完成比赛。
yield return new WaitForSeconds(5);
相反,我希望获得动画[索引]长度。但是anim [index]没有任何长度属性。
第二个目标是让while循环无穷大,这样它就会不停地再次播放所有状态。
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor.Animations;
using UnityEngine;
public class SwitchAnimations : MonoBehaviour
{
private Animator animator;
private static UnityEditor.Animations.AnimatorController controller;
private AnimatorState[] states;
// Use this for initialization
void Start()
{
animator = GetComponent<Animator>();
states = GetStateNames(animator);
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
StartCoroutine(QueueAnim(states));
}
}
private static UnityEditor.Animations.AnimatorState[] GetStateNames(Animator animator)
{
controller = animator ? animator.runtimeAnimatorController as UnityEditor.Animations.AnimatorController : null;
return controller == null ? null : controller.layers.SelectMany(l => l.stateMachine.states).Select(s => s.state).ToArray();
}
IEnumerator QueueAnim(params AnimatorState[] anim)
{
int index = 0;
while (index < anim.Length)
{
if (index == anim.Length)
index = 0;
animator.Play(anim[index].name);
yield return new WaitForSeconds(5);
index++;
}
}
private void RollSound()
{
}
private void CantRotate()
{
}
private void EndRoll()
{
}
private void EndPickup()
{
}
}
更新: 这就是我现在尝试的:
但是当我按下A时它只播放一个状态,如果我在A上多次点击,那么它将播放另一个状态,但就是这样。
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor.Animations;
using UnityEngine;
public class SwitchAnimations : MonoBehaviour
{
private Animator animator;
private static UnityEditor.Animations.AnimatorController controller;
private AnimatorState[] states;
// Use this for initialization
void Start()
{
animator = GetComponent<Animator>();
states = GetStateNames(animator);
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
StartCoroutine(QueueAnim(states));
}
}
private static UnityEditor.Animations.AnimatorState[] GetStateNames(Animator animator)
{
controller = animator ? animator.runtimeAnimatorController as UnityEditor.Animations.AnimatorController : null;
return controller == null ? null : controller.layers.SelectMany(l => l.stateMachine.states).Select(s => s.state).ToArray();
}
IEnumerator QueueAnim(AnimatorState[] anim)
{
int index = 0;
while (index < anim.Length)
{
if (index == anim.Length)
index = 0;
animator.Play(anim[index].name);
string name = anim[index].name;
while (animator.GetCurrentAnimatorStateInfo(0).IsName(name) && animator.GetCurrentAnimatorStateInfo(0).normalizedTime < 1.0f)
{
//Wait every frame until animation has finished
yield return null;
}
index++;
}
}
private void RollSound()
{
}
private void CantRotate()
{
}
private void EndRoll()
{
}
private void EndPickup()
{
}
}
答案 0 :(得分:2)
我等了5秒但是我想等到每个州完成 播放。
为了做到这一点,你不需要获得状态长度。您可以使用GetCurrentAnimatorStateInfo
和normalizedTime
来检查动画师何时完成播放。只需使用以下代码替换yield return new WaitForSeconds(5);
:
string name = anim[index].name;
while (animator.GetCurrentAnimatorStateInfo(0).IsName(name) && animator.GetCurrentAnimatorStateInfo(0).normalizedTime < 1.0f)
{
//Wait every frame until animation has finished
yield return null;
}
如果你想让它永远循环,只需把它放在另一个while
循环中。
不相关,但我建议您在使用Unity时避免在函数中使用params
。它根本不值得。只需使用数组。原因与性能有很大关系,这取决于调用函数的频率。
答案 1 :(得分:0)
试试这个,在那里你可以将长度保存为变量并继续使用
http://answers.unity3d.com/questions/692593/get-animation-clip-length-using-animator.html