倒數計時 code
新建腳本名稱:count_down
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
public class count_down : MonoBehaviour
{
// Start is called before the first frame update
private Text textclock;
private float countdowntimerdelay;
private float countdowntimerstarttime;
public int input_sec;
void Start()
{
textclock = GetComponent<Text>();
//input_sec = 30;
CountdownTimerRestet(input_sec);
}
// Update is called once per frame
void Update()
{
string timermess = "countdown has \n finished";
int timeleft = (int)CountdownTimersecre();
if (timeleft > 0)
timermess = "Countdown seconds \n remaining = " + LeadingZero(timeleft);
textclock.text = timermess;
}
private void CountdownTimerRestet(float delaysec)
{
countdowntimerdelay = delaysec;
countdowntimerstarttime = Time.time;
}
private float CountdownTimersecre()
{
float elapsedsec = Time.time - countdowntimerstarttime;
float timeleft = countdowntimerdelay - elapsedsec;
return timeleft;
}
private string LeadingZero(int n)
{
return n.ToString().PadLeft(2, '0');
}
}
數位時鐘 code
新建腳本名稱:clock_digital
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
public class clock_digital : MonoBehaviour
{
// Start is called before the first frame update
private Text textclock;
void Start()
{
textclock = GetComponent<Text>();
}
// Update is called once per frame
void Update()
{
DateTime time = DateTime.Now;
string hour = LeadingZero(time.Hour);
string minute = LeadingZero(time.Minute);
string second = LeadingZero(time.Second);
textclock.text = hour + ":" + minute + ":" + second;
}
private string LeadingZero(int n)
{
return n.ToString().PadLeft(2, '0');
}
}
洛克人圖
Latest posts by SHXJ (see all)
- 受保護的內容: NAS 版 Mathbot 管理網站與 Linebot 啟動方法 - 2024 年 11 月 15 日
- Realtime 啥鬼的 - 2021 年 6 月 15 日
- nodejs 數學遊戲 - 2021 年 6 月 8 日
