Hi peeps.
again. i wasted few hours to understand how to access UI object in Unity scene, in code
Ok today i'm going to share with you guys,
step by step okay!
Our objectives here is to Have a text - display from "Hello" to "Hello World" dynamically using UI Text and C# Scripting
1. Create new scene, name it DynamicTextScene
2. Create new UI Object : Canvas, go to Hierarchy Window, right click select UI>Canvas.
3. In Canvas inspector, select its render mode to be World Space.
4. Now create a Text UI, Under the Canvas object, name it as dynamicText.
5. Now add new Button UI, under the Canvas and name it as PressButton.
6. Select the button text, change the button display text to "Press to Change Text"
7. Now select the Canvas, navigate to its Inspector, add new component script, name it as DynamicText.
8. Inside the DynamicText.cs script, code as follow.
a. ensure that UnityEngine.UI package is imported
b. ensure to create 2 public variables for the text and button.
9. After the two public variables is created, we can see from the Canvas inspector, two new script variables populated.
a. bind the Dynamic Text variable to dynamicText, by dragging the Text UI object to the canvas inspector variable script.
10. Assign the "Hello" string to dynamicText.text in the Start() method.
11. Create a new method called updateText(), that assign dynamicText.text to "Hello World" string.
12. Save the script changes.
14. Okay by doing all these steps.. what we get when we run the scene, we can see the text displayed at beginning is Hello, later after we press the press button, the text will change to Hello World, this is how we manipulate the Text UI using scripting.
Hope this helps you guys too.
Comments
Post a Comment