C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Unity UI TextThe UI text element displays a non-interactive piece of text to the user. Text elements can be used to provide captions or labels for other GUI controls or to display instructions or other text. For example printing the current score of the player to the screen requires the numeric value of the score to be converted to a string, generally through the .toString() method, before it is displayed. To insert a Text UI element in Unity, right-click on the Scene Hierarchy, then select GameObject -> UI -> Text. There are many properties of the Text element. In which Text Field is the most important property. You can type out what you want the text box to show in that field. You can change the font of the text; you must first import the font file from your system into Unity as an Asset. We can also accept the Text element through the scripting; this is where the importance of dynamic UI comes in. Let's see one simple example, instead of the console, which is printing how many times the button has been pressed, as in the previous chapter; let's print it out on the game screen trough the Text element. To do so, open your previous script file (ButtonAction.cs) and make some changes to it: using System.Collections; using System.Collections.Generic; using UnityEngine; public class ButtonAction : MonoBehaviour { int n; public Text myText; public void OnButtonPress(){ n++; myText.text = "Button clicked " + n + " times."; } } Here, we added a public Text variable, where we can drag and drop our Text UI element. Save your script and go to the ButtonGameObject, and you will now see the new slot for the Text UI element. Drag and drop your Text GameObject on to the slot. Now hit the play button.
Next Topic#
|