Skip to content

Instantly share code, notes, and snippets.

@jakevsrobots
Created March 12, 2013 22:27
Show Gist options
  • Save jakevsrobots/5147674 to your computer and use it in GitHub Desktop.
Save jakevsrobots/5147674 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
public class CameraFader : MonoBehaviour
{
public static CameraFader main;
public Texture2D blackTexture;
private float alpha = 0f;
void Awake()
{
main = this;
}
void OnGUI()
{
GUI.color = new Color(0, 0, 0, alpha);
GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height ), blackTexture);
}
public void FadeIn(float duration)
{
StartCoroutine(FadeIn);
}
public void FadeOut(float duration)
{
StartCoroutine(FadeOut);
}
public IEnumerator FadeIn(float duration)
{
alpha = 1;
float timePassed = 0;
while(timePassed < duration)
{
timePassed += Time.deltaTime;
alpha = Mathf.Lerp(1, 0, timePassed/duration);
yield return null;
}
alpha = 0;
yield return null;
}
public IEnumerator FadeOut(float duration)
{
alpha = 0;
float timePassed = 0;
while(timePassed < duration)
{
timePassed += Time.deltaTime;
alpha = Mathf.Lerp(0, 1, timePassed/duration);
yield return null;
}
alpha = 1;
yield return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment