Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Auto-typing text in Unity 4.6?

Discussion in 'UGUI & TextMesh Pro' started by Sykoo, Nov 30, 2014.

  1. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    I want the game to autotype a text in game. If you can do this for me, I'd super-apprecaite it :) I tried TypeWriter asset but it's only for GUIText and I am using Unity 4.6 :(

    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 :)
     
  2. Senshi

    Senshi

    Joined:
    Oct 3, 2010
    Posts:
    556
    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:

    Code (csharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class AutoText : MonoBehaviour{
    6.     public static void TypeText(Text textElement, string text, float time){
    7.         float characterDelay = time / text.Length;
    8.         textElement.StartCoroutine(SetText(textElement, text, characterDelay));
    9.     }
    10.  
    11.     static IEnumerator SetText(Text textElement, string text, float characterDelay){
    12.         for(int i=0; i<text.Length; i++){
    13.             textElement.text += text[i];
    14.             yield return new WaitForSeconds(characterDelay);
    15.         }
    16.     }
    17. }
    Usage:
    Code (csharp):
    1. AutoText.TypeText(myUIText, "This text is auto-typing! =D", 2.5f);
    Let me know how it works, cheers!
     
    Last edited: Nov 30, 2014
    Sykoo likes this.
  3. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    Errors all over the place, please try and fix it :)
     
  4. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    I fixed the code but it aint working :(

    The new code:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class AutoTypingText : MonoBehaviour {
    6.  
    7.     public static float chracterDelay = time / text.Length;
    8.     public static Text textElement;
    9.     public static string text;
    10.     public static float time;
    11.  
    12.     void Update()
    13.     {
    14.         TypeText();
    15.     }
    16.  
    17.     public static void TypeText()
    18.     {
    19.         textElement.StartCoroutine(SetText(textElement, text, chracterDelay));
    20.     }
    21.  
    22.     static IEnumerator SetText(Text textElement, string text, float characterDelay)
    23.     {
    24.         for(int i=0; i<text.Length; i++)
    25.         {
    26.             textElement.text += text[i];
    27.             yield return new WaitForSeconds(characterDelay);
    28.         }
    29.     }
    30. }
    31.  
     
  5. Senshi

    Senshi

    Joined:
    Oct 3, 2010
    Posts:
    556
    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.
     
    Sykoo likes this.
  6. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    Thank you for the edit and the help, I will try now if it works :)
     
  7. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    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?
     
  8. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    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.
     
  9. Senshi

    Senshi

    Joined:
    Oct 3, 2010
    Posts:
    556
    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:

    Code (csharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class AutoplayText : MonoBehaviour{
    5.     public string text = "";
    6.     public float typeDuration = 2f;
    7.  
    8.     void Start(){
    9.         AutoText.TypeText(GetComponent<Text>(), text, typeDuration);
    10.     }
    11. }
    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?
     
    Last edited: Nov 30, 2014
  10. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    Thank you so super-much for your help! :) I will give you credits in my project :D
     
    Senshi likes this.
  11. Senshi

    Senshi

    Joined:
    Oct 3, 2010
    Posts:
    556
    Thanks and no problem; glad I could help! Just took a peek and EOS is looking pretty sweet already! =)
     
    Sykoo likes this.
  12. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    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 :) But thank you very much for your kind comment :D
     
    Senshi likes this.