Version: 2022.2
Language : English
Create a transition event
UI Renderer

Create looping transitions

Version: 2022.1+

This example demonstrates how to leverage the TransitionEndEvent to create transitions that loop.

Example overview

This example demonstrates two looping animations:

  • Yo-yo: The loop starts from state A to state B with transition, and then return back to state A with transition.
  • A-to-B: The loop starts from state A to state B with transition, and then return back to state A without transition.
Window shows a yo-yo and a A-to-B loop transition effects
Window shows a yo-yo and a A-to-B loop transition effects

You can find the completed files that this example creates in this GitHub repository.


This guide is for developers familiar with the Unity Editor, UI(User Interface) Allows a user to interact with your application. Unity currently supports three UI systems. More info
See in Glossary
Toolkit, and C# scripting. Before you start, get familiar with the following:

Create the example

  1. Create a project in Unity with any template.

  2. In your Project windowA window that shows the contents of your Assets folder (Project tab) More info
    See in Glossary
    , create a folder named loop-transition-example.

  3. Right-click in the folder, and select Create > UI Toolkit > Editor Window.

  4. In the UI Toolkit Editor Window Creator window, enter LoopingExample.

  5. Save your changes. This creates three files as LoopingExample.cs, LoopingExample.uss, and LoopingExample.uxml.

  6. Replace LoopingExample.cs with the following content:

    using UnityEditor;
    using UnityEngine;
    using UnityEngine.UIElements;
    public class LoopingExample : EditorWindow
        [SerializeField] private VisualTreeAsset m_VisualTreeAsset = default;
        private Label _yoyoLabel;
        private Label _a2bLabel;
        [MenuItem("Window/UI Toolkit/Transition Looping Example")]
        public static void ShowExample()
            var wnd = GetWindow<LoopingExample>();
            wnd.titleContent = new GUIContent("TransitionStyle");
        public void CreateGUI()
            VisualElement root = rootVisualElement;
            VisualElement asset = m_VisualTreeAsset.Instantiate();
        // This method powers the yo-yo loop.
        private void SetupYoyo(VisualElement root)
            _yoyoLabel = root.Q<Label>(name: "yoyo-label");
            // When the animation ends, the callback toggles a class to set the scale to 1.3 
            // or back to 1.0 when it's removed.
            _yoyoLabel.RegisterCallback<TransitionEndEvent>(evt => _yoyoLabel.ToggleInClassList("enlarge-scale-yoyo"));
            // Schedule the first transition 100 milliseconds after the root.schedule.Execute method is called.
            root.schedule.Execute(() => _yoyoLabel.ToggleInClassList("enlarge-scale-yoyo")).StartingIn(100);
        // This method powers the A-to-B cycle.
        private void SetupA2B(VisualElement root)
            _a2bLabel = root.Q<Label>(name:"a2b-label");
            _a2bLabel.RegisterCallback<TransitionEndEvent>(evt =>
                _a2bLabel.schedule.Execute(() => _a2bLabel.AddToClassList("enlarge-scale-a2b")).StartingIn(10);
            _a2bLabel.schedule.Execute(() => _a2bLabel.AddToClassList("enlarge-scale-a2b")).StartingIn(100);
  7. Replace LoopingExample.uxml with the following content:

    <ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" 
             engine="UnityEngine.UIElements" editor="UnityEditor.UIElements" 
        <Style src="LoopingExample.uss" />
        <ui:VisualElement name="container">
                    <ui:Label text="Yo-yo Transition" name="yoyo-label" class="text-style" />
                    <ui:Label text="A-to-B Transition" name="a2b-label" class="text-style"/>
  8. Replace LoopingExample.uss with the following content:

            transition-duration: 3s;
        .text-style {
            font-size: 20px;
            flex-grow: 0;
            margin: 20px; 
            scale: 1.5 1.5;
            transition-duration: 3s;
            scale: 1.5 1.5;
            justify-content: space-around;
            align-items: center;
  9. To test the example, from the menu, select Window -> UI Toolkit -> Transition Looping Example.

Additional resources

Create a transition event
UI Renderer