簡體   English   中英

Unity 播放動畫選定的次數

[英]Unity playing animation a chosen amount of times

所以我嘗試通過腳本多次播放我的動畫,但唯一發生的事情是它運行卡住並需要由任務管理器關閉。 我的代碼:

    int NeededAni;
    public Animator Sheep;


    void Start()
    {
        PickRandomNumber(9);

        StartCoroutine(PlaySheepInterval(NeededAni, 0F));
    }

    private IEnumerator PlaySheepInterval (int n, float Time)
    {
        while (n > 0)
        {
            Sheep.SetFloat("SceneLoaded", 1f);
            --n;
            Debug.Log(n);
            yield return new WaitForSeconds(Time);
        }

        while (n <= 0)
        {
            Sheep.SetFloat("SceneLoaded", 0f);
        }
    }

    public void PickRandomNumber(int maxInt)
    {
        NeededAni = Random.Range(1, maxInt + 1);
        Debug.Log(NeededAni);
    }

你怎么能解決這個問題? PS:Sceneloaded是動畫師附帶的參數。 PS:浮動“waitForSeconds”是必要的,但我並沒有真正使用它。

好的,所以我找到了解決方案。

    void Start()
    {

        StartCoroutine(PlaySheepInterval(NeededAni, 3.2F));

    }

    private IEnumerator PlaySheepInterval(int n, float time)
    {
        while (n > 0)
        {
            Sheep.SetFloat("SceneLoaded", 1f);
            n = n - 1;
            yield return new WaitForSeconds(time);
            Debug.Log(n);
        }

       if (n <= 0)
        {
            Sheep.SetFloat("SceneLoaded", 0f);
        }
    }

3.2F 是動畫的持續時間,因此當動畫完成時,每 3.2 幀 n 值就會減少 1。 來源

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM