Skip to content

Instantly share code, notes, and snippets.

@JackDraak
Created February 10, 2016 05:13
Show Gist options
  • Save JackDraak/c5ed2219970e1cd67ef8 to your computer and use it in GitHub Desktop.
Save JackDraak/c5ed2219970e1cd67ef8 to your computer and use it in GitHub Desktop.
fader for Unity
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class FadePanel : MonoBehaviour {
public float fadeInTime= 1f;
private Image fadePanel;
private Color currentColor = new Color(0f,0f,0f,1f); // or = Color.black;
void Start() {
fadePanel = GetComponent<Image>(); if (!fadePanel) Debug.LogError (this + " fadePanel IMAGE failure");
}
void Update () {
if (Time.timeSinceLevelLoad < fadeInTime) {
float alphaChange = Time.deltaTime / fadeInTime;
currentColor.a -= alphaChange;
Debug.Log (this + " Delta: " + alphaChange + " for Alpha result: " + currentColor.a);
fadePanel.color = currentColor;
} else {
gameObject.SetActive(false);
}
}
}
@JackDraak
Copy link
Author

FadePanel (FadePanel) Delta: 0.006666666 for Alpha result: 0.9933333​
...
FadePanel (FadePanel) Delta: 0.005522184 for Alpha result: 0.4902869​
...
FadePanel (FadePanel) Delta: 0.005522385 for Alpha result: -0.001190957​

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment