Auto-typing text in Unity 4.6?

I want the game to autotype a text in game. If you can do this for me, I’d super-apprecaite it :slight_smile: I tried TypeWriter asset but it’s only for GUIText and I am using Unity 4.6 :frowning:

Your name will be featured in the credits section of my game as well for helping out in the development ^^
I just want the game to literally type out as if the player is typing it out whatever the text I put in. Please help me with this :slight_smile:

This should probably be in the scripting forum, but what the heck. Untested (typed in here, so likely a syntax error somewhere…) and limited to one character per frame:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class AutoText : MonoBehaviour{
    public static void TypeText(Text textElement, string text, float time){
        float characterDelay = time / text.Length;
        textElement.StartCoroutine(SetText(textElement, text, characterDelay));
    }

    static IEnumerator SetText(Text textElement, string text, float characterDelay){
        for(int i=0; i<text.Length; i++){
            textElement.text += text[i];
            yield return new WaitForSeconds(characterDelay);
        }
    }
}

Usage:

AutoText.TypeText(myUIText, "This text is auto-typing! =D", 2.5f);

Let me know how it works, cheers!

1 Like

Errors all over the place, please try and fix it :slight_smile:

I fixed the code but it aint working :frowning:

The new code:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class AutoTypingText : MonoBehaviour {

    public static float chracterDelay = time / text.Length;
    public static Text textElement;
    public static string text;
    public static float time;

    void Update()
    {
        TypeText();
    }

    public static void TypeText()
    {
        textElement.StartCoroutine(SetText(textElement, text, chracterDelay));
    }

    static IEnumerator SetText(Text textElement, string text, float characterDelay)
    {
        for(int i=0; i<text.Length; i++)
        {
            textElement.text += text[i];
            yield return new WaitForSeconds(characterDelay);
        }
    }
}

There was a small syntax error on line 11 (rogue );), but just tested and it works here. I edited my post to remove the syntax error, can you try again?

Either way, there’s no need to expose textElement and the like, and definitely no need for an Update() call.

1 Like

Thank you for the edit and the help, I will try now if it works :slight_smile:

CAn you make it so I can run a text object I already created on new UI? I mean I have the script attached to that text object to make it “auto-write”. How do I call it to auto-write otherwise?

Alright it worked, but I want my Text Object from the new UI that I already have in the scene to autowrite as if it was an animation for that specific object, instead of creating a new text in the script.

Sorry, I’m not 100% sure I understand correctly. =)

First, you don’t need to actually attach the AutoText script to anything. TypeText() is a static function, which means you can call it just like the Mathf functions (as long as the file is in your project).

So if you want to have the text “autoplay”, you can create a separate script for that that could look like this:

using UnityEngine;
using UnityEngine.UI;

public class AutoplayText : MonoBehaviour{
    public string text = "";
    public float typeDuration = 2f;

    void Start(){
        AutoText.TypeText(GetComponent<Text>(), text, typeDuration);
    }
}

You can then add that as a component to any UI Text element you want, and set the values as you want from the Inspector.

Of course you could also add this to the AutoText script itself if you prefer; I just like the separation in this case. =)

Sorry for not explaining it well before; I typed it all in a bit of a hurry. Is this about what you had in mind?

Thank you so super-much for your help! :slight_smile: I will give you credits in my project :smile:

1 Like

Thanks and no problem; glad I could help! Just took a peek and EOS is looking pretty sweet already! =)

1 Like

Oh thank you very much! I take EOS as a side-project right now as I am working with something smaller as a main project :slight_smile: But thank you very much for your kind comment :smile:

1 Like