您的位置:首页 > 移动开发 > Unity3D

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

2017-08-29 09:20 465 查看
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Timer : 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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: