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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Fading in and out of scene

Discussion in 'Getting Started' started by Goodeddie, Aug 23, 2016.

  1. Goodeddie

    Goodeddie

    Joined:
    Aug 3, 2016
    Posts:
    79
    Hello.

    Does anybody know of a good fading in and out tutorial that is updating? The few that I found were outdated and confused me.

    Thanks
     
  2. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    813
    Get a tweening engine, like DOTween. It's easy to make things fade in and out with a tweening engine, plus do all sorts of other animations like slide in and out.
     
  3. Goodeddie

    Goodeddie

    Joined:
    Aug 3, 2016
    Posts:
    79
    Thanks! But I ended up using DrawRect in the OnGUI method and changing the alpha each time the method is called. The only thing I have to figure out now is how to calculate the length of time that passes for the entire fade.
     
  4. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Here is some code snippets you can add to your project to fade things
    Code (CSharp):
    1. // add these variables at the top of your class
    2. private bool isFading;
    3. private float elapsedTime;
    4.  
    5. void Start()
    6. {
    7.     isFading = false;
    8.     elapsedTime = 0;
    9. }
    10.  
    11. void Update()
    12. {
    13.      if (isFading)
    14.      {
    15.               // this is the time since the last Update
    16.               elapsedTime += Time.deltaTime
    17.               YourFunctionToFade()
    18.      }
    19. }
    20.  
    21. void YourFunctionToFade()
    22. {
    23.       // Write your code here to fade based
    24.       // on how big elapsedTime is
    25.  
    26.        // check if we've faded to target color (black?)
    27.        if (  // code to check our color is correct) )
    28.         {
    29.                isFading = false;
    30.                // maybe other code here like Load the next scene
    31.          }
    32. }
    33.  
    34. // call this to start the fade
    35. void StartFade()
    36. {
    37.          //reset our elapsedTIme and set the IsFading flag
    38.         elapsedTime = 0;
    39.         isFading = true;
    40. }
     
  5. Goodeddie

    Goodeddie

    Joined:
    Aug 3, 2016
    Posts:
    79
    Thanks! I should've said so but I've already got fading done. Thanks thanks!