12 years ago

Unity Screen Fade In/Out Tutorial


Hello,

We thought we would put another little unity dev tutorial up for your viewing. This tutorial will feature how to create a simple fade in/out script.

The basis of this is to first, create a GameObject that can be anywhere in the scene. To this object attache a GUITexture. Give this GUITexture a any black texture, and set the width and height to any aspect you want, as you can see from the image below, we chose 1920x1080 as this is the max resolution of that the game will run at.

After this add a script, for ease of discussion we have decided to copy the entire source of code from our SceneFader script. Please feel free to use it if you ever need.


using UnityEngine;

using System.Collections;

public class SceneFadeInOut : MonoBehaviour

{

public float fadeSpeed = 1.5f; // Speed that the screen fades to and from black.

private bool sceneStarting = true; // Whether or not the scene is still fading in.

public GUITexture guiTextre;

void Awake ()

{

// Set the texture so that it is the the size of the screen and covers it.

guiTexture.pixelInset = new Rect(0f, 0f, Screen.width, Screen.height);

}

void Update ()

{

// If the scene is starting…

if(sceneStarting)

// … call the StartScene function.

StartScene();

}

public void FadeToClear ()

{

// Lerp the colour of the texture between itself and transparent.

guiTexture.color = Color.Lerp(guiTexture.color, Color.clear, fadeSpeed * Time.deltaTime);

}

public void FadeToBlack ()

{

// Lerp the colour of the texture between itself and black.

guiTexture.color = Color.Lerp(guiTexture.color, Color.black, fadeSpeed * Time.deltaTime);

}

void StartScene ()

{

// Fade the texture to clear.

FadeToClear();

// If the texture is almost clear…

if(guiTexture.color.a <= 0.05f)

{

// … set the colour to clear and disable the GUITexture.

guiTexture.color = Color.clear;

guiTexture.enabled = false;

// The scene is no longer starting.

sceneStarting = false;

}

}

public void EndScene (string name)

{

// Make sure the texture is enabled.

guiTexture.enabled = true;

// Start fading towards black.

FadeToBlack();

// If the screen is almost black…

if(guiTexture.color.a >= 0.95f)

// … reload the level.

Application.LoadLevel(name);

}

}


The last step is to set the fade speed to a required prefference in the editor, and you should have a working fade in script when you first run the scene.

We hope this helps.

Stay tuned for more as we move closer to the demo’s release.

Thanks

Deceptive Games



1 comment

Loading...

Next up

Just as Pecaminosa's musicians #improvise this rendition of #LittleSunflower 🌻, so do we improvise with this post you're reading. 😂

What's coming out of your own improvs? 🤔

#Pecaminosa | #IndieDev | #OST | #Jazz

Shoobies leave a sticky trail of mucus in their wake, which can impede movement for any creature that steps in it.

🍒Did you like the game? A demo is available for free on Steam.

✌️ Thanks for playing IndieGameiacs!

⚙️ Free Demo: http://bit.ly/UniDuni

📺 Full Video: https://youtu.be/5qEbVXg7GaQ

#steam #uniduni #clops #screenshotsaturday

Shadow The Hedgehog X pixel art

What do you think guys in my robot?Just a beginner in 3d modeling..

My instagram https://www.instagram.com/rojhonbb/

Sometimes the suction cup get sticky. Small animation I made in Blender after doing my tutorial on picking up/dropping objects in animations. Crate model by jQueary (https://sketchfab.com/3d-models/game-ready-sci-fi-crate-d98deca6…).

One of the most critical update for Sunblaze demo. Now you are able to pet a cat! Finally!

Microsoft Windows XP Unprofessional (windows logo prototype)

Hi everyone! I started to create some environment props for my new video game, here is a cozy house in the forest. I'm planning to switch from Unity to Unreal... Let's see what new challenges I have to face. Made in Blender and Substance Painter.