游戏开始就开始记录时间:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TimeCost : MonoBehaviour {
private Text TimeTip;
private float TimeCos;
private float StartTime;
private int TimeNum;
private int m_Hour;//时
private int m_Minute;//分
private int m_Second;//秒
private void Start()
{
TimeTip = transform.GetComponent<Text>();
StartTime = Time.time;
}
// Update is called once per frame
void Update ()
{
TimeCos = Time.time- StartTime;
TimeNum = Mathf.FloorToInt(TimeCos);
m_Hour =TimeNum / 3600;
m_Minute = (TimeNum - m_Hour * 3600)/60;
m_Second = TimeNum - m_Hour * 3600 - m_Minute * 60;
TimeTip.text = "操作用时:" + m_Hour + "时" + m_Minute + "分" + m_Second+"秒";
}
private void OnEnable()
{
StartTime = Time.time;
}
}