Changing Text Mid-Animation

By default animated text elements reset their animation whenever you change the value of .text . To create a continuous animation while changing text it's possible to use the SetTime() function to manually manage the animation progress.

Example

The following script pauses the text element, which keeps it from advancing the internal timer on its own, then it manually sets the time each frame.

using TextAnimationsForUIToolkit;
using UnityEngine;
using UnityEngine.UIElements;

public class ChangeTextMidAnimation : MonoBehaviour
{
    public UIDocument uiDocument;

    private IAnimatedTextElement _element;
    private float _startTime;

    private void Start()
    {
        _element = TextAnimationUtility.GetAnimatedTextElement(uiDocument, "animated-timer");

        // Stop the visual element from automatically advancing time
        _element.Pause();
        _startTime = Time.time;
    }

    private void Update()
    {
        var currentTime = Time.time - _startTime;
        _element.text = $"<wave>Timer: {currentTime:F}</wave>";
        _element.SetTime(currentTime);
    }
}

Last updated