public class ChangeColor : MonoBehaviour
{
public GameObject buttonText;
public void Enter()
{
buttonText.GetComponent<Text>().CrossFadeColor(Color.yellow, 1f, true, false);
}
public void Exit()
{
buttonText.GetComponent<Text>().CrossFadeColor(Color.red, 1f, true, false);
}
}
ButtonText is a Text of button
If i write
buttonText.GetComponent<Text>().color = Color.red;
It works but i want the color change slowly
so i found CrossFadeColor method
“public void CrossFadeColor(Color targetColor, float duration, bool ignoreTimeScale, bool useAlpha);”
Maybe the method is deprecated?
I cant fin it in 2021.3
but i can find it in google and version 2018 api
For this project i am using 2020.3.32f1
I’m new in unity from 3 months ago
I don’t get a warning if I’m using a deprecated method
Sounds like CrossFadeColor is or was an extension method someone must have made and you must have used.
I’ve never heard of it.
But the process of slowly changing a value (ANY value including colors) is extremely simple.
Whatever you do, DO NOT USE COROUTINES for this problem.
Smoothing movement between any two particular values:
You have currentQuantity and desiredQuantity.
- only set desiredQuantity
- the code always moves currentQuantity towards desiredQuantity
- read currentQuantity for the smoothed value
Works for floats, Vectors, Colors, Quaternions, anything continuous or lerp-able.
The code: SmoothMovement.cs · GitHub
OR… just make a simple canned animation… No code needed!
@Kurt-Dekker Yeah, I had to look it up also cause I hadn’t seen it.
For OP. this does work just fine in Unity 2021. Just had the Using UnityEngine.UI; at the top and the code is there.
GetComponent<Text>().CrossFadeColor(Color.red, 10f, true, true);
This also works with TextMeshPro with Using TMPro;
GetComponent<TextMeshProUGUI>().CrossFadeColor(Color.red, 10f, true, true);
1 Like