显示剩余的精确时间

时间:2013-02-05 15:18:42

标签: unity3d countdowntimer unityscript

我有以下代码,我想添加miliSeconds var,所以它会显示如下:

分钟:秒:miliSeconds

2:35:98

Time.time是从app开始经过的时间,每帧都会调用update函数。

var startTime:float;
var timeRemaining:float;

var minutes:int;
var seconds:int;
var miliSeconds:int;
var timeStr:String;

function Start () {
    startTime = 130.0;
}

function Update () {
    timeRemaining = startTime - Time.time;

    minutes = timeRemaining / 60;
    seconds = timeRemaining % 60;
    //miliSeconds = ?
    timeStr = minutes.ToString()+":"+seconds.ToString("D2");

    guiText.text = timeStr;
}

1 个答案:

答案 0 :(得分:0)

如果我理解你正在尝试做什么,那么试试这段代码:

var startTime:float;
var timeRemaining:float;

var minutes:int;
var seconds:int;
var miliSeconds:int;
var timeStr:String;

function Start () {
    startTime = 130.0;
}

function Update () {
    timeRemaining = startTime - Time.time;

    minutes = timeRemaining / 60;
    seconds = timeRemaining;        
    miliSeconds = timeRemaining * 1000;

    seconds -= minutes*60;
    miliSeconds -= seconds*1000;

    timeStr = minutes.ToString()+":"+seconds.ToString("D2")+":"+miliSeconds.ToString();

    guiText.text = timeStr;
}