Unity 显示时间(00:00)

Mathf

        Mathf.CeiToInt (返回一个大于或等于输入值的最小整数)

        Mathf.RoundToInt (返回一个舍入的值,根据输入的值判断)

String

        string.Format (将字符串中的一个或多个格式项替换为指定对象的字符串表示形式)

本次方法的思想为

        判断秒数和分数的字符串长度如果是1个单位长度的话就是小于10,反之则大于等于10

totalSeconds = 时间总数;

//  将int类型转换为string类型
string secondsPart = (totalSeconds % 60).ToString();

string minsPart = Mathf.RoundToInt(totalSeconds / 60).ToString();

//string testNameA = "A";
//Debug.Log(testNameA.Length);//1

if (secondsPart.Length == 1)

{

        secondsPart = "0" + secondsPart;

}

if (minsPart.Length == 1)
        {
            minsPart = "0" + minsPart;
        }

timerText.text = string.Format("{0}:{1}", minsPart, secondsPart);

猜你喜欢

转载自blog.csdn.net/qq_24977805/article/details/122230487
00