数值变化跳动效果

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);
        }
    }
相关推荐
_瑞7 小时前
深入理解 iOS 渲染原理
前端·ios
IT_陈寒7 小时前
SpringBoot自动配置失灵?你可能忘了这个关键注解
前端·人工智能·后端
iCOD3R7 小时前
Skill - kill-ai-slop 解决“AI 味”样式
前端·css·ai编程
皮音8 小时前
Skill 在项目中的实践 —— 从 Agent 困境到 Skill 工程化
前端
大龄秃头程序员8 小时前
RN 0.86 新架构实战:TurboModule + Fabric 组件 + iOS 原生容器,完整代码开源
前端·react native
这是个栗子8 小时前
前端开发中的常用工具函数(八)
开发语言·前端·javascript
Hilaku8 小时前
Vue 和 React 真正的差距,不在语法,而在团队犯错成本
前端·javascript·程序员
研☆香8 小时前
为什么变量声明在赋值前也能使用?—— 深入理解 JavaScript 变量提升与作用域
开发语言·前端·javascript
Liora_Yvonne9 小时前
目录结构到底怎么设计?别再把 components 和 utils 当垃圾桶了
前端
柯克七七9 小时前
我把 bug 修得太干净了,测试组以为这个模块本来就没问题
前端·javascript·typescript