一个只有分钟秒钟的计时器
效果如图
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class time : MonoBehaviour
{
// Start is called before the first frame update
private Text gradetext;
public static float second=0;
public static float minute=0;
void Start()
{
gradetext = GameObject.Find("gradetext").GetComponent<Text>();
//我自己的名叫gradetext的text控件
}
// Update is called once per frame
void Update()
{
}
private void FixedUpdate()
{
Timerbegin();
}
void Timerbegin()
{
second += Time.deltaTime;
if (second >= 60)
{
second -= 60;
minute++;
}//秒钟到60,分钟加一
gradetext.text=Timer(minute,second);
}
public static String Timer(float minute, float second)//兼具转换功能
{
String gradetext;
if (second >= 60)
{
second -= 60;
minute++;
}
if (second < 10)
gradetext = "0" + Math.Round(second, 2).ToString();
else
gradetext = Math.Round(second, 2).ToString();
if (minute < 10)
gradetext = "0" + minute + ":" + gradetext;
else
gradetext = minute.ToString() + ":" + gradetext;
return gradetext;
}
}