Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Fading in/out GUI text with C#? {SOLVED}

Discussion in 'Scripting' started by milliehashh, Jan 20, 2016.

  1. milliehashh

    milliehashh

    Joined:
    Sep 17, 2015
    Posts:
    72
    I'm trying to make a simple 2D platforming game with a story thats told through text, similar to the way 'Thomas was alone' uses text fading in and out to tell a story - I'm gonna need a lot of text so I've been looking for a way to fade the text in with a few simple lines of code, otherwise ill have to animate a LOT of text fading in and out
    i don't really know where to start with the script, all i know is that i will have to adjust the alpha level of the text to actually fade it in/out - anyone have any suggestions of how i could do it in a time efficient way without lines and lines of script? thanks in advance :D
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using System.Collections;
    5.  
    6. public class TestScript : MonoBehaviour
    7. {
    8. // can ignore the update, it's just to make the coroutines get called for example
    9.     void Update()
    10.     {
    11.         if (Input.GetKeyDown(KeyCode.Q))
    12.         {
    13.             StartCoroutine(FadeTextToFullAlpha(1f, GetComponent<Text>()));
    14.         }
    15.         if (Input.GetKeyDown(KeyCode.E))
    16.         {
    17.             StartCoroutine(FadeTextToZeroAlpha(1f, GetComponent<Text>()));
    18.         }
    19.     }
    20.  
    21.  
    22.  
    23.     public IEnumerator FadeTextToFullAlpha(float t, Text i)
    24.     {
    25.         i.color = new Color(i.color.r, i.color.g, i.color.b, 0);
    26.         while (i.color.a < 1.0f)
    27.         {
    28.             i.color = new Color(i.color.r, i.color.g, i.color.b, i.color.a + (Time.deltaTime / t));
    29.             yield return null;
    30.         }
    31.     }
    32.  
    33.     public IEnumerator FadeTextToZeroAlpha(float t, Text i)
    34.     {
    35.         i.color = new Color(i.color.r, i.color.g, i.color.b, 1);
    36.         while (i.color.a > 0.0f)
    37.         {
    38.             i.color = new Color(i.color.r, i.color.g, i.color.b, i.color.a - (Time.deltaTime / t));
    39.             yield return null;
    40.         }
    41.     }
    42. }
    43.  
    attach to a Text UI gameobject
     
  3. milliehashh

    milliehashh

    Joined:
    Sep 17, 2015
    Posts:
    72
    Thank you, that does the job perfectly! :D
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    there are probably some tweaks to make it better, but you can change out the "Text" type for other things, originally I was using this to control a black Image overlay to cover a state transition between menus and gameplay etc.
     
  5. milliehashh

    milliehashh

    Joined:
    Sep 17, 2015
    Posts:
    72
    its working fine for me at the moment but if i do need to do some tweaking i will update the code and post it here, thank you :) ah yeah that makes sense, a nice easy way to make it look like a smooth transition - that might come in handy at some point too, thanks!
    One last thing, what type of script would i need to put in Void Start to make it so that the text starts up with the alpha level being 0? thank you again for all the help and awesome tips :D
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    first line of FadeTextToFullAlpha sets the alpha to 0, so if you want the text to fade in as soon as it is created in the scene just call that coroutine.
     
  7. DigitalReaper

    DigitalReaper

    Joined:
    Feb 25, 2014
    Posts:
    1
    HI, this is exactly what i'm looking for but it seems to feeze my application when i use it, i think its something to do with the while loop? any ideas?
     
  8. ImTheC

    ImTheC

    Joined:
    Sep 7, 2016
    Posts:
    1
    Can you post the code from your script so we can look it over?
     
  9. ManiaCO

    ManiaCO

    Joined:
    Mar 19, 2018
    Posts:
    1
    This only seems to work when I hit E or Q keys. How can I get this for pressing any key?
    I tried changing
    if (Input.GetKeyDown(KeyCode.Q))

    to
    if (Input.anyKey)
     
  10. shebuterne

    shebuterne

    Joined:
    Nov 9, 2017
    Posts:
    3
    Hi, everybody !
    I'm pretty new in Unity, and everything is going well except... text component !
    I've tried to make a fading text with the code given here : my text is fading well, but all the others texts components are fading at the same time !
    The most disturbing is that when I stop the game, the faded texts stay faded (I don't see them anymore). I really don't get it.
    Any help would be very welcome !
     
  11. Piranha771

    Piranha771

    Joined:
    Oct 19, 2016
    Posts:
    11
  12. Divis10nStudios

    Divis10nStudios

    Joined:
    Dec 26, 2017
    Posts:
    195
    don't add it to your canvas but add it to the TEXT that you want faded
     
    shebuterne likes this.