Conteúdo do Curso
Unity for Beginners
Unity for Beginners
Unity's UI System
User Interface (UI) in Unity refers to the graphical elements that players interact with in a game or application. UI plays a critical role in guiding users through the experience, providing feedback, and enabling various actions.
Unity UI Essentials
Unity provides a dedicated UI system equipped with tools and components tailored for UI creation. Key components include Canvas, which acts as a container for UI elements, and pre-built UI components like buttons, text fields, and images. These elements can be easily arranged and styled using Unity's editor interface.
Creating UI in Unity
To create a UI in Unity, navigate to the Hierarchy, then press the left mouse button. After that, select UI.
We will have a list of all the UI elements that we can create, from text to image to buttons. We will discuss more about those elements and how to manipulate them in the next lessons. But now let’s take a small example of how to manipulate a text with script.
Manipulating text with script
Let’s select textMeshPro from that list.
A new text will be created for us in the game.
Note
If Unity asks you to implement TMP essentials please accept.
If we click on the text, all of its components will be shown in the inspector, and we can manipulate them manually.
Let’s add the test script to the object and write this code.
using TMPro
This line is an import statement that allows the script to use classes and functions from the TextMeshPro(TMP)
namespace. TMP is a Unity package for advanced text rendering.
[SerializeField] TMP_Text myText
This line declares a serialized field named "myText" of type TMP_Text
. Serialized fields are accessible in the Unity Editor, meaning you can assign values to them in the Unity Inspector.
myText.text = "test"
This line sets the text property of the "myText" TMP_Text
component to the string "test".
Now if we start the game the text will be modified to “test".
Obrigado pelo seu feedback!