Unity 利用Coroutine實現跳動數字效果


純粹轉載:轉載注明參考鏈接!

參考鏈接:http://xataxnova.blog.163.com/blog/static/236620063201451061738122/,作者:網易博客 xataxnova

 

示例代碼:(將該腳本綁在一個Lable對象上,即可測試,顯示為從1跳到10)

 

using UnityEngine;
using System.Collections;

public class test : MonoBehaviour 
{
    int result = 0;
    private int start = 1;
    private int end = 10;
    private int jumpTimes = 9;

    private UILabel label = null;
    void Start()
    {
        label = gameObject.GetComponent<UILabel>();
        StartCoroutine(JumpNumber());
    }

    public IEnumerator JumpNumber()
    {
        int delta = (end - start) / jumpTimes;
        result = 0;
        
        for (int i = 0; i < jumpTimes; i++)
        {
            result += delta;
            label.text = result.ToString();
            yield return new WaitForSeconds(1);
        }

        result = end;
        label.text = result.ToString();
        StopCoroutine(JumpNumber());
    }
}

   代碼中 yield return new WaitForSeconds(1);表示返回1秒,若寫成yield return1;表示返回1幀。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM