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

Fading a GUI.Label

Discussion in 'Scripting' started by JoeCBS, Jun 20, 2014.

  1. JoeCBS

    JoeCBS

    Joined:
    Feb 25, 2013
    Posts:
    41
    Hey there! I am using a GUI.Label when my Unlock happens to alert the player they have a new weapon. I get it to appear, but now I would like it to fade out. Everything I have a looked up doesn't seem to work. Here is the code. Just need it to fade out.

    Code (CSharp):
    1.     public void OnGUI() {
    2.    
    3.         if(alreadyUnlockedShotGun == true) {
    4.             Debug.Log ("GUI!");
    5.             GUI.Label (new Rect (Screen.width / 2, 40, 150, 25), "Shotgun Unlocked!");
    6.         }
    7.  
    8.     }
     
  2. Erisat

    Erisat

    Joined:
    Jan 31, 2013
    Posts:
    88
    maybe check this out, looks like similar thing with opacity. looks like hes just giving alpha transparancy to a color and then making that to be gui.color. http://answers.unity3d.com/questions/430949/gui-opacity.html
    i think thats what u meant. you could over time increase that alpha value, making it getting more and more transparent until it disappears. havent done that myself, but sounds like it makes sense
     
  3. JoeCBS

    JoeCBS

    Joined:
    Feb 25, 2013
    Posts:
    41
    Definitely looks interesting. Trying to find a way to fade it out, that makes it go from 100% opacity to 0% very very quick.
     
  4. Erisat

    Erisat

    Joined:
    Jan 31, 2013
    Posts:
    88
    you could be like: (maybe, havent tried)

    Code (CSharp):
    1.  
    2. float alphaAmount = 0f; //i believe 0 is none, and 1 is 100% dont quote me though >.>
    3.  
    4. void OnGUI()
    5. {
    6.  
    7. if(alreadyUnlockedShotGun == true && alphaAmount < 1f)
    8. alphaAmount += 0.1f * Time.deltaTime; //i think this will make it take 10 seconds to reach full transparency i think
    9. //higher value than 0.1f will make it go faster
    10.  
    11.  
    12. GUI.color = new Color(1,1,1,alphaAmount);
    13.  
    14.  
    15. if(alreadyUnlockedShotGun == true && alphaAmount < 1f)
    16. GUI.Label(new Rect (Screen.width / 2, 40, 150, 25), "Shotgun Unlocked!");
    17.  
    18. }
    19.  
    again idk if this will work, just off the top of my head. i think what this will do is if the label is being shown, increase alpha over time from 0% to 100%, and when it reaches 100%, dont show the label at all.

    edit: adding code tags >.>
     
    Last edited: Jun 20, 2014
  5. JoeCBS

    JoeCBS

    Joined:
    Feb 25, 2013
    Posts:
    41

    Ah! You're the best! Works perfefctly! Thanks much
     
  6. Erisat

    Erisat

    Joined:
    Jan 31, 2013
    Posts:
    88
    Glad i could help :D
     
  7. Erisat

    Erisat

    Joined:
    Jan 31, 2013
    Posts:
    88
    also, this being run will set the gui.color to that always, so you may want to store the color in a variable before running, and then after its done, reset gui.color to its original from the reference, so that you dont end up making your entire gui transparent and not just that label



    EDIT:
    I like this better than what i did before.

    EDIT2: i also included the changes i mentioned in my last post.

    Code (CSharp):
    1.  
    2.  
    3. Color originalColor;
    4. float alphaAmount = 0f;
    5. bool showFadingLabel = false;
    6. string fadingLabelContent = "";
    7.  
    8.  
    9. void Start()
    10. {
    11. originalColor = GUI.color;
    12. }
    13.  
    14. void OnGUI()
    15. {
    16.  
    17. if(showFadingLabel && alphaAmount < 1f)
    18. {
    19. alphaAmount += 0.1f * Time.deltaTime;
    20. GUI.color = new Color(originalColor.r,originalColor.g,originalColor.b,alphaAmount);
    21. GUI.Label(new Rect(Screen.width / 2, 40, 150, 25), fadingLabelContent);
    22. }
    23. else if(alphaAmount >= 1f)
    24. {
    25. alphaAmount = 0f;
    26. GUI.color = originalColor;
    27. showFadingLabel = false;
    28. }
    29.  
    30.  
    31. }
    32.  
    33.  
    34. void blah()//calling this shows the label
    35. {
    36. showFadingLabel = true;
    37. fadingLabelContent = "Shotgun Unlocked!";
    38. }
    39.  
    40.  
    41.  
     
    Last edited: Jun 20, 2014
  8. JoeCBS

    JoeCBS

    Joined:
    Feb 25, 2013
    Posts:
    41
    I slapped a GUIStyle in there to stop that :D thanks for the heads up though!
     
  9. Erisat

    Erisat

    Joined:
    Jan 31, 2013
    Posts:
    88
    oh alright, as long as it works :D you might also want to change that bool to be just like
    bool showFadingLabel = false;

    and add a string for the label content like
    string fadingLabelContent = "";

    and in ongui just pass that string to the label

    and in when you initialize it be like

    showFadingLabel = true;
    fadingLabelContent = "Shotgun Unlocked!";




    this way you could reuse that same little tidbit of code over and over again as i imagine youll have multiple weapon types to be unlocked, this would probably be easier on the eyes (dont forget to reset the alphaAmount or the label would start out transparent).
    Anyway, I think i've given my two cents already >.> Sometimes i talk too much, lmao. GL with your game
     
  10. JoeCBS

    JoeCBS

    Joined:
    Feb 25, 2013
    Posts:
    41

    With that code, It contonously fades it in then dissapears, over and over haha. Trying to figure out the cause
     
  11. Erisat

    Erisat

    Joined:
    Jan 31, 2013
    Posts:
    88
    did you try the changes i made to the code in the post i made before?

    1. Color originalColor;
    2. float alphaAmount = 0f;
    3. bool showFadingLabel = false;
    4. string fadingLabelContent = "";


    5. void Start()
    6. {
    7. originalColor = GUI.color;
    8. }

    9. void OnGUI()
    10. {

    11. if(showFadingLabel && alphaAmount < 1f)
    12. {
    13. alphaAmount += 0.1f * Time.deltaTime;
    14. GUI.color = new Color(originalColor.r,originalColor.g,originalColor.b,alphaAmount);
    15. GUI.Label(new Rect(Screen.width / 2, 40, 150, 25), fadingLabelContent);
    16. }
    17. else if(alphaAmount >= 1f)
    18. {
    19. alphaAmount = 0f;
    20. GUI.color = originalColor;
    21. showFadingLabel = false;
    22. }


    23. }


    24. void blah()//calling this shows the label
    25. {
    26. showFadingLabel = true;
    27. fadingLabelContent = "Shotgun Unlocked!";
    28. }

    i think that should work >.> call the blah method will show it, then the fading starts, and when the alphaamount is greater than 1, it disables the showing of the label. I don't see why it would repeat >.> but then again i just woke up so i might just be tired and missing something
     
  12. JoeCBS

    JoeCBS

    Joined:
    Feb 25, 2013
    Posts:
    41
    I have that exact code, it just repeats. Fades in, dissapears, fades in again. Over and over lol
     
  13. Erisat

    Erisat

    Joined:
    Jan 31, 2013
    Posts:
    88
    i just tested it in an empty scene, only attaching that same script to the main camera and then calling blah(); in start(). it works for me >.> doesnt repeat, although it fades in and not fades out, which is the opposite of desirable. but i'm not getting that repeating issue.
     
  14. Erisat

    Erisat

    Joined:
    Jan 31, 2013
    Posts:
    88
    tweaked it a bit, it fades out and not fades in this time. doesnt repeat.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class gui : MonoBehaviour {
    6.  
    7.  
    8.     Color originalColor;
    9.     float alphaAmount = 1f;
    10.     bool showFadingLabel = false;
    11.     string fadingLabelContent = "";
    12.  
    13.  
    14.     void Start()
    15.     {
    16.         originalColor = GUI.color;
    17.         blah();
    18.     }
    19.  
    20.     void OnGUI()
    21.     {
    22.      
    23.         if(showFadingLabel && alphaAmount <= 1f && alphaAmount > 0f)
    24.         {
    25.             GUI.color = new Color(originalColor.r,originalColor.g,originalColor.b,alphaAmount);
    26.             alphaAmount -= 0.1f * Time.deltaTime;
    27.             GUI.Label(new Rect(Screen.width / 2, 40, 150, 25), fadingLabelContent);
    28.         }
    29.         else if(alphaAmount <= 0f)
    30.         {
    31.             alphaAmount = 1f;
    32.             GUI.color = originalColor;
    33.             showFadingLabel = false;
    34.         }
    35.      
    36.      
    37.     }
    38.  
    39.  
    40.     void blah()//calling this shows the label
    41.     {
    42.         showFadingLabel = true;
    43.         fadingLabelContent = "Shotgun Unlocked!";
    44.     }
    45. }
    46.  
    47.  
    thats a copy paste of my entire script. works for me

    vid of it working fine:
     
    Last edited: Jun 21, 2014
  15. Erisat

    Erisat

    Joined:
    Jan 31, 2013
    Posts:
    88
    if you want to send me your script or post it here i can take a look and try to figure out why it would be repeating. i cant reproduce this issue without removing the part in the else if block that sets showFadingLabel = false; or by calling blah() more than once.