[Unity 2D/3D Tutorial] Quick, Basic Camera Fade In and Fade Out
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!
1 view
51
9
7 months ago 15:33:59 1
Unity - Лучший курс | С Нуля до Junior за ОДНО видео
7 months ago 00:05:27 1
С чего начать делать игры | Разработка для новичков
7 months ago 00:02:02 1
Как сделать полоску здоровья в Unity | Health bar in unity | Tutorial
7 months ago 00:01:03 4
Gennady - новая игра, хардкорный 2D-платформер студии FEED 64
7 months ago 00:10:33 1
How To... Billboarding in Unity 2020 - 2D Sprites in 3D
7 months ago 00:05:14 1
Character Customization Using Spritesheets - Unity Tutorial
7 months ago 00:28:15 2
2D Top Down игра на Unity с нуля #3 | Движение персонажа, Input Manager
7 months ago 00:29:33 1
2D Top Down игра на Unity с нуля #5 | Анимация главного героя
7 months ago 00:18:47 2
2D Top Down игра на Unity с нуля #6 | Коллайдеры
7 months ago 00:42:47 2
2D Top Down игра на Unity с нуля #7 | Добавляем врага, NavMesh, State Machine
7 months ago 00:22:03 3
2D Top Down игра на Unity с нуля #8 | Добавляем анимацию движения врага и его поворот
7 months ago 01:22:32 1
2D Top Down игра на Unity с нуля #9 | Заменяем спрайты, меч главного героя, события Unity
7 months ago 00:31:21 1
2D Top Down игра на Unity с нуля #10 | Эффект взмаха меча
7 months ago 00:29:07 1
2D Top Down игра на Unity с нуля #11 | Коллайдер для меча, нанесение урона врагу
7 months ago 01:03:07 1
2D Top Down игра на Unity с нуля #12 | Состояния врага. Состояние покоя, брождения, преследования.
7 months ago 00:34:35 1
2D Top Down игра на Unity с нуля #13 | Атака скелета
7 months ago 00:08:41 1
Procedural Walking Animation in Minecraft
7 months ago 01:41:15 4
Q&A о Nau Engine: все, что вы хотели спросить, в одном видео
7 months ago 00:18:03 10
Making Game Assets in Blender - Pet Lair - Atlas Empires
7 months ago 00:16:45 6
Making Mobile Game Assets - Atlas Empires - Ultimate Outpost
7 months ago 00:12:09 2
Making Mobile Game Assets - Atlas Empires - Summoning Stump
7 months ago 00:19:24 3
Making Mobile Game Assets - Atlas Empires - Spell Lab
7 months ago 00:18:23 12
Making Mobile Game Assets - Atlas Empires - Vault
7 months ago 00:06:55 1
Code Monkey - TOP 20 NEW VISUAL Assets MARCH 2024! (Animations, Effects, Textures) Unity Asset Store