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