/// <summary>
/// 指定数值逐渐刷新至目标数值
/// </summary>
/// <param name="startValue">起始数值</param>
/// <param name="targetValue">目标数值</param>
/// <param name="timer">数值刷新间隔时间</param>
/// <param name="frequency">数值刷新次数</param>
/// <returns></returns>
IEnumerator RefreshNum(float startValue, float targetValue, float timer, int frequency)
{
//差值
float differenceValue = Mathf.Abs(startValue - targetValue);
if ((startValue - targetValue) > 0)//起始数值大于目标数值 递减
{
while (startValue > targetValue)
{
yield return new WaitForSeconds(timer);
startValue -= (differenceValue) / frequency;
if (startValue < targetValue)
{
startValue = targetValue;
}
Debug.Log("递减: "+System.String.Format("{0:F1}", startValue));
}
}//起始数值大小于目标数值 递増
else
{
while (startValue < targetValue)
{
yield return new WaitForSeconds(timer);
startValue += (differenceValue) / frequency;
if (startValue > targetValue)
{
startValue = targetValue;
}
Debug.Log("递增: " + System.String.Format("{0:F1}", startValue));
}
}
}
PS:读取时刷新数值时,碰巧刷新的数值时整数时,有的方法保留一位小数不好用,下面是整理内容
//eg:显示保留一位小数
//1,当值碰巧为整数时,不显示小数点
float.ToString("0.#");
//2,当值碰巧为整数时,不显示小数点
Math.Round(float, 1);
//2,当值碰巧为整数时,依然显示小数点后一位
String.Format("{0:F1}", float);