using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class clock : MonoBehaviour { public Text txtDATE; public Text txtTIME; public Sprite[] spSec; public Image imgSec; public Sprite[] spMin; public Image imgMin; public Sprite[] spHour; public Image imgHour; public GameObject goALH; private Animator aniALH; public GameObject goALM; private Animator aniALM; public Sprite[] spALPT; public Image imgALPT; AudioSource audioData; public AudioClip AlarmRing; bool bSound=false; bool bOnTime=false; // Use this for initialization void Start () { //txtDATE.text = "日期測試"; aniALH = goALH.GetComponent(); aniALM = goALM.GetComponent(); audioData = this.gameObject.GetComponent(); audioData.clip = AlarmRing; bSound = false; bOnTime = false; } // Update is called once per frame void Update () { //txtDATE.text = System.DateTime.Now.ToString("yyyy/MM/dd"); //txtTIME.text = System.DateTime.Now.ToString("HH:mm:ss"); int iHour,iH,iMinute,iSecond; int iALH, iALM; iALH = (int)aniALH.GetFloat ("h"); iALM = (int)aniALM.GetFloat ("m"); iSecond = System.DateTime.Now.Second; //Debug.Log (iSecond.ToString ()); imgSec.sprite = spSec[iSecond]; iMinute = System.DateTime.Now.Minute; imgMin.sprite = spMin[iMinute]; iHour = System.DateTime.Now.Hour; iH = (iHour * 5) % 60 + (int)((float)iMinute/60.0f*5.0f); imgHour.sprite = spHour[iH]; imgALPT.sprite = spALPT [(iALH % 12) * 6 + (int)(iALM/10)]; txtTIME.text = iHour.ToString() + ":" + iMinute.ToString(); int iTime1, iTime2; iTime1 = (iHour % 12) *60 + iMinute; iTime2 = (iALH % 12) * 60 + iALM; txtDATE.text = iTime1.ToString() + "/" + iTime2.ToString(); if ((bOnTime == true) && (bSound==false)) { audioData.Play(); bSound = true; } if ((bSound == true) && (bOnTime == false)) { audioData.Stop (); bSound = false; } if (iTime1 == iTime2) { bOnTime = true; } else { bOnTime = false; } } }