简体   繁体   English

在Unity中使用Coroutine

[英]Using Coroutine in Unity

Am working on Unity and here is what I want to do: play the animationType in a time difference of 10sec. 我正在使用Unity,这就是我想要做的事情:以10秒的时差播放animationType I want the code to loop through the animations and play them each for 10seconds. 我希望代码循环播放动画并每次播放它们10秒。 The code runs without errors, except the result is not what I expected it to be. 代码运行没有错误,除了结果不是我预期的结果。 It plays the first animation, Boxing , for 10 seconds and just when it's about to play the Backflip animation, it starts to do some weird thing to the character. 它播放第一个动画, 拳击 ,持续10秒,当它即将播放Backflip动画时,它开始对角色做一些奇怪的事情。 That's where is goes wrong. 这就是出错的地方。

Here is my code: 这是我的代码:

public class BeBot_Controller : MonoBehaviour
{    

    Animator anim;
    string animationType;
    string[] split;
    int arrayLength;

    void Start()
    {
        //AndroidJavaClass pluginClass = new AndroidJavaClass("yenettaapp.my_bebot_plugin.My_BeBot_Plugin");
        //animationType = pluginClass.CallStatic<string>("getMessage");
        animationType="Null,Boxing,Backflip";
        split = animationType.Split(',');
        anim = gameObject.GetComponentInChildren<Animator> ();
        arrayLength = split.Length;

    }

    // Update is called once per frame
    void Update () {
        if (arrayLength > 1){
            StartCoroutine ("LoopThroughAnimation");
        }
    }

    IEnumerator LoopThroughAnimation()
    {
        for (int i = 1 ; i < arrayLength; i++) {
            animationType = split [i];
            //anim.SetInteger ("AnimPar", 0);
            anim.Play (animationType);
            yield return new WaitForSeconds (10);
        }
    }
}

So what did I do wrong here ? 那我在这里做错了什么? Is there any other way I can solve this problem ? 还有其他办法可以解决这个问题吗?

Since your animation loop only needs to be called once, simply move StartCoroutine() to Start() and remove the Update() stuff: 由于您的动画循环只需要调用一次,只需将StartCoroutine()移动到Start()并删除Update()内容:

public class BeBot_Controller  : MonoBehaviour
{
    private Animator anim;
    private string animationType;
    private string[] split;
    private int arrayLength;

    void Start ()
    {
        //AndroidJavaClass pluginClass = new AndroidJavaClass("yenettaapp.my_bebot_plugin.My_BeBot_Plugin");
        //animationType = pluginClass.CallStatic<string>("getMessage");
        animationType = "Null,Boxing,Backflip";
        split = animationType.Split(',');
        anim = gameObject.GetComponentInChildren<Animator>();
        arrayLength = split.Length;

        // Call here
        StartCoroutine(LoopThroughAnimation());
    }

    IEnumerator LoopThroughAnimation ()
    {
        for (int i = 1; i < arrayLength; i++)
        {
            animationType = split[i];
            Debug.Log(animationType);

            //anim.SetInteger ("AnimPar", 0);
            anim.Play(animationType);

            yield return new WaitForSeconds(10);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM