How to animate the TMPro.TMP_Text.colorGradient property #114
-
I tried to Lerp textmesh pro gradient with Tween.Custom (to edit a curve in the inspector) with no succes. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Can you show your code? Animating something is very similar to setting the property directly. So if you can change the property without animation, then animating it with PrimeTween should be very similar. |
Beta Was this translation helpful? Give feedback.
-
Here is one of the possible ways to animate the using PrimeTween;
using TMPro;
using UnityEngine;
public class PrimeTweenExample : MonoBehaviour {
[SerializeField] TMP_Text text;
[SerializeField] TweenSettings tweenSettings;
[SerializeField] VertexGradient startGradient, endGradient;
public Tween AnimateTextGradient() {
return Tween.Custom(this, 0f, 1f, tweenSettings, (target, t) => {
target.text.colorGradient = LerpVertexGradient(target.startGradient, target.endGradient, t);
});
// same as above but allocates GC memory because of delegate allocation
// more info: https://github.com/KyryloKuzyk/PrimeTween?tab=readme-ov-file#zero-allocations-with-delegates
return Tween.Custom(0f, 1f, tweenSettings, t => {
text.colorGradient = LerpVertexGradient(startGradient, endGradient, t);
});
}
public static VertexGradient LerpVertexGradient(VertexGradient a, VertexGradient b, float t) {
return new VertexGradient(
Color.Lerp(a.topLeft, b.topLeft, t),
Color.Lerp(a.topRight, b.topRight, t),
Color.Lerp(a.bottomLeft, b.bottomLeft, t),
Color.Lerp(a.bottomRight, b.bottomRight, t));
}
} |
Beta Was this translation helpful? Give feedback.
-
It works perfectly, Thanks! |
Beta Was this translation helpful? Give feedback.
Here is one of the possible ways to animate the
TMPro.TMP_Text.colorGradient
property: