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

Free basic camera fade in script

Discussion in 'Scripting' started by LaneFox, Dec 19, 2017.

  1. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,384
    So I was needing a camera fade script and after briefly looking for a free one realized that they were old or overcomplicated so I threw this together and wanted to post it in case someone else needs something really barebone to get going with.

    Just put it on a Camera object.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class FadeCamera : MonoBehaviour
    5. {
    6.     public AnimationCurve FadeCurve = new AnimationCurve(new Keyframe(0, 1), new Keyframe(0.6f, 0.7f, -1.8f, -1.2f), new Keyframe(1, 0));
    7.  
    8.     private float _alpha = 1;
    9.     private Texture2D _texture;
    10.     private bool _done;
    11.     private float _time;
    12.  
    13.     public void Reset()
    14.     {
    15.         _done = false;
    16.         _alpha = 1;
    17.         _time = 0;
    18.     }
    19.  
    20.     [RuntimeInitializeOnLoadMethod]
    21.     public void RedoFade()
    22.     {
    23.         Reset();
    24.     }
    25.  
    26.     public void OnGUI()
    27.     {
    28.         if (_done) return;
    29.         if (_texture == null) _texture = new Texture2D(1, 1);
    30.  
    31.         _texture.SetPixel(0, 0, new Color(0, 0, 0, _alpha));
    32.         _texture.Apply();
    33.  
    34.         _time += Time.deltaTime;
    35.         _alpha = FadeCurve.Evaluate(_time);
    36.         GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), _texture);
    37.  
    38.         if (_alpha <= 0) _done = true;
    39.     }
    40. }
     
    Melanzue likes this.
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Cool. I generally just uses a tweening engine to create a camera fade, considering I'll already have the tween in the project anyways. Always good to have more options.
     
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,842
    You don't need a tweening engine or more than one line of code — just have a giant black Image at the front of your Canvas, and call Graphic.FadeAlpha on it.
     
    grafiboys likes this.
  4. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,384
    I wanted zero setup and no canvas requirements. Add the script, press play and it's done. Dummy proof and easy.
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,842
    Fair enough. Thanks for sharing it.
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    I use the tweening engine because I can set an oncomplete call to do something after the fade ends, or an onUpdate call if I want something to happen during the fade. And since it's one line of code to call the tween, it's not really any different. Works great.

    Graphic.FadeAlpha is just another way of doing a fade.
     
    JoeStrout likes this.
  7. LucasRizzotto

    LucasRizzotto

    Joined:
    Dec 11, 2015
    Posts:
    26
    Good script - fortunately doesn't work in VR.
     
  8. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,384
    Haven't tested it in VR, but it basically just draws black pixels over the entire screen in the OnGUI() loop. Not sure why this would be ignored by the HMD since it's definitely something that should be seen and is surely rendered.
     
  9. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    My approach is to just have a single pixel texture which I stretch to fill the screen, and using the CanvasGroup component to adjust the alpha. Might work in VR.
     
    Last edited: Nov 9, 2020
  10. Deleted User

    Deleted User

    Guest

    Thank you so much! Very helpful!
     
  11. F00buzz

    F00buzz

    Joined:
    Oct 4, 2016
    Posts:
    11
    Hi Guys,

    I was looking for a solution to fade in from a black screen. The animation-curve config didn't work for me properly so I edited the Script. Since @LaneFox shared his code, I think it's fair to share mine, too.

    You can configure:
    - how long the Camera will be filled with a color
    - how fast will it fade
    - what color will the screen be filled with

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class ColorScreenFadeInCamera : MonoBehaviour
    4. {
    5.     [Tooltip("How fast should the texture be faded out?")]
    6.     public float fadeTime;
    7.  
    8.     [Tooltip("How long will the screen stay black?")]
    9.     public float blackScreenDuration;
    10.  
    11.     [Tooltip("Choose the color, which will fill the screen.")]
    12.     public Color fadeColor;
    13.  
    14.     private float _alpha = 1.0f;
    15.     private Texture2D _texture;
    16.  
    17.     private float _passedBlackScreenTime;
    18.  
    19.     private void Start()
    20.     {
    21.         _texture = new Texture2D(1, 1);
    22.         _texture.SetPixel(0, 0, new Color(fadeColor.r, fadeColor.g, fadeColor.b, _alpha));
    23.         _texture.Apply();
    24.     }
    25.  
    26.  
    27.     public void OnGUI()
    28.     {
    29.         // If the texture is no more visible, we are done.
    30.         if (_alpha <= 0.0f)
    31.         {
    32.             return;
    33.         }
    34.  
    35.         GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), _texture);
    36.  
    37.         if (_passedBlackScreenTime < blackScreenDuration)
    38.         {
    39.             _passedBlackScreenTime += Time.deltaTime;
    40.             return;
    41.         }
    42.  
    43.         calculateTexture();
    44.     }
    45.  
    46.     private void calculateTexture()
    47.     {
    48.         _alpha -= Time.deltaTime / fadeTime;
    49.         _texture.SetPixel(0, 0, new Color(fadeColor.r, fadeColor.g, fadeColor.b, _alpha));
    50.         _texture.Apply();
    51.     }
    52. }
     
    Last edited: Apr 29, 2020
    SaintBob likes this.
  12. X0FloH

    X0FloH

    Joined:
    May 25, 2016
    Posts:
    7
    I wanted a way to fade in and out of a scene, here is my extremely hacky, rushed and overcomplicated version of @LaneFox 's code

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FadeCamera : MonoBehaviour
    6. {
    7.     public AnimationCurve FadeCurve = new AnimationCurve(new Keyframe(0, 1), new Keyframe(0.6f, 0.7f, -1.8f, -1.2f), new Keyframe(1, 0));
    8.  
    9.     private float _alpha = 1;
    10.     private Texture2D _texture;
    11.     private bool _done;
    12.     private float _time;
    13.     private bool _reverse;
    14.  
    15.     public void Reset()
    16.     {
    17.         _done = false;
    18.         _alpha = 1;
    19.         _time = 0;
    20.         _reverse = false;
    21.     }
    22.  
    23.     public void Reverse()
    24.     {
    25.         _done = false;
    26.         _alpha = 0;
    27.         _time = 1;
    28.         _reverse = true;
    29.     }
    30.  
    31.     [RuntimeInitializeOnLoadMethod]
    32.     public void RedoFade()
    33.     {
    34.         Reset();
    35.     }
    36.  
    37.     public void OnGUI()
    38.     {
    39.         if (_texture == null) _texture = new Texture2D(1, 1);
    40.  
    41.         _texture.SetPixel(0, 0, new Color(0, 0, 0, _alpha));
    42.         _texture.Apply();
    43.  
    44.         if (!_reverse)
    45.         {
    46.             if (!_done)
    47.             {
    48.                 _time += Time.deltaTime;
    49.                 _alpha = FadeCurve.Evaluate(_time);
    50.                 GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), _texture);
    51.             }
    52.  
    53.             if (_alpha <= 0) _done = true;
    54.         } else
    55.         {
    56.             if (!_done)
    57.             {
    58.                 _time -= Time.deltaTime;
    59.                 _alpha = FadeCurve.Evaluate(_time);
    60.                 GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), _texture);
    61.             } else
    62.             {
    63.                 GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), _texture);
    64.             }
    65.  
    66.             if (_alpha >= 1) _done = true;
    67.         }
    68.     }
    69. }
     
  13. Sada4907

    Sada4907

    Joined:
    Apr 18, 2020
    Posts:
    2
    Hey how do I make it fade to brightness rather than darkness
    I tried inverting the fade curve but it just went dark to what it originally showed...
     
  14. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,497
    Replace this:
    _texture.SetPixel(0, 0, new Color(0, 0, 0, _alpha));

    With this:
    _texture.SetPixel(0, 0, new Color(1, 1, 1, _alpha));

    Edit: somehow got mangled, fixed it
     
  15. Sada4907

    Sada4907

    Joined:
    Apr 18, 2020
    Posts:
    2
    Thanks will check this out now
     
  16. SaintBob

    SaintBob

    Joined:
    Feb 21, 2014
    Posts:
    4
    My first time posting in this forum, actually made an account just to give my contribution as you guys are so nice showing your versions of what @LaneFox proposed. I started from @Grauen 's version but had a different idea in mind. I wanted a way to both fade-in and fade-out but also have methods to easily call it from any other script in the game so let's say a door when clicked can call the camera and tell it to fade-out before teleporting the character. Sorry if I am not compliant with all your common conventions.

    Didn't test the part about calling from outside yet, but it should work. Anyway, here it is, hope it helps someone... :)

    Code (CSharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class CameraManager : MonoBehaviour
    8. {
    9.     static private CameraManager instance;
    10.     static public CameraManager Instance {
    11.         get {
    12.             if (instance == null)
    13.                 instance = GameObject.FindObjectOfType<CameraManager>();
    14.  
    15.             return instance;
    16.         }
    17.     }
    18.  
    19.     [SerializeField]
    20.     [Tooltip("How fast should the texture be faded out?")]
    21.     private float fadeTime = 5.0f;
    22.  
    23.     [SerializeField]
    24.     [Tooltip("Choose the color, which will fill the screen.")]
    25.     private Color fadeColor = new Color(255.0f, 255.0f, 255.0f, 1.0f);
    26.  
    27.     [SerializeField]
    28.     [Tooltip("How long will the screen stay black during FadeIn?")]
    29.     private float blackScreenDuration;
    30.  
    31.     private float alpha = 1.0f;
    32.     private Texture2D texture;
    33.  
    34.     private bool isFadingIn = false;
    35.     private bool isFadingOut = false;
    36.  
    37.     private float currentTime = 0;
    38.  
    39.     public class Options {
    40.         public Nullable<float> fadeTime;
    41.         public Nullable<float> blackScreenDuration;
    42.         public Color fadeColor;
    43.     }
    44.  
    45.     private void Start()
    46.     {
    47.         texture = new Texture2D(1, 1);
    48.         texture.SetPixel(0, 0, new Color(fadeColor.r, fadeColor.g, fadeColor.b, alpha));
    49.         texture.Apply();
    50.  
    51.         FadeIn();
    52.     }
    53.  
    54.     private void startFading(bool isFadingIn, bool isFadingOut, Options options = null)
    55.     {
    56.         currentTime = 0;
    57.  
    58.         if (options != null)
    59.         {
    60.             if (options.fadeTime != null) fadeTime = (float) options.fadeTime;
    61.  
    62.             if (options.blackScreenDuration != null) blackScreenDuration = (float)options.blackScreenDuration;
    63.  
    64.             if (options.fadeColor != null) fadeColor = options.fadeColor;
    65.         }
    66.  
    67.         this.isFadingIn = isFadingIn;
    68.         this.isFadingOut = isFadingOut;
    69.     }
    70.  
    71.     public void FadeIn(Options options = null)
    72.     {
    73.         alpha = 1.0f;
    74.         startFading(true, false, options);
    75.     }
    76.  
    77.     public void FadeOut(Options options = null)
    78.     {
    79.         alpha = 0.0f;
    80.         startFading(false, true, options);
    81.     }
    82.  
    83.     public void OnGUI()
    84.     {
    85.         if (isFadingIn || isFadingOut)
    86.         {
    87.             showBlackScreen();
    88.         }
    89.     }
    90.  
    91.     private void showBlackScreen()
    92.     {
    93.         if (isFadingIn && alpha <= 0.0f)
    94.         {
    95.             isFadingIn = false;
    96.  
    97.             return;
    98.         }
    99.  
    100.         GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), texture);
    101.  
    102.         if (isFadingIn && blackScreenDuration > 0)
    103.         {
    104.             blackScreenDuration -= Time.deltaTime;
    105.  
    106.             return;
    107.         }
    108.  
    109.         if (isFadingOut && alpha >= 1.0f) return;
    110.          
    111.         calculateTexture();
    112.     }
    113.  
    114.     private void calculateTexture()
    115.     {
    116.         currentTime += Time.deltaTime;
    117.  
    118.         if (isFadingIn) alpha = 1.0f - currentTime / fadeTime;
    119.         else alpha = currentTime / fadeTime;
    120.  
    121.         texture.SetPixel(0, 0, new Color(fadeColor.r, fadeColor.g, fadeColor.b, alpha));
    122.         texture.Apply();
    123.     }
    124. }
    125.  
    Edit 1: Fixed a line out of OCD.
    Edit 2: Had forgotten to remove some test code.
    Edit 3: Realized my forum account is actually way older than I thought when I wrote this.
     
    Last edited: Jun 14, 2021
    LaneFox likes this.
  17. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
  18. SaintBob

    SaintBob

    Joined:
    Feb 21, 2014
    Posts:
    4
  19. Hoffy17

    Hoffy17

    Joined:
    Aug 17, 2021
    Posts:
    1
    Hi! Thanks so much for this script, it's really helped :)

    I'm having an issue where I can't called either the FadeIn or FadeOut functions OnClick () however, they don't show up in the inspector under the Camera Manager. I'd like my camera to fade out when a button is pressed. Any idea why that might be?
     
  20. DerDicke

    DerDicke

    Joined:
    Jun 30, 2015
    Posts:
    291
    Thanks to all of you and especially the OP.
    A clever alternative to the canvas jazz.