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

AutoType and Coroutine Performance

Discussion in 'Scripting' started by riot9, Nov 17, 2014.

  1. riot9

    riot9

    Joined:
    May 6, 2014
    Posts:
    35
    Hi!
    I want to have text type out letter by letter. I'm using the AutoType script found on the wiki, but just modified it to work with the new UI.
    Found here: http://wiki.unity3d.com/index.php/AutoType

    I'm using the new UI, so the only change I really made was use UnityEngine.UI, and have it access the text with gameObject.GetComponent<Text>().text;

    What I want to happen is have my text type out, but if they don't want to wait, they can tap the text and it speeds up really fast. Even when I put a delay of 0 in, the typing speed can never get fast enough to type out a paragraph in less than a few seconds. I'd need it to be less than a second.

    The problem is even worse when I deploy the game to my phone, even with a delay of 0 each paragraph take about 6 seconds.

    It makes sense to me that it's affected by CPU performance, but are coroutines inherently this slow, or is there something I can do to speed it up?

    Thanks!
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Coroutines aren't slow, but using a delay of 0 is a bad idea because that makes everything framerate-dependent, so the time taken depends on the framerate, since you're doing 1 character per frame. Use different logic (instead of 1 character per x time, do x characters per x time).

    --Eric
     
  3. riot9

    riot9

    Joined:
    May 6, 2014
    Posts:
    35
    Thanks for your quick reply Eric!

    I'm still pretty new to Unity, but I *think* I understand what you're saying. So setting it to 0 means that the fastest it can possibly go is how long it takes to go through a draw/update loop, so suddenly it is going to feel different on each device.

    So if I need something faster than what a given processor can handle, it just makes more sense to output more characters per update. And to ensure a similar experience on all platforms, use a time greater than 0, is that right?
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Exactly. Although the best way to print text quickly would probably be to figure out how many characters per frame should be output. e.g., if you were getting 60fps, say 10 characters per frame, and if you're getting 30fps, then it should be 20 characters per frame. However, that's more complex to program than, say, just outputting 100 characters every 1/6 second and probably wouldn't look that different.

    In general you don't want to go too small with WaitForSeconds values, since varying framerates can cause imprecision...Unity won't wait the exact amount of time, but rather it will wait for the closest time that corresponds to the nearest frame. In other words, the smallest amount of time it can wait is whatever the current framerate is.

    --Eric
     
  5. riot9

    riot9

    Joined:
    May 6, 2014
    Posts:
    35
    Thanks, re-worked my logic and it works better than I expected.

    I appreciate the help!
     
  6. AlphaGTR

    AlphaGTR

    Joined:
    Oct 26, 2013
    Posts:
    38
    Hi @riot9 can you show how you got this to work? I am trying to do this with the new UI Text object. I replaced

    Code (CSharp):
    1. using UnityEngine;
    with

    Code (CSharp):
    1. using UnityEngine.UI;
    and instances of

    Code (CSharp):
    1. guiText.text
    with

    Code (CSharp):
    1. gameObject.GetComponent<Text>().text
    But would like to know how you were able to have the player tap to make the text speed up.
     
    Last edited: Jan 18, 2015
  7. riot9

    riot9

    Joined:
    May 6, 2014
    Posts:
    35
    Hey AlphaGTR,

    Sorry for the late reply. I actually ran into problems speeding it up when tapping and decided to have it just fill the entire text out immediately when they tap. So to do that I made a method that would stop all the coroutines and then just set the text to the full source.

    I set up an invisible image to catch their tap that would call the method that stops the coroutine and fills the text in. Tapping that image also disables the image so it doesn't interfere with scrolling (mine's a mobile game).