- Home /
Question by
Dilaxer01 · Apr 13, 2020 at 10:39 AM ·
texteventsystembutton trigger eventsmathf.lerpfadein
Mathf.lerp in an onClick method
Hello,
I want to fade in text when a button is clicked. How do I do that?
Regards
inkedlerp-li.jpg
(203.6 kB)
Comment
Hi @Dilaxer01 , you have to can this function in Update and interpolate between 0 to 1
float timeCounter =0;
void Update()
{
if(coinsNedded <= CoinStorate.coinAmount)
{
timeCounter += 1;
//for each rgb in color of textmesh component
$$anonymous$$athf.Lerp(0,1,timeCounter);
if(timecounter >= 1)
//reached to the color
}
}
Answer by Quickz · Apr 13, 2020 at 11:49 AM
I would suggest checking the docs regarding the Marf.Lerp method: https://docs.unity3d.com/ScriptReference/Mathf.Lerp.html
But in any case, here's an example of an Image fade in method using a Coroutine:
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class ImageFader : MonoBehaviour
{
[SerializeField]
private Image image = null;
[SerializeField]
private float speed = 1f;
private void Start()
{
StartCoroutine(FadeIn());
}
private IEnumerator FadeIn()
{
SetOpacity(0f);
while (image.color.a < 0.9f)
{
SetOpacity(Mathf.Lerp(
image.color.a,
1f,
Time.deltaTime * speed));
yield return new WaitForEndOfFrame();
}
while (image.color.a < 1f)
{
SetOpacity(Mathf.MoveTowards(
image.color.a,
1f,
Time.deltaTime / 5f * speed));
yield return new WaitForEndOfFrame();
}
Debug.Log("DONE");
}
private void SetOpacity(float opacity)
{
image.color = new Color(
image.color.r,
image.color.g,
image.color.b,
opacity);
}
}
Here's a reference regarding Coroutines: https://docs.unity3d.com/Manual/Coroutines.html