Unity3D中获取游戏时间并显示成秒表格式

这个显示游戏时间在游戏中比较常见 如下图:

 

直接附上脚本(直接把脚本挂在要显示的UI    即Text上就好)

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class GetTime : MonoBehaviour {

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

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

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

// Use this for initialization
void Start()
{
text_timeSpend = GetComponent<Text>();
}

// Update is called once per frame
void Update()
{
timeSpend += Time.deltaTime;
// GlobalSetting.timeSpent = timeSpend;

hour = (int)timeSpend / 3600;
minute = ((int)timeSpend - hour * 3600) / 60;
second = (int)timeSpend - hour * 3600 - minute * 60;
millisecond = (int)((timeSpend - (int)timeSpend) * 1000);

text_timeSpend.text = string.Format("{0:D2}:{1:D2}:{2:D2}.{3:D3}", hour, minute, second, millisecond);
}
}

posted @ 2017-12-08 16:01  WalkingSnail  阅读(4777)  评论(0编辑  收藏  举报