Unity3D 编写游戏运行时间 格式为00:00:00

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

public class GameTime : MonoBehaviour {

	// Use this for initialization
    
    private Text text;

    private float timer = 0f;

    private int h = 0;

    private int m = 0;

    private int s = 0;

    private string timeStr = string.Empty;
    void Start()
    {
        text = GetComponent();
    }
	
  

    // Update is called once per frame
    void Update()
    {
        timer += Time.deltaTime;
        if (timer >= 1f)
        {
            s++;
            timer = 0;
        }
        if (s >= 60)
        {
            m++;
            s = 0;
        }
        if (m >= 60)
        {
            h++;
            m = 0;
        }
        if (h >= 99)
        {
            h = 0;
        }
        
        timeStr = string.Format("{0:D2}:{1:D2}:{2:D2}", h, m, s);
        text.text = "" + timeStr;
    }
}


 

你可能感兴趣的:(Unity3D 编写游戏运行时间 格式为00:00:00)