How to assign string contents to a text field in Unity 4.6?

I am working on a C # class that imports text data from a website. This is working fine. Where I lose this is how to display text in Unity 4.6, when I have all the text in a string variable. Any advice is appreciated.

+5
source share
3 answers

The Unity 4.6 UI system has a component called Text . You can watch the video tutorial here . I also suggest that you check its API .

As always, you have two options for adding this component to the game object. You can do this from the editor (just click on the game object that you want to have this component in the hierarchy and add the Text component). Or you can do it with a script using gameObject.AddComponent<Text>() .

If you are not familiar with the components, I suggest you read the article.

Anyway, in your script you need to add using UnityEngine.UI; to the very top, because the Text class is in the UnityEngine.UI namespace. So now back to the script that sets the value to Text .

First you need a variable that refers to the Text component. This can be done by setting it to the editor:

 public class MyClass : MonoBehaviour { public Text myText; public void SetText(string text) { myText.text = text; } } 

And the binding of the gameObject with the text component to this value in the editor.

Another option:

 public class MyClass : MonoBehaviour { public void SetText(string text) { // you can try to get this component var myText = gameObject.GetComponent<Text>(); // but it can be null, so you might want to add it if (myText == null) { myText = gameObject.AddComponent<Text>(); } myText.text = text; } } 

The previous script is not a good example, because GetComponent is actually expensive. Therefore, you may need to cache its link:

 public class MyClass : MonoBehaviour { Text myText; public void SetText(string text) { if (myText == null) { // looks like we need to get it or add myText = gameObject.GetComponent<Text>(); // and again it can be null if (myText == null) { myText = gameObject.AddComponent<Text>(); } } // now we can set the value myText.text = text; } } 

BTW, the "GetComponent" or "Add" parameter, if it does not already exist, is so common that usually in Unity you want to define a function

 static public class MethodExtensionForMonoBehaviourTransform { static public T GetOrAddComponent<T> (this Component child) where T: Component { T result = child.GetComponent<T>(); if (result == null) { result = child.gameObject.AddComponent<T>(); } return result; } } 

So you can use it like:

 public class MyClass : MonoBehaviour { Text myText; public void SetText(string text) { if (myText == null) { // looks like we need to get it or add myText = gameObject.GetOrAddComponent<Text>(); } // now we can set the value myText.text = text; } } 
+4
source

make sure you import the ui library - using UnityEngine.UI

gameObject.GetComponent<Text>().text .text - replace .text with any other field for interface text

+2
source

My guess is that the problem is creating the dynamic size of the β€œtext field”, and not just assigning a string to a GUIText GameObject. (If not, just put the GUIText GameObject in your scene, access it through the GUIText variable in the script and use myGUIText.text = myString at the beginning or update.)

If I am true in my assumption, then I think you should just use the GUI shortcut:

http://docs.unity3d.com/ScriptReference/GUI.Label.html

If you need to split the line before placing the text on different labels or GUITexts, you will need to use substrings

0
source

Source: https://habr.com/ru/post/1205644/


All Articles