Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

text

unity3d

I am working on a C# class that imports text data from a web site. That works OK. Where I'm losing it is how to display the text in Unity 4.6 once I have all the text in a string variable. Any advice is appreciated.

like image 726
jadkins4 Avatar asked Oct 27 '14 21:10

jadkins4


People also ask

How do I assign text in Unity?

1) Create a GameObject with a Text component; 2) Create a GameObject with a Button component; 3) Create a GameObject with a component of your custom script; 4) Create the reference in your custom script on the Text component you want to update; 5) Create a public method in your custom script that will be invoked when ...

How do I add input to text in Unity?

Details. The Input Field script can be added to any existing Text control object from the menu (Component > UI > Input Field). Having done this, you should also drag the object to the Input Field's Text property to enable editing.

How do I make a text box in Unity?

You can use the Unity UI system and EventSystems coupled with some custom scripting to create this logic. GameObject > UI > Text - this will create a new Text element (along with a UI Canvas and EventSystem) and you can edit the displayed text (and change it dynamically with its text property).


2 Answers

Unity 4.6 UI system has component named Text. You can watch video tutorial here. Also I suggest you to check out its API.

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

In case you are not familiar with components yet, I suggest you to read this article.

Anyway, in your script you'll need to add using UnityEngine.UI; at the very top of it, because the Text class is in UnityEngine.UI namespace. Ok, so now back to script that will set the value of Text component.

First you need variable that refers to Text component. It can be done via exposing it to editor:

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

And attaching gameObject with text component to this value in 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;
    }
}

Previous script is not a good example, because GetComponent is actually expensive. So you might want to cache it’s reference:

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 patter of ‘GetComponent or Add if it doesn’t exist yet’ is so common, that usually in Unity you want to define 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 as:

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;
    }
}
like image 160
d12frosted Avatar answered Oct 05 '22 22:10

d12frosted


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

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

like image 24
ina Avatar answered Oct 06 '22 00:10

ina