Search Unity

Wiki typewriter script and GUI 2.0

Discussion in 'Immediate Mode GUI (IMGUI)' started by yellowlabrador, Jan 11, 2008.

  1. yellowlabrador

    yellowlabrador

    Joined:
    Oct 20, 2005
    Posts:
    562
    Hello All,

    I have this script using the wiki's typewriter script.
    I was trying to use it so I can output a text on a GUI.Box or Label.
    Since the wiki script requires a GUIText component I used a GUIText component instead of an empty one and slap this code to it.


    Code (csharp):
    1.  
    2. /* Test GUI Scripts */
    3.  
    4. var word = "this is a test";
    5. var myFont : GUISkin;
    6. var letterPause = 0.2;
    7. var sound : AudioClip;
    8.  
    9. var test = true;
    10. function OnGUI ()
    11. {
    12.    
    13.     GUI.BeginGroup(Rect(Screen.width/2-256,Screen.height/2-256,512,512));
    14.    
    15.     GUI.Box(Rect(0,0,512,512),"");
    16.    
    17.     GUI.skin = myFont;
    18.     GUI.Label(Rect(10,2,200,18),GUIContent(guiText.text));
    19.     GUI.EndGroup();
    20.        
    21.    
    22.    
    23. }
    24.  
    25. function Update()
    26. {
    27.     if(test == true)
    28.     {
    29.  
    30.     TypeText();
    31.     test = false;
    32.     }
    33. }
    34.  
    35.  
    36. function TypeText()
    37. {
    38.     //yield WaitForSeconds(4);
    39.     for (var i = 0; i < word.Length; i++)
    40.         {
    41.             guiText.text += word[i];
    42.             if (sound)
    43.             audio.PlayOneShot (sound);
    44.             yield WaitForSeconds (letterPause);
    45.         }
    46.     //test = false;
    47. }
    48.  
    49.  
    50.  
    The problem is if the GUIText component transform is at 0.5,0.5,0.0...
    it is also typing the string on that location(because I think it is attached to a GUIText component).

    See my attached image.

    I fixed this by putting the transform into some location far far away.

    Is there a better way of doing this other than my hocus focus approach?

    Thanks,
    Ray

    Edit: I meant Hello :oops:
     

    Attached Files:

  2. lgoss007

    lgoss007

    Joined:
    Dec 6, 2006
    Posts:
    88
    You may have figured this out already, but you should just use an empty Game Object if you're going to use a Box or Label from the GUI. The wiki script probably used the GUIText because there was no GUI before Unity 2.0.
     
  3. yellowlabrador

    yellowlabrador

    Joined:
    Oct 20, 2005
    Posts:
    562
    lgoss007

    Yes, I was using an empty GO at first, but using it I get an error that guiText.text requires a GUITexture. What I was trying to figure out is "How can I do this typewritter effect" on a GUI 2.0

    Thanks,
    Ray
     
  4. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    Instead of assigning guiText.text with the character string, just pass it to a variable that you pass into a GUI.Label. Since OnGUI is updated immediately, it will always reflect the changes in your variable that is updated in the TypeText method.

    Cheers
    Shaun

    EDIT: If you've created a GUIText GO, I suggest dumping it, creating an empty GO and dropping this script on it, then updating the code.
     
  5. lgoss007

    lgoss007

    Joined:
    Dec 6, 2006
    Posts:
    88
    Ah, I missed that guiText.text part. You can just fix that by doing what Shaun says. Code would be (comments included on changes):

    Code (csharp):
    1. /* Test GUI Scripts */
    2.  
    3. var word = "this is a test";
    4. var myFont : GUISkin;
    5. var letterPause = 0.2;
    6. var sound : AudioClip;
    7. private var wordLabel : String; // Add this as the display text for our typewriter
    8.  
    9. var test = true;
    10. function OnGUI ()
    11. {
    12.    GUI.BeginGroup(Rect(Screen.width/2-256,Screen.height/2-256,512,512));
    13.    
    14.    GUI.Box(Rect(0,0,512,512),"");
    15.    
    16.    GUI.skin = myFont;
    17.   // Change the content to our new wordLabel
    18.    GUI.Label(Rect(10,2,200,18),GUIContent(wordLabel));
    19.    GUI.EndGroup();
    20. }
    21.  
    22. function Update()
    23. {
    24.    if(test == true)
    25.    {
    26.      TypeText();
    27.      test = false;
    28.    }
    29. }
    30.  
    31. function TypeText()
    32. {
    33.    //yield WaitForSeconds(4);
    34.    for (var i = 0; i < word.Length; i++)
    35.       {
    36.         // set the word label text one at a time
    37.          wordLabel += word[i];
    38.          if (sound)
    39.          audio.PlayOneShot (sound);
    40.          yield WaitForSeconds (letterPause);
    41.       }
    42.    //test = false;    
    43. }
     
  6. yellowlabrador

    yellowlabrador

    Joined:
    Oct 20, 2005
    Posts:
    562
    lgoss007 shaun

    Thank you, Works perfect

    Ray