Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Fade in/out

Discussion in 'Scripting' started by iammfa, Dec 31, 2009.

  1. iammfa

    iammfa

    Joined:
    Dec 3, 2009
    Posts:
    155
    Hi :D
    There is s scene in the intro of a game, this scene includes 640x480 GUI texture (developer name jpg image)

    I'm trying make this image fade in then after seconds fade out, but the problem is image fade in and stopped not fade out ...!


    Code (csharp):
    1. function Start () {
    2.    yield Fade(0, .5, 1);  // Start, end, length in seconds
    3.    }
    4.  
    5. function Fade (start : float, end : float, length : float) {
    6.    for (i = 0.0; i < 1.0; i += Time.deltaTime*(1/length)) {
    7.       guiTexture.color.a = Mathf.Lerp(start, end, i);
    8.       yield;
    9.    }
    10. }
    11. function End() {
    12.    yield Fade(.5, 0, 1);  // Start, end, length in seconds
    13.    }
    14.  
    what is my mistake here ..?
     
  2. Barbur

    Barbur

    Joined:
    Oct 30, 2009
    Posts:
    160
    I may think is probably because the function Mathf.Lerp(minimum, maximum, Time.time); that should go from a min value to a max. In your case when you perform Fade out you are going from a min value of 5 to 0. This is my guess :)

    EDIT: Ok sorry what I said in this post is not true :)
     
  3. iammfa

    iammfa

    Joined:
    Dec 3, 2009
    Posts:
    155
    This fade/in code:
    Code (csharp):
    1. function Start () {
    2.    yield Fade(0, .5, 1);  // Start, end, length in seconds
    3.    }
    4.  
    5. function Fade (start : float, end : float, length : float) {
    6.    for (i = 0.0; i < 1.0; i += Time.deltaTime*(1/length)) {
    7.       guiTexture.color.a = Mathf.Lerp(start, end, i);
    8.       yield;
    9.    }
    10. }
    This fade/out code:

    Code (csharp):
    1. function Start () {
    2.    yield Fade(.5, 0, 1);  // Start, end, length in seconds
    3.    }
    4.  
    5. function Fade (start : float, end : float, length : float) {
    6.    for (i = 0.0; i < 1.0; i += Time.deltaTime*(1/length)) {
    7.       guiTexture.color.a = Mathf.Lerp(start, end, i);
    8.       yield;
    9.    }
    10. }
    How can i apply them to one GUI texture object to make fade in then fade out
     
  4. Barbur

    Barbur

    Joined:
    Oct 30, 2009
    Posts:
    160
    I use this C# function for it:

    Code (csharp):
    1.  
    2. IEnumerator FadeAlpha (float from, float to, float duration)
    3.     {
    4.         float startTime = Time.time;
    5.         while (Time.time - startTime < duration)
    6.         {
    7.             m_FadeColor.a = Mathf.Lerp(from, to, (Time.time - startTime) / duration);
    8.  
    9.             yield return 0;
    10.         }
    11.     }
    But if you look for other posts you will find lots of stuff about Fading :)
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You're never calling the End() function. But I wouldn't suggest making that a separate function anyway.

    Code (csharp):
    1. function Start () {
    2.    yield Fade(0, .5, 1);  // Start, end, length in seconds
    3.    yield WaitForSeconds(1.0); // Or whatever
    4.    yield Fade(.5, 0, 1);  // Start, end, length in seconds  
    5. }
    --Eric
     
  6. ChaosFire

    ChaosFire

    Joined:
    Jul 16, 2010
    Posts:
    13
    hello everyone

    So im having a problem with my fade in function, I came here and this page was very helpful but i cant get mine to work. Its a javascript attached to a GUITexture. I just need the fade in ability I'm not using the fade out here is the code im using:

    function Start () {
    yield Fade(0, .5, 1); // Start, end, length in seconds
    }

    function Fade (start : float, end : float, length : float) {
    for (i = 0.0; i < 1.0; i += Time.deltaTime*(1/length)) {
    guiTexture.color.a = Mathf.Lerp(start, end, i);
    yield;
    }
    }

    as you can see the above was very inspiring but when i start the game my image dosent fade. Any help here would be greatly appreciated.
     
  7. andy_h

    andy_h

    Joined:
    Nov 3, 2009
    Posts:
    146
    Well I have used this one several times both for iPhone and PC/Mac use.

    Code (csharp):
    1.  
    2. var guiObject : GUITexture;
    3. var fadeTime = 1.0;
    4. enum Fade {In, Out}
    5.  
    6.  
    7. // Fade in the GUITexture, wait a couple of seconds, then fade it out
    8. function Start () {
    9.    yield FadeGUITexture(guiObject, fadeTime, Fade.In);
    10.    yield WaitForSeconds(2.0);
    11.    yield FadeGUITexture(guiObject, fadeTime, Fade.Out);
    12.    Application.LoadLevel (Application.loadedLevel+1);
    13.    }
    14.  
    15. function FadeGUITexture (guiObject : GUITexture, timer : float, fadeType : Fade) {
    16.    var start = fadeType == Fade.In? 0.0 : 1.0;
    17.    var end = fadeType == Fade.In? 1.0 : 0.0;
    18.    var i = 0.0;
    19.    var step = 1.0/timer;
    20.    
    21.    while (i < 1.0) {
    22.       i += step * Time.deltaTime;
    23.       guiObject.color.a = Mathf.Lerp(start, end, i)*.5;
    24.       yield;
    25.    }
    26. }
    27.  
    Hope it helps :D
     
  8. ChaosFire

    ChaosFire

    Joined:
    Jul 16, 2010
    Posts:
    13
    Thanx andy_h for the quick response and help but unfortunately it never worked...maybe i did something wrong with the code. I put in your code almost exactly, I did remove these 2 lines:

    yield FadeGUITexture(guiObject, fadeTime, Fade.Out);
    Application.LoadLevel (Application.loadedLevel+1);

    only because Im not useing a fade out and im not loading another level but other then that i kept it the same. I never got any errors or any wierd glitches it just never faded. Any more help would be greatly appreciated. Also im kinda a noob so please bare with me, thanks
     
  9. andy_h

    andy_h

    Joined:
    Nov 3, 2009
    Posts:
    146
    Did you attach the script to the GUI texture asset in the Hierarchy?
     
  10. ChaosFire

    ChaosFire

    Joined:
    Jul 16, 2010
    Posts:
    13
  11. ChaosFire

    ChaosFire

    Joined:
    Jul 16, 2010
    Posts:
    13
    sorry about that dident meen to hit the submit button issues on my side sorry anyway yes the script is attached to the GUITexture no problem there
     
  12. andy_h

    andy_h

    Joined:
    Nov 3, 2009
    Posts:
    146
    If you change the fade time on the top line, to say 5, does that work.

    I'm not getting any errors and it works fine here. The only difference is I have the fade time set to 3.
     
  13. ChaosFire

    ChaosFire

    Joined:
    Jul 16, 2010
    Posts:
    13
    Thanx again but still no change maybe it would help if I posted my all my code:

    var MaskTexture : Texture2D;
    static var depth : int;
    var guiObject : GUITexture;
    var fadeTime = 5.0;
    enum Fade {In, Out}


    function Update () {

    }

    // Fade in the GUITexture, wait a couple of seconds, then fade it out
    function Start () {
    yield FadeGUITexture(guiTexture, fadeTime, Fade.In);
    yield WaitForSeconds(2.0);
    //yield FadeGUITexture(guiObject, fadeTime, Fade.Out);
    // Application.LoadLevel (Application.loadedLevel+1);
    }

    function FadeGUITexture (guiObject : GUITexture, timer : float, fadeType : Fade) {
    var start = fadeType == Fade.In? 0.0 : 1.0;
    var end = fadeType == Fade.In? 1.0 : 0.0;
    var i = 0.0;
    var step = 1.0/timer;

    while (i < 1.0) {
    i += step * Time.deltaTime;
    guiObject.color.a = Mathf.Lerp(start, end, i)*.5;
    yield;
    }
    }


    function OnGUI()
    {
    GUI.DrawTexture(Rect((Screen.width / 2 - 280), 0.0, 560.0, 291.0),
    MaskTexture, ScaleMode.StretchToFill, true, 0);
    GUI.depth = 1;
    }

    the function onGUI is what places the image on the screen and its position hopefully this will give you a better clue as to the problem thanx
     
  14. andy_h

    andy_h

    Joined:
    Nov 3, 2009
    Posts:
    146
    I suspect it's because you are calling the fade before calling the GUI texture.

    Is it a single full screen texture in it's own scene, like a splash screen, or is it an element for a HUD or something.
     
  15. ChaosFire

    ChaosFire

    Joined:
    Jul 16, 2010
    Posts:
    13
    The image is an element for a HUD. I also moved the onGUI function below the Update function but above the start function to see if it was because of what you suggested it being below the fade function but again no change, the image still stays regular with no fade.
     
  16. andy_h

    andy_h

    Joined:
    Nov 3, 2009
    Posts:
    146
    Interesting. OK let me try a few things.
     
  17. ChaosFire

    ChaosFire

    Joined:
    Jul 16, 2010
    Posts:
    13
    sure thanx again for the help also not sure if this will help but before i used the onGUI to position the image I placed it on screen and positioned it useing the transform panel then used the fade in code I put up on my first comment and it worked no problem, but as soon as I put in the onGUI function for positioning and scaleing the fade stopped working...not sure if this helps but thought Id let you know just in case thanx again
     
  18. ChaosFire

    ChaosFire

    Joined:
    Jul 16, 2010
    Posts:
    13
    Hi All

    Has anybody been able to figure out this issue yet? Any help would be greatly appreciated thanks.
     
  19. tomvds

    tomvds

    Joined:
    Oct 10, 2008
    Posts:
    1,028
    You'll probably have to post the malfunctioning code here, or it will be almost impossible for anyone to help you.
     
  20. ChaosFire

    ChaosFire

    Joined:
    Jul 16, 2010
    Posts:
    13
    I did post my malfunctioning code on the first page but for convience sake here it is again:

    var MaskTexture : Texture2D;
    static var depth : int;
    var guiObject : GUITexture;
    var fadeTime = 5.0;
    enum Fade {In, Out}


    function OnGUI()
    {
    GUI.DrawTexture(Rect((Screen.width / 2 - 280), 0.0, 560.0, 291.0),
    MaskTexture, ScaleMode.StretchToFill, true, 0);
    GUI.depth = 1;
    }


    function Update () {

    }


    // Fade in the GUITexture, wait a couple of seconds, then fade it out
    function Start () {
    yield FadeGUITexture(guiTexture, fadeTime, Fade.In);
    yield WaitForSeconds(2.0);
    //yield FadeGUITexture(guiObject, fadeTime, Fade.Out);
    // Application.LoadLevel (Application.loadedLevel+1);
    }


    function FadeGUITexture (guiObject : GUITexture, timer : float, fadeType : Fade) {
    var start = fadeType == Fade.In? 0.0 : 1.0;
    var end = fadeType == Fade.In? 1.0 : 0.0;
    var i = 0.0;
    var step = 1.0/timer;

    while (i < 1.0) {
    i += step * Time.deltaTime;
    guiObject.color.a = Mathf.Lerp(start, end, i)*.5;
    yield;
    }
    }

    any help would be great thanks
     
  21. tomvds

    tomvds

    Joined:
    Oct 10, 2008
    Posts:
    1,028
    Apologies. I saw your question and didn't realize it was a follow up on a previous post. Guess that'll teach me for not reading the entire thread :p.

    Your problem is that you are changing the alpha of a GUITexture, but (if I understand correctly), you are now using OnGUI to draw it instead. Using OnGUI is completely different from using GUITexture. You can fix it like this:
    Code (csharp):
    1. var MaskTexture : Texture2D;
    2. static var depth : int;
    3. var fadeTime = 5.0;
    4. private var alpha = 0.0;
    5.  
    6. enum Fade {In, Out}
    7.  
    8.  
    9. function OnGUI()
    10. {
    11.         GUI.color = Color(1,1,1,alpha);
    12.     GUI.DrawTexture(Rect((Screen.width / 2 - 280), 0.0, 560.0, 291.0),
    13.         MaskTexture, ScaleMode.StretchToFill, true, 0);
    14.             GUI.depth = 1;
    15. }
    16.  
    17. // Fade in the GUITexture, wait a couple of seconds, then fade it out
    18. function Start () {
    19.    yield FadeOnGUIAlpha(fadeTime, Fade.In);
    20.    yield WaitForSeconds(2.0);
    21.    //yield FadeOnGUIAlpha(fadeTime, Fade.Out);
    22.   // Application.LoadLevel (Application.loadedLevel+1);
    23.    }
    24.  
    25. function FadeOnGUIAlpha (timer : float, fadeType : Fade) {
    26.    var start = fadeType == Fade.In? 0.0 : 1.0;
    27.    var end = fadeType == Fade.In? 1.0 : 0.0;
    28.    var i = 0.0;
    29.    var step = 1.0/timer;
    30.    
    31.    while (i < 1.0) {
    32.       i += step * Time.deltaTime;
    33.       alpha = Mathf.Lerp(start, end, i)*.5;
    34.       yield;
    35.    }
    36. }
    Note that you will not need a GUITexture at all for this, you can remove it from the game object.
     
  22. ChaosFire

    ChaosFire

    Joined:
    Jul 16, 2010
    Posts:
    13
    dude you are the man!!! This works great unfortunately the image dont come in solid, it comes in transparent so i can see right threw it. Any idea on how to fix that sorry if its a noob question but I am still new with this. Thanks again and any help with this final issue would be greatly appreciated.
     
  23. tomvds

    tomvds

    Joined:
    Oct 10, 2008
    Posts:
    1,028
    Perhaps adding
    Code (csharp):
    1. alpha = end;
    as last line in the FadeOnGUIAlpha function will solve that.
     
  24. ChaosFire

    ChaosFire

    Joined:
    Jul 16, 2010
    Posts:
    13
    I happily repeat my last comment " Dude you are the man!!!" It worked perfectly and my HUD is finally working right thank you so much for helping me solve this issue thats been plagueing me for over a week I seriously appreciate this thanks again.
     
  25. Rush-Rage-Games

    Rush-Rage-Games

    Joined:
    Sep 9, 2010
    Posts:
    1,997
    Exactly what I needed, thank you so much!!!!!!
     
  26. bawi

    bawi

    Joined:
    Aug 12, 2011
    Posts:
    16
  27. mcunha98

    mcunha98

    Joined:
    Jun 13, 2010
    Posts:
    261
    C# Version:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class FadeInOut : MonoBehaviour
    5. {
    6.     public enum FadeInOutType
    7.     {
    8.         In,
    9.         Out
    10.     }
    11.    
    12.     public Texture2D MaskTexture;
    13.     private int depth;
    14.     public float fadeTime = 5f;
    15.     private float alpha = 0f;
    16.    
    17.     void Start ()
    18.     {
    19.         StartCoroutine(FadeOnGUIAlpha(fadeTime,FadeInOutType.In));
    20.         //yield WaitForSeconds(2.0);
    21.     }
    22.    
    23.     void OnGUI()
    24.     {
    25.         GUI.color = new Color(1, 1, 1, alpha);
    26.         GUI.DrawTexture(new Rect((Screen.width / 2 - 280), 0f, 560f, 291f), MaskTexture, ScaleMode.StretchToFill, true, 0f);
    27.         GUI.depth = 1;
    28.     }
    29.  
    30.     IEnumerator FadeOnGUIAlpha (float timer,  FadeInOutType fadeType)
    31.     {
    32.         float start = fadeType == FadeInOutType.In ? 0f : 1f;
    33.         float end = fadeType == FadeInOutType.In ? 1f : 0f;
    34.         float i = 0f;
    35.         float step = 1f / timer;
    36.        
    37.         while (i < 1f)
    38.         {
    39.             i += step * Time.deltaTime;
    40.             alpha = Mathf.Lerp(start, end, i) * 0.5f;
    41.             yield return null;
    42.         }
    43.     }
    44. }
     
    harplot28 likes this.
  28. sebastardo

    sebastardo

    Joined:
    Nov 23, 2012
    Posts:
    1
    I need to say to you THANKS!! i spend all my day searching a fading effect and nothing but after all your code works PERFECTLY , you're AWESOME!
     
    mcunha98 likes this.