Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何获得特定于animator的状态长度?如何使循环成为无穷大?_C#_Animation_Unity3d_Unity5 - Fatal编程技术网

C# 如何获得特定于animator的状态长度?如何使循环成为无穷大?

C# 如何获得特定于animator的状态长度?如何使循环成为无穷大?,c#,animation,unity3d,unity5,C#,Animation,Unity3d,Unity5,它不是获取动画片段长度,而是获取动画师状态长度。 我等了5秒钟,但我想等到每个州结束比赛 yield return new WaitForSeconds(5); 相反,我想得到动画[索引]长度。但是anim[index]没有任何长度属性 第二个目标是使while循环无限大,这样它将继续不停地重复播放所有状态 using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEdit

它不是获取动画片段长度,而是获取动画师状态长度。 我等了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()
    {

    }
}
使用系统集合;
使用System.Collections.Generic;
使用System.Linq;
使用UnityEdit.Animations;
使用UnityEngine;
公共类:单行为
{
私人动画师;
专用静态UnityEdit.Animations.AnimatorController控制器;
私有动画州[]个州;
//用于初始化
void Start()
{
animator=GetComponent();
states=GetStateNames(animator);
}
//每帧调用一次更新
无效更新()
{
if(Input.GetKeyDown(KeyCode.A))
{
STARTCOUTION(队列动画(状态));
}
}
私有静态UnityEdit.Animations.AnimatorState[]GetStateNames(Animator Animator)
{
控制器=animator?animator.runtimeAnimatorController作为UnityEdit.Animations.AnimatorController:null;
返回controller==null?null:controller.layers.SelectMany(l=>l.stateMachine.states)。Select(s=>s.state.ToArray();
}
IEnumerator队列动画(参数AnimatorState[]动画)
{
int指数=0;
while(索引<动画长度)
{
如果(索引==动画长度)
指数=0;
animator.Play(anim[index].name);
产生返回新WaitForSeconds(5);
索引++;
}
}
私人声音
{
}
私有void CantRotate()
{
}
私有void EndRoll()
{
}
私家车
{
}
}
更新: 这就是我现在尝试的:

但当我按下一次按钮时,它只播放一种状态,如果我多次点击,它将播放另一种状态,但就是这样

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()
    {

    }
}
使用系统集合;
使用System.Collections.Generic;
使用System.Linq;
使用UnityEdit.Animations;
使用UnityEngine;
公共类:单行为
{
私人动画师;
专用静态UnityEdit.Animations.AnimatorController控制器;
私有动画州[]个州;
//用于初始化
void Start()
{
animator=GetComponent();
states=GetStateNames(animator);
}
//每帧调用一次更新
无效更新()
{
if(Input.GetKeyDown(KeyCode.A))
{
STARTCOUTION(队列动画(状态));
}
}
私有静态UnityEdit.Animations.AnimatorState[]GetStateNames(Animator Animator)
{
控制器=animator?animator.runtimeAnimatorController作为UnityEdit.Animations.AnimatorController:null;
返回controller==null?null:controller.layers.SelectMany(l=>l.stateMachine.states)。Select(s=>s.state.ToArray();
}
IEnumerator队列动画(AnimatorState[]anim)
{
int指数=0;
while(索引<动画长度)
{
如果(索引==动画长度)
指数=0;
animator.Play(anim[index].name);
字符串名称=anim[index].name;
while(animator.GetCurrentAnimatorStateInfo(0).IsName(名称)&&animator.GetCurrentAnimatorStateInfo(0).normalizedTime<1.0f)
{
//等待每一帧,直到动画完成
收益返回空;
}
索引++;
}
}
私人声音
{
}
私有void CantRotate()
{
}
私有void EndRoll()
{
}
私家车
{
}
}

试试这个,你可以将长度保存为变量并继续使用


试试这个,你可以将长度保存为一个变量并继续使用它

我正在等待5秒钟,但我想等到每个状态结束 玩

这样做不需要获取状态长度。您可以使用
GetCurrentAnimatorStateInfo
normalizedTime
来检查动画师何时完成播放。只需替换
yield返回新的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;
}
string name=anim[index].name;
while(animator.GetCurrentAnimatorStateInfo(0).IsName(名称)&&animator.GetCurrentAnimatorStateInfo(0).normalizedTime<1.0f)
{
//等待每一帧,直到动画完成
收益返回空;
}
如果你想让它永远循环,只要把它放在另一个
的while
循环中就行了

不相关,但我建议您在使用Unity时避免在函数中使用
params
。这根本不值得。只需使用数组即可。原因与性能有很大关系,这取决于调用函数的频率

我正在等待5秒钟,但我想等到每个状态结束 玩

这样做不需要获取状态长度。您可以使用
GetCurrentAnimatorStateInfo
normalizedTime
来检查动画师何时完成播放。只需替换
yield返回新的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;
}
string name=anim[index].name;
while(animator.GetCurrentAnimatorStateInfo(0).IsName(名称)&&animator.GetCurrentAnimatorStateInfo(0).normalizedTime<1.0f)
{
//等待每一帧,直到动画完成
收益返回空;
}
如果你想让它永远循环,只要把它放在另一个
的while
循环中就行了

不相关,但我建议您在使用Unity时避免在函数中使用
params
。这根本不值得。只需使用数组即可。原因与性能有很大关系,这取决于调用函数的频率。

Un