数值变化跳动效果

csharp 复制代码
 public Text txt;

    private int num;

    private float _timers=10;
    private int _target;

    public void Set(float index)
    {
        txt.text = index.ToString();
    }

    public void Go(int target)
    {
        _target = target;
        num = int.Parse(txt.text);
        StopCoroutine("IReduce");
        StartCoroutine("IReduce");
    }

    IEnumerator IReduce()
    {
        float offset = (num - _target) / _timers;
        if (Mathf.Abs(offset) < 1)
        {
            txt.text = _target.ToString();
            yield break;
        }

        for (int i = 0; i < _timers; i++)
        {
            num =(int)(num-offset);
            txt.text = num.ToString();
            if (i == _timers-1)
            {
                txt.text = _target.ToString();
            }
            yield return new WaitForSeconds(0.1f);
        }
    }
相关推荐
Kagol2 小时前
🎉OpenTiny NEXT-SDK 重磅发布:四步把你的前端应用变成智能应用!
前端·开源·agent
GIS之路4 小时前
ArcGIS Pro 中的 notebook 初识
前端
JavaGuide4 小时前
7 道 RAG 基础概念知识点/面试题总结
前端·后端
ssshooter4 小时前
看完就懂 useSyncExternalStore
前端·javascript·react.js
格砸5 小时前
从入门到辞职|从ChatGPT到OpenClaw,跟上智能时代的进化
前端·人工智能·后端
Live000006 小时前
在鸿蒙中使用 Repeat 渲染嵌套列表,修改内层列表的一个元素,页面不会更新
前端·javascript·react native
柳杉6 小时前
使用Ai从零开发智慧水利态势感知大屏(开源)
前端·javascript·数据可视化
兆子龙6 小时前
从高阶函数到 Hooks:React 如何减轻开发者的心智负担(含 Demo + ahooks 推荐)
前端
狗胜6 小时前
测试文章 - API抓取
前端