# 3. UI Toolkit USS Transitions Part of [[Tweening and UI Juice]]. Back: [[2. A Hand-Rolled Tween Runner]]. Next: [[4. DOTween and PrimeTween Compared]]. > [!warning] Code not compiled in Unity > `MenuPanelTransitions` was type-checked against Unity 6000.5.8f1 (`PanelRenderer.RegisterUIReloadCallback`, `EnableInClassList`, `TransitionEndEvent`). It has not been run on a panel. USS was not opened in UI Builder. ## 3.1 Official properties Official 6000.5 USS transition table: | USS | C# (`IStyle`) | Role | |---|---|---| | `transition-property` | `transitionProperty` | Which properties animate | | `transition-duration` | `transitionDuration` | How long | | `transition-timing-function` | `transitionTimingFunction` | Easing | | `transition-delay` | `transitionDelay` | When it starts | | `transition` | (shorthand) | All of the above | A transition runs when a style value **changes** and a duration is set. Official triggers: `:hover` (or other pseudo-classes), C# class list changes (`EnableInClassList` / `ToggleInClassList` / `AddToClassList`), or assigning `element.style....`. Official constraints you will actually hit: 1. **First frame has no previous state.** "You must start a transition animation after the first frame." If you open a panel in `OnEnable` of the first frame, there may be no transition. Official workaround mentioned in the transition-event docs: set an initial style, then change it later (for example `schedule.Execute`). 2. **Units must match.** Official example: `translate` defaults to `0px`; transitioning to a percentage does not run. If you animate `left`, set `left: 0px` explicitly, not the default `auto`. 3. Put the transition **on the element**, not only on `:hover`. Official note: if the transition lives only on `:hover`, it will not run when the pointer leaves. 4. Shorthand properties fan out: changing `margin` produces separate lifecycles for each side. Lifecycle events (all bubble): `TransitionRunEvent` (created), `TransitionStartEvent` (after delay), `TransitionEndEvent` (finished), `TransitionCancelEvent` (property changed mid-transition). Official `TransitionEndEvent`: "in the case where a transition is removed before completion then the event will not fire." ## 3.2 MenuPanel USS and UXML Set start values and the transition on the panel. Toggle `is-open` for the end values. `translate` uses `px` on both ends. ```css #menu-panel { transition-property: opacity, translate; transition-duration: 200ms, 250ms; transition-timing-function: ease-out; opacity: 0; translate: 0 16px; } #menu-panel.is-open { opacity: 1; translate: 0 0; } ``` ```xml <ui:UXML xmlns:ui="UnityEngine.UIElements"> <ui:VisualElement name="menu-panel" class="menu-panel"> <ui:Label text="Paused" /> <ui:Button name="resume" text="Resume" /> </ui:VisualElement> </ui:UXML> ``` Wire the UXML to a `PanelRenderer` as in [[2.UI Toolkit Project Setup]]. Sort order above the gameplay HUD. ## 3.3 MenuPanelTransitions ```csharp using UnityEngine; using UnityEngine.UIElements; /// <summary> /// Opens and closes a UI Toolkit panel by toggling a USS class that has official /// transition-property / transition-duration rules. Listens for TransitionEndEvent. /// </summary> [RequireComponent(typeof(PanelRenderer))] public sealed class MenuPanelTransitions : MonoBehaviour { [SerializeField] string panelName = "menu-panel"; [SerializeField] string openClass = "is-open"; PanelRenderer _panelRenderer; VisualElement _panel; bool _open; PanelRenderer.VersionedUIReloadCallback _reload; public bool IsOpen => _open; public event System.Action Closed; void OnEnable() { _panelRenderer = GetComponent<PanelRenderer>(); _reload = OnUIReload; _panelRenderer.RegisterUIReloadCallback(_reload); } void OnDisable() { if (_panel != null) _panel.UnregisterCallback<TransitionEndEvent>(OnTransitionEnd); if (_panelRenderer != null && _reload != null) _panelRenderer.UnregisterUIReloadCallback(_reload); } void OnUIReload(PanelRenderer renderer, VisualElement rootElement, int version) { if (_panel != null) _panel.UnregisterCallback<TransitionEndEvent>(OnTransitionEnd); _panel = rootElement != null ? rootElement.Q(panelName) : null; if (_panel == null) { Debug.LogWarning("MenuPanelTransitions: no element named '" + panelName + "'.", this); return; } _panel.RegisterCallback<TransitionEndEvent>(OnTransitionEnd); ApplyOpenClass(_open); } public void SetOpen(bool open) { _open = open; ApplyOpenClass(open); } void ApplyOpenClass(bool open) { if (_panel == null) return; _panel.EnableInClassList(openClass, open); } void OnTransitionEnd(TransitionEndEvent evt) { if (evt.target != _panel) return; if (!_open) Closed?.Invoke(); } } ``` `SetOpen(true)` adds `is-open` and the USS transition runs. `SetOpen(false)` removes it. `Closed` fires on `TransitionEndEvent` only when the panel is closing — use that to hide the GameObject or return to gameplay, not at the instant of the click. If `Closed` never fires, the transition did not run (first frame, unit mismatch, or the transition was cancelled). Fall back to a `TweenRunner` unscaled fade, or delay the first open by a frame. Next: [[4. DOTween and PrimeTween Compared]].