用Untiy自带的函数实现分数的动态变化模块,数值由a到b的动态变化,需要提供目标数值以及时间

用Unity自带的函数实现,有两种方案方

方案一:使用协程(实际操作没有成功)

方案二:使用Update(在Update中实现)

以下代码是在Update中实现的:

    /// 
    /// 目标数值
    /// 
    int _target;
    /// 
    /// 变化所需要的时间
    /// 
    float temp;
    /// 
    /// 变化的时间
    /// 
    float ok = 0;
    /// 
    /// 计算出每个数字所需要的时间
    /// 
    /// 
    /// 
    public void ChangeNumber(int target ,float time)
    {
        _target = target;
        int cha = target - curValue;//变化的数值差
        temp = ((float)time / (float)cha);
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            ChangeNumber(5, 20.0f);//鼠标按下调用此函数,传递数值和时间
        }
        if (curValue < _target)//条件判断
        {
            ok += 0.02f;
            if (ok >= temp)
            {
                curValue += 1;//自增
                txt.text = curValue.ToString();
                ok = 0;
            }
        }
    }

 

你可能感兴趣的:(用Untiy自带的函数实现分数的动态变化模块,数值由a到b的动态变化,需要提供目标数值以及时间)