
上QQ阅读APP看书,第一时间看更新
How to do it...
To create a digital clock, follow these steps:
- Create a new Unity 2D project.
- Import the provided Fonts folder.
- In the Hierarchy panel, add a UI | Text game object to the scene named Text-clock.
- Ensure that the Text-clock GameObject is selected in the Hierarchy panel. Now, in Inspector, ensure that the following properties are set:
-
- Text set to read as time goes here (this placeholder text will be replaced by the time when the scene is running)
- Font type set to Xolonium Bold
- Font Size set to 20
- Alignment set to horizontal and vertical center
- Horizontal and Vertical Overflow settings set to Overflow
- Color set to white
- In the Rect Transform, click on the Anchor Presets square icon, which will result in the appearance of several rows and columns of preset position squares. Hold down Shift+Alt and click on the top and center column rows.
- Create a folder named _Scripts and create a C# script class called ClockDigital in this new folder:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
public class ClockDigital : MonoBehaviour {
private Text textClock;
void Awake (){
textClock = GetComponent<Text>();
}
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;
}
string LeadingZero (int n){
return n.ToString().PadLeft(2, '0');
}
}
Underscore prefix so items appear first in sequence
Since scripts and scenes are things that are most often accessed, prefixing their folder names with an underscore character, _as _Scenes and _Scripts, means they are always at the top in the Project panel.
Since scripts and scenes are things that are most often accessed, prefixing their folder names with an underscore character, _as _Scenes and _Scripts, means they are always at the top in the Project panel.
Although the preceding code is useful for illustrating how to access the time component of a DateTime object individually, the Format(...) method of the String class can be used to format a DateTime object all in a single statement, for example, the preceding could be written more succinctly in a single statement:
String.Format("HH:mm:ss", DateTime.Now)
For more examples, see http://www.csharp-examples.net/string-format-datetime/.
String.Format("HH:mm:ss", DateTime.Now)
For more examples, see http://www.csharp-examples.net/string-format-datetime/.
- Ensure the Text-clock GameObject is selected in the Hierarchy panel.
- In the Inspector panel, add an instance of the ClockDigital script class as a component by clicking the Add Component button, selecting Scripts, and choosing the Clock Digital script class:

Add script components through drag and drop
Script components can also be added to GameObjects via drag and drop. For example, with the Text-clock GameObject selected in the Hierarchy panel, drag your ClockDigital script onto it to add an instance of this script class as a component to the Text-clock GameObject.
Script components can also be added to GameObjects via drag and drop. For example, with the Text-clock GameObject selected in the Hierarchy panel, drag your ClockDigital script onto it to add an instance of this script class as a component to the Text-clock GameObject.
- When you run the scene, you will now see a digital clock that shows hours, minutes, and seconds at the top-center part of the screen.