Skip to content

Instantly share code, notes, and snippets.

@vikhik
Created September 26, 2017 04:39
Show Gist options
  • Save vikhik/d2b72a15c1b9dc75e70266cdca1404ed to your computer and use it in GitHub Desktop.
Save vikhik/d2b72a15c1b9dc75e70266cdca1404ed to your computer and use it in GitHub Desktop.
public float TargetAlpha
{
// TODO: validate on Set (0.f .. 1.f)
get; set;
};
void Update() {
Fade();
}
void Fade()
{
Color c = renderer.material.color;
if (!Mathf.Approximately(c.a, targetAlpha))
{
// Scaling up or down
// TODO: Move to 'rate' variable?
var alphaAdjust = targetAlpha < c.a ? -.1f : .1f;
// Frame rate independence
alphaAdjust *= Time.deltaTime;
// TODO: Check potential bug with shifting c.a past target alpha and oscilating, verify if not an issue depending on FPS?
c.a += alphaAdjust;
renderer.material.color = c;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment