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);
}
}