Unity中获取游戏时间并显示成秒表格式显示在界面上

把该脚本直接托给要显示的文本上即可
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GetTime : MonoBehaviour
{

private int hour;
private int minute;
private int second;
private int millisecond;

//已经花费的时间
float timeSpeed = 0.0f;

//显示时间区域的文本
Text text_timeSpeed;

// Use this for initialization
void Start ()
{
    text_timeSpeed = GetComponent();
}

// Update is called once per frame
void Update ()
 {
    timeSpeed += Time.deltaTime;
    hour = (int)timeSpeed / 3600;
    minute = ((int)timeSpeed - hour * 3600) / 60;
    second = (int)timeSpeed - hour * 3600 - minute * 60;
    //text_timeSpeed.text = string.Format("{0:D2}:{1:D2}:{2:D2}", hour, minute, second);
    millisecond = (int)((timeSpeed - (int)timeSpeed) * 1000);
    text_timeSpeed.text = string.Format("{0:D2}:{1:D2}:{2:D2}.{3:D3}", hour, minute, second, millisecond);
 }

}

你可能感兴趣的:(Unity中获取游戏时间并显示成秒表格式显示在界面上)