How to use invokerepeating and make ui text fade in/out over time?

40 Views Asked by At

at the top

public TMP_Text savingIndicatorText;

start

private void Start()
{
    // Initialize savingIndicatorText to be fully transparent initially
    SetTextAlpha(savingIndicatorText, 0f);
    // Start autosave with a small initial delay and repeat every 10 seconds
    InvokeRepeating(nameof(AutosaveNotes), 2f, 10f);
}

then the two methods for the fade in out:

public IEnumerator FadeTextToFullAlpha(float t, TMP_Text i)
    {
        // Start fading in
        i.color = new Color(i.color.r, i.color.g, i.color.b, 0);
        while (i.color.a < 1.0f)
        {
            SetTextAlpha(i, i.color.a + (Time.deltaTime / t));
            yield return null;
        }

        // Wait for a moment when fully visible
        yield return new WaitForSeconds(1f);

        // Start fading out
        while (i.color.a > 0.0f)
        {
            SetTextAlpha(i, i.color.a - (Time.deltaTime / t));
            yield return null;
        }
    }

and

private void SetTextAlpha(TMP_Text textElement, float alpha)
{
    textElement.color = new Color(textElement.color.r, textElement.color.g, textElement.color.b, alpha);
}

and autosavenotes

private void AutosaveNotes()
{
    if (saveToFile && inputField.gameObject.activeSelf)
    {
        SaveToFile(inputField.text);
        StartCoroutine(FadeTextToFullAlpha(1f, savingIndicatorText));
    }
}

what it does now it's fading out the text once one time.

and i want it to fade out/in or in/out depending on setting time for example 3 so it will fade out/in for 3 seconds.

0

There are 0 best solutions below