Search Unity

Typewriter Effect, Display Next String Problem

Discussion in 'Scripting' started by GeneralPhysc, May 29, 2017.

?

Is this a Simple fix?

  1. Yes

    50.0%
  2. Maybe

    50.0%
  3. No

    0 vote(s)
    0.0%
  1. GeneralPhysc

    GeneralPhysc

    Joined:
    May 29, 2017
    Posts:
    14
    When the first string index is done, I want it to go to pause for a second, then display the next string in the [] array. Please help? Here is my Script:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class QuoteScroll : MonoBehaviour
    6. {
    7.  
    8.     public float delay = 0.1f;
    9.     public string[] strings;
    10.     private Text currentText;
    11.  
    12.     void Start()
    13.     {
    14.         currentText = GetComponent<Text>();
    15.         currentText.text = "";
    16.         StartCoroutine(ShowText(strings[0]));
    17.     }
    18.  
    19.     void Update()
    20.     {
    21.        
    22.     }
    23.  
    24.     private IEnumerator ShowText(string textToDisplay)
    25.     {
    26.         int stringLength = textToDisplay.Length;
    27.         int currentIndex = 0;
    28.  
    29.         currentText.text = "";
    30.        
    31.         while (currentIndex < stringLength)
    32.         {
    33.             currentText.text += textToDisplay[currentIndex];
    34.             currentIndex++;
    35.  
    36.             if (currentIndex < stringLength)
    37.             {
    38.                 yield return new WaitForSeconds(delay);
    39.             }
    40.             else
    41.             {
    42.                 break;
    43.             }
    44.         }
    45.        
    46.     }
    47.  
    48. }
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You want the whole string to take (and wait ) 1 second, or each character or both? At the moment, it looks like you're going by the character, but you could send in the whole array to the Coroutine, instead and then wait on the character and/or word for 1 second.. the code would be very similar.
     
  3. GeneralPhysc

    GeneralPhysc

    Joined:
    May 29, 2017
    Posts:
    14
    I just want the first array [0] to display then it waits a full second, and then sets the text to be empty, and then the next array [2] is displayed, and it just keeps scrolling through arrays. 1 second between each other.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Ya, okay. Well pass in the array and just modify the IEnumerator to be slightly different so that it takes the array index instad of the character index.
     
  5. GeneralPhysc

    GeneralPhysc

    Joined:
    May 29, 2017
    Posts:
    14
    Okay, simple, um, I'm still kinda new to coding, can you show me where in the code
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    change the parameter of IENumertor to string []
    and then pass in the array instead of array[0] when you call the coroutine.
     
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Code (csharp):
    1. private IEnumerator ShowText(string [] textToDisplay)
    and this:
    Code (csharp):
    1.  StartCoroutine(ShowText(strings));
     
  8. GeneralPhysc

    GeneralPhysc

    Joined:
    May 29, 2017
    Posts:
    14
    They are all displayed at the same time now...
     
  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Change the delay to 1 instead of 0.1, I think?
     
  10. GeneralPhysc

    GeneralPhysc

    Joined:
    May 29, 2017
    Posts:
    14
    Just displays them all slower.
     
  11. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Wait, but isn't that what you wanted?
     
  12. GeneralPhysc

    GeneralPhysc

    Joined:
    May 29, 2017
    Posts:
    14
    I have trouble explain things, I just want it to display the first array, wait 1 second then set the text component to be blank, then display the second array, and just cycle through each other, using the "Typewriter Effect"
     
  13. GeneralPhysc

    GeneralPhysc

    Joined:
    May 29, 2017
    Posts:
    14
    Like a dialogue box
     
  14. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Sorry, I have not forgotten about your post, but I am maybe just confused about what you wanted, because I thought the suggestion and its result is what you were looking to do. :(
     
  15. GeneralPhysc

    GeneralPhysc

    Joined:
    May 29, 2017
    Posts:
    14
    You are fine, I would just like it to cycle through the arrays one at a time.
     
  16. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Code (csharp):
    1.  
    2. IEnumerator ShowText(string [] Alltext) {
    3. WaitForSeconds wfs = new WaitForSeconds(1);
    4.    for(int i = 0; i < Alltext.Length; ++i) {
    5.       currentText.text = Alltext[i];
    6.       yield return wfs;
    7.     }
    8. }
    9.  
     
  17. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    I think perhaps. (and I might be misunderstanding) the question is to display several strings one word at a time.

    So, for example, the array may contain (passed into the ienumerator)

    "This is a car"
    "It is for sale"
    "The price is a good deal"

    Then, the IEnumerator should loop through and display with a pause between words
    This
    This is
    This is a
    This is a car

    Then it should pause, clear and start the next string. If this is the case, each string in the array needs to be split up to create a second array that is looped through. But, @GeneralPhysc needs to confirm if this is what they are trying to do.
     
  18. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You may be /are probably right. I think I got hung up on the character vs. word vs phrase.
    Just in case that's the case, may as well write this up in advance :)
    Code (CSharp):
    1.  
    2. IEnumerator ShowText(string [] Alltext) {
    3. WaitForSeconds wfs = new WaitForSeconds(1);
    4.    for(int i = 0; i < Alltext.Length; ++i) {
    5.       currentText = "";
    6.       string [] words = Alltext[i].Split();
    7.       for(int j = 0; j < words.Length; ++j) {
    8.          currentText += words[j] + " ";
    9.          yield return wfs;
    10.       }
    11.    }
    12. }
    13.  
     
    Last edited: May 30, 2017
  19. GeneralPhysc

    GeneralPhysc

    Joined:
    May 29, 2017
    Posts:
    14
    Maybe 1 character at a time? Like Dialogue in a game's story.
     
  20. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    lol. Well, then you need to just modify it a little bit... again :)
    Can you see how to modify it by now for that?
     
  21. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    I will help out just a little here. A string is nothing more than an array of chars. So it's easy enough to loop through a string and get a char at a certain index and add it to your text object.
     
  22. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Code (csharp):
    1.  
    2. string [] strArray = // some string array
    3. string word = strArray[index]; // one word
    4. char c = word[anotherIndex]; // character.
    5.  
    6. // also
    7. strArray[index][anotherIndex] // character.
    8.  
    :)
     
  23. GeneralPhysc

    GeneralPhysc

    Joined:
    May 29, 2017
    Posts:
    14
    Can you put it together so it's easier for me to look at, like with my script
     
  24. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Sigh, I mean this politely.. Do you mean "Put it together so it's easier to just copy "?
    Did you try to incorporate the several posts, including the last & try to make it ?

    If you try, maybe you'll get it.. or maybe you'll get really close and you can post your update here and get some feedback..:)
     
  25. GeneralPhysc

    GeneralPhysc

    Joined:
    May 29, 2017
    Posts:
    14
    I wasn't planning on copying it, I just can understand it better if it's together, I don't use other's code, I type my own so I know that I understand it.

    I will see what I can do, then post the script right here and see what you two think.
     
  26. GeneralPhysc

    GeneralPhysc

    Joined:
    May 29, 2017
    Posts:
    14
    I don't see how I make it do one character at a time
     
  27. GeneralPhysc

    GeneralPhysc

    Joined:
    May 29, 2017
    Posts:
    14
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class QuoteScroll : MonoBehaviour
    6. {
    7.     private Text currentText;
    8.  
    9.     void Start()
    10.     {
    11.         currentText = GetComponent<Text>();
    12.         currentText.text = "";
    13.         StartCoroutine(ShowText(strings));
    14.     }
    15.  
    16.     void Update()
    17.     {
    18.        
    19.     }
    20.  
    21.     private IEnumerator ShowText(string[] textToDisplay)
    22.     {
    23.         WaitForSeconds delay = new WaitForSeconds(.1f);
    24.         for (int i = 0; i < Alltext.Length; ++i)
    25.         {
    26.             currentText.text = "";
    27.             string[] strings = textToDisplay[i].Split();
    28.             for (int j = 0; j < words.Length; ++j)
    29.             {
    30.                 currentText.text += words[j] + " ";
    31.                 yield return delay;
    32.             }
    33.         }
    34.     }
    35.  
    36. }
     
  28. Scabbage

    Scabbage

    Joined:
    Dec 11, 2014
    Posts:
    268
    That looks fine to me, except for the variable names, and the Split() method should have a regex (just characterin this case) in it. I'm assuming Alltext should be textToDisplay, and strings should be words. So your ShowText function would be:

    Code (csharp):
    1.  
    2.     private IEnumerator ShowText(string[] textToDisplay)
    3.     {
    4.         WaitForSeconds delay = new WaitForSeconds(.1f);
    5.         for (int i = 0; i < textToDisplay.Length; i++)
    6.         {
    7.             currentText.text = "";
    8.             string[] words = textToDisplay[i].Split(' ');
    9.             for (int j = 0; j < words.Length; j++)
    10.             {
    11.                 currentText.text += words[j] + " ";
    12.                 yield return delay;
    13.             }
    14.         }
    15.     }
     
  29. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    @Scabbage : I'm pretty sure the default for String.Split is whitespace. :)

    However, the OP's last post only repeated a post I made earlier, but doesn't have any change to add the "character at a time" to it. You'll need to loop through "words"' and add a character there, and insert a second long pause if that's still desired, in the appropriate place.
     
  30. Scabbage

    Scabbage

    Joined:
    Dec 11, 2014
    Posts:
    268
    @methos5k Hmmm.. actually why have the split at all?
    Code (csharp):
    1.  
    2.     private IEnumerator ShowText(string[] textToDisplay)
    3.     {
    4.         WaitForSeconds delay = new WaitForSeconds(.1f);
    5.         for (int i = 0; i < textToDisplay.Length; i++)
    6.         {
    7.             currentText.text = "";
    8.             for (int j = 0; j < textToDisplay[i].Length; j++)
    9.             {
    10.                 currentText.text += textToDisplay[i][j];
    11.                 yield return delay;
    12.             }
    13.         }
    14.     }
     
  31. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, possibly a couple of reasons.. 1 was a small misunderstanding of what was sought in the beginning. Secondly, I thought there was talk of wanting a longer pause between strings (of the array). Anyhow, the OP can choose what's suiting :)