Skip to content

Instantly share code, notes, and snippets.

@GuilleUCM
Created February 13, 2015 09:12
Show Gist options
  • Save GuilleUCM/b19ce22c85167cac8861 to your computer and use it in GitHub Desktop.
Save GuilleUCM/b19ce22c85167cac8861 to your computer and use it in GitHub Desktop.
Unity:Animation:Fade out alpha
using UnityEngine;
using System.Collections;
/// <summary>
/// Creates a fadeout animation modifying the alpha component of the first material color.
/// The material must be transparent
/// </summary>
public class FadeOut : MonoBehaviour {
/// <summary>
/// Seconds until completely transparent
/// </summary>
public float animationTime = 1.0f;
private bool accept = false;
private Material mat;
private float startTime;
private Color startColor;
/// <summary>
/// Initialization when enabled
/// </summary>
void OnEnable () {
Debug.Log("OnEnable");
mat = gameObject.renderer.material;
if (mat!=null) {
accept = true;
startTime = Time.time;
startColor = mat.color;
Debug.Log("accepted");
}
else {
Debug.Log("No renderere!!!");
}
}
// Update is called once per frame
void FixedUpdate () {
if (accept) {
float timeCovered = (Time.time - startTime);
float fracTime = timeCovered / animationTime;
Color c = startColor;
c.a = Mathf.Lerp(startColor.a, 0.0f, fracTime);
Debug.Log (c);
mat.color = c;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment