Advertisemen
In this episode of short unity tutorials, we are going to take a look at making digital clock in unity 3d.
It is very simple and short, with few lines of code, you will be able to make digital clock like this in Unity.
HH:MM:SS [ 03 : 08 : 08]
First of all, create one C# script, name it and drag it into main camera. You are not gonna have anything else then mainCamera in the scene.
Ok, Now open up the script you have just create and type the followings in highlights. Your complete script should look like this -
--------------------------------------------------------------------------------------------------------------------------
using UnityEngine;
using System.Collections;
using System;
public class digitalClock : MonoBehaviour {
void OnGUI(){
DateTime time = DateTime.Now;
String hour = time.Hour.ToString().PadLeft(2, '0');
String minute = time.Minute.ToString().PadLeft(2, '0');
string second = time.Second.ToString().PadLeft(2, '0');
GUILayout.Label(hour +":"+ minute +":"+ second);
}
}
--------------------------------------------------------------------------------------------------------------------------using System.Collections;
using System;
public class digitalClock : MonoBehaviour {
void OnGUI(){
DateTime time = DateTime.Now;
String hour = time.Hour.ToString().PadLeft(2, '0');
String minute = time.Minute.ToString().PadLeft(2, '0');
string second = time.Second.ToString().PadLeft(2, '0');
GUILayout.Label(hour +":"+ minute +":"+ second);
}
}
First thing, you have to import "System", otherwise functions won't work (-- DateTime, Hour, PadLeft etc.).
After that, Inside the OnGUI function, create the instance of variable DateTime to hold current time and add some Strings (hour, minute second) to hold respective values of current time.
In above example what PadLeft function does is, if hour has less then two digits like - 1 to 9, it adds 0 at the front [ 01 - 09] and so on.
Finally, use GUILayout.label to display current just created in the scene.
Now, fire up unity, you should see current time in the scene.
If this is helpful, please like and share with others.
Thanks.....
Advertisemen
Tidak ada komentar:
Posting Komentar