fade in scene when level is loaded, fade out when button pressed
hi all! i'm new. i want to fade in the scene when the level loads in and fade out when the player presses the space bar to get to the next level and do the same thing in the next level. i have no idea how to do this, looking up tutorials but didn't get it to work. Thanks
void Update()
{
if (Application.loadedLevel == 2)//if loaded level is 1
{
{
if (score >= 0)//if score is 0
DesTime -= Time.deltaTime;//level description kill time countdown
{
if (DesTime <= 0)
Destroy(LevelDes);
if (Input.GetButtonDown("Restart"))
Application.LoadLevel(Application.loadedLevel);
}
}
if (score >= 8)//if score is 8
{
if (Input.GetButtonDown("Jump"))
Application.LoadLevel("Level2");//load level 2
}
}
Comment
Try this out.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.Scene$$anonymous$$anagement;
public class ScreenFader : $$anonymous$$onoBehaviour
{
public Image FadeImg;
public float fadeSpeed = 1.5f;
void Awake()
{
if (FadeImg != null)
{
StartCoroutine(DoFade(true, fadeSpeed, -1));
}
}
IEnumerator DoFade(bool fadeIn, float fadeTime, int sceneIndex)
{
bool done = false;
float time = 0;
Color startColor = FadeImg.color;
startColor.a = (!fadeIn) ? 0 : 1;
Color endColor = new Color(FadeImg.color.r, FadeImg.color.g, FadeImg.color.b, fadeIn ? 0 : 1);
FadeImg.enabled = true;
while (!done)
{
time += Time.deltaTime;
float lerpValue = time / fadeTime;
FadeImg.color = Color.Lerp (startColor, endColor, lerpValue);
yield return null;
if (fadeIn)
done = FadeImg.color.a <= 0.0f;
else
done = FadeImg.color.a >= 1.0f;
}
yield return new WaitForSeconds(1);
if (sceneIndex >= 0)
{
Scene$$anonymous$$anager.LoadScene(sceneIndex);
yield return new WaitForSeconds(0.5f);
}
FadeImg.enabled = false;
}
public void LoadScene(int SceneNumber)
{
StartCoroutine(DoFade(false, fadeSpeed, SceneNumber));
}
public void RestartScene()
{
LoadScene(Scene$$anonymous$$anager.GetActiveScene().buildIndex);
}
public void LoadNextScene()
{
if (Scene$$anonymous$$anager.GetActiveScene().buildIndex < (Scene$$anonymous$$anager.sceneCountInBuildSettings - 1))
LoadScene(Scene$$anonymous$$anager.GetActiveScene().buildIndex + 1);
else
RestartScene();
}
public void LoadPreviousScene()
{
if (Scene$$anonymous$$anager.GetActiveScene().buildIndex > 0)
LoadScene(Scene$$anonymous$$anager.GetActiveScene().buildIndex - 1);
else
RestartScene();
}
}
The public functions for loading scenes are
LoadScene(int SceneNumber)
RestartScene()
LoadNextScene()
LoadPreviousScene()
Your answer
![](https://koobas.hobune.stream/wayback/20220612100941im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
how to fade away scene for few seconds 2 Answers
How do I slowly fade text? 0 Answers
GUYS! Need to make fade in when level ending 0 Answers
Fade child object with a coroutine 0 Answers