Last active
January 31, 2022 22:23
-
-
Save Kobusvdwalt/acc8442e334584713849 to your computer and use it in GitHub Desktop.
Simple script for fading any UI image in unity.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use this script you simple need to instantiate the script and then call the function Fade()