1.关键代码
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Test : MonoBehaviour {
//毫秒
private float ms = 0f;
//小时
private int h = 0;
//分
private int m = 0;
//秒
private int s = 0;
//字符串
private string timeStr = string.Empty;
void Update () {
//累加游戏运行时间
timer += Time.deltaTime;
//毫秒是0-1的小数,超过1,进位清零,重新计算
if (timer >= 1f) { s++;timer = 0; }
if (s >= 60) { m++;s = 0; }
if (m >= 60) { h++;m = 0; }
if (h >= 99) { h = 0; }
}
private void OnGUI()
{
//将数字转字符串
timeStr = string.Format("{0}:{1}:{2}", h, m, s);
//GUI风格
GUIStyle style= new GUIStyle();
//设置字体大小
style.fontSize = 100;
//GUI标签
GUI.Label(new Rect(10,10,100,200),timeStr,style);
}
}