C# script to make digital clock showing current time.

 

Content not available.
Please allow cookies by clicking Accept on the banner

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

// ? 2017 TheFlyingKeyboard and released under MIT License
// theflyingkeyboard.net

public class Clock : MonoBehaviour {
    public Text clockText;

    public bool showSeconds;

    private int seconds;
    private int minutes;
    private DateTime time;

    // Use this for initialization
    void Start () {
        seconds = -1;
        minutes = -1;
    }
  
  // Update is called once per frame
  void FixedUpdate () {
        time = DateTime.Now;

        if (showSeconds)
        {
            if (seconds != time.Second)
            {
                UpdateText();
                seconds = time.Second;
            }
        }
        else
        {
            if (minutes != time.Minute)
            {
                UpdateText();
                minutes = time.Minute;
            }
        }     
    }

    void UpdateText()
    {
        clockText.text = time.Hour.ToString("D2") + ":" + time.Minute.ToString("D2");

        if (showSeconds)
        {
            clockText.text += ":" + time.Second.ToString("D2");
        }
    }
}

 



Unity C# UI Digital Clock
Tagged on:         

Leave a Reply

Your email address will not be published. Required fields are marked *

By continuing to use the site, you agree to the use of cookies. You can read more about it the Cookies&Privacy Policy Section Above. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this. You can read more about it the Cookies&Privacy Policy Section.

Close