Unity中实现在规定时间内从一个值递增到另一个值
1、进度条(在规定时间内完成进度条)
private Image progressBar; private float currentProgress = 0; /// <summary> /// 进度条需要持续的时长 /// </summary> private float duration = 5f;//这个时间可以任意定义 private void ProgressBar() { currentProgress += Time.deltaTime; progressBar.fillAmount = currentProgress / duration; if (currentProgress >= duration) { progressBar.fillAmount = 1; } }
2、数值递增(在规定时间内从0递增到115)
private Text showText;//展示的Text private float time = 5f;//指定的时间 private float currentVaule = 0f;//起始值 private float targetVaule = 115f;//目标值 /// <summary> /// 数据递增 /// </summary> public void DataIncrement() { currentVaule += Time.deltaTime * (targetVaule / time); if (currentVaule >= targetVaule) { currentVaule = targetVaule; } showText.text = currentVaule.ToString("0000.00"); }
注:以上方法需放在Update中调用执行