Skip to content

Instantly share code, notes, and snippets.

@CapnRat
Created October 19, 2014 15:49
Show Gist options
  • Select an option

  • Save CapnRat/2766a21a13e7938872ce to your computer and use it in GitHub Desktop.

Select an option

Save CapnRat/2766a21a13e7938872ce to your computer and use it in GitHub Desktop.

Revisions

  1. CapnRat created this gist Oct 19, 2014.
    32 changes: 32 additions & 0 deletions FadeOut
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    using UnityEngine;
    using System.Collections;

    public class FadeOut : MonoBehaviour
    {
    public float fadeOutTime = 5f;

    private SpriteRenderer spriteRenderer { get { return renderer as SpriteRenderer; } }

    void Update ()
    {
    if (Input.GetKeyDown(KeyCode.Space))
    StartCoroutine(DoFadeOut());
    }

    IEnumerator DoFadeOut ()
    {
    float startTime = Time.time;
    while (true)
    {
    float fade = (Time.time - startTime) / fadeOutTime;
    var color = spriteRenderer.color;
    color.a = Mathf.Lerp (1f, 0f, fade);
    spriteRenderer.color = color;

    if (fade >= 1f)
    break;

    yield return 1;
    }
    }
    }