Skip to content

Instantly share code, notes, and snippets.

@Kobusvdwalt
Last active January 31, 2022 22:23
Show Gist options
  • Save Kobusvdwalt/acc8442e334584713849 to your computer and use it in GitHub Desktop.
Save Kobusvdwalt/acc8442e334584713849 to your computer and use it in GitHub Desktop.
Simple script for fading any UI image in unity.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class FadeAnyImage : MonoBehaviour {
float timePassed = 0;
float totalFadeLength;
Image imageToFade;
AnimationCurve curve;
public void Fade (Image img, float fadeLength, AnimationCurve animCurve)
{
timePassed = 0;
totalFadeLength = fadeLength;
imageToFade = img;
curve = animCurve;
}
void Update ()
{
timePassed += Time.deltaTime;
Color temp = imageToFade.color;
temp.a = curve.Evaluate(timePassed/totalFadeLength);
imageToFade.color = temp;
if (timePassed / totalFadeLength > 1)
{
Destroy(this);
}
}
}
@Kobusvdwalt
Copy link
Author

To use this script you simple need to instantiate the script and then call the function Fade()

gameObject.AddComponent<FadeAnyImage>();
GetComponent<FadeAnyImage>().Fade(splashImage, splashLength, curve);

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