Hello. Today I have a quick tutorial for you on a very basic camera fader that can be used to fade scenes in and out. You can set any fade colour – let’s say you’re a fan of deep purple. You can also set the fade speed to give you something slow and gradual, or something quick and snappy. Finally, you can set the fade curve to any curve you want to give you any combination of fading in out, for example a slight flickering effect. The script is available on GitHub with a few of my other scripts. Scripts: *----*----* My Playlists *----*----* Unity Journey Of Discovery (Game Dev and Tutorials): The Main Course (Maths and Science Concepts Explained Visually: Eco-Pool Natural Swimming Pool (Build and Progress): 4K Treats (Ultra HD content): Finger Snacks (Shorter, Unclassified Videos): *----*----*----*----*----*----*----* The basic idea is that we create a one-pixel texture, give it a color, set its transparency, and then drag it across the entire screen in front of everything else, updating the transparency over time. You’ll usually want to call the fading from somewhere in your game, but for demonstration purposes I’m calling it when a specific key is pressed. This, as well as the following public variables, can be set in the editor, as we’ll see later. Next, we select the fading speed and color. We create a default fading curve to allow us to customize the fading sequence, and finally set whether we want the scene to start faded out already. As our private variables we have a float alpha controlling the current transparency, the texture we want to display over the scene, an integer for the direction of the fade, namely 1 for fading in and -1 for fading out, and finally the time progressed through the fade. In our start method we set alpha according to whether we want to start faded out or not, and then create a 2D texture consisting of a single pixel. We set the color of that pixel to the supplied fade color, with the current alpha as opacity or transparency. We have to apply this change to the texture. In our update method we initialize the fading out or fading in only if a fade is not currently in progress, and if the specified key was pressed in that frame. If the scene is faded out completely, we cap alpha at 1 and set time and direction so it will start decreasing alpha. If the scene is faded in completely, we floor alpha at 0 and set the time and direction such that it will start increasing alpha. All that remains is to actually draw the texture, which we only do if it isn’t completely transparent, and then we update our variable values if the direction is not equal to zero, so if we’re currently in a fading phase. We do this in the OnGUI method, which renders and handles graphical user interface events. We add deltaTime to our time variable, but we scale it with the specified speed and multiply it with direction to let it read from the fade curve in the correct direction. Then we set alpha to the point on the fade curve for the current time value, and we adjust the color of the one-pixel texture by changing the alpha. Again, we need to apply the change to the texture. Finally, we stop the fading whenever the scene is either fully faded in or fully faded out. This script is then attached to our camera, and we can run our scene with the default input values. Every time we pressed the space bar, the scene fades in our out, depending on its current state. You can now play around with these input values to give you different effects. And there you have it – a very basic camera fader. As I’ve said at the start, the script is available in GitHub, with the link in the description. Go forth and fade!
Hide player controls
Hide resume playing