Skip to content

Instantly share code, notes, and snippets.

@TheCuttlefish
Created April 25, 2023 14:50
Show Gist options
  • Save TheCuttlefish/780a115c516d38236d6306ade8b960ab to your computer and use it in GitHub Desktop.
Save TheCuttlefish/780a115c516d38236d6306ade8b960ab to your computer and use it in GitHub Desktop.
Screen Fade
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Fade : MonoBehaviour
{
CanvasGroup group;
float timer;
float fadeOutTimer;
bool startFadeOut;
[Range(1f,5f)]
public float seconds = 1f;
void Awake()
{
group = GetComponent<CanvasGroup>();
group.alpha = 1f;
}
void Update()
{
if (timer < 1)
{
timer += Time.deltaTime / seconds ;
group.alpha = 1 - timer;
}else
{
group.alpha = 0f;
}
if (Input.GetKeyDown(KeyCode.Space))
{
startFadeOut = true;
}
if (startFadeOut)
{
if (fadeOutTimer < 1)
{
fadeOutTimer += Time.deltaTime / seconds ;
group.alpha = fadeOutTimer;
}else
{
group.alpha = 1f;
print("load scene!!");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment