In this lesson, you will learn how to use USS transition properties to add smooth, performant animations to your UI elements when their state or style changes.
When you modify the style of a UI element—such as changing its color or size when a user hovers over it—the change happens instantly by default. While functional, this can feel rigid and jarring. USS transitions allow you to bridge that gap by defining an animation duration and timing curve, making the transition between styles feel fluid and professional.
Defining Transitions in USS
To create a transition, you define four key properties within your USS selector:
transition-property: Specifies which style attribute should be animated (e.g.,width,background-color, oropacity).transition-duration: Sets how long the animation takes to complete (e.g.,0.3s).transition-timing-function: Controls the easing, such asease-in,ease-out, orlinear.transition-delay: (Optional) Sets how long to wait before starting the animation.
Consider a button that expands when a user hovers over it using the :hover pseudo-class:
.my-button {
width: 100px;
background-color: blue;
/* Define the transition on the base element */
transition-property: width, background-color;
transition-duration: 0.5s;
transition-timing-function: ease-out;
}
.my-button:hover {
width: 150px;
background-color: lightblue;
}
In this example, when the mouse enters the button's area, the UI Toolkit detects the state change to :hover. Because we defined transition-property on the base class, the system automatically interpolates the values for width and background-color over 0.5 seconds, rather than snapping them to the new values immediately.
Performance Benefits
Because these transitions are handled by the underlying UI Toolkit engine, they are highly optimized. By letting the engine manage the interpolation between states, you keep your code clean and ensure the animations remain performant even as your UI complexity grows. You don't need to write custom C# scripts to move elements frame-by-frame; simply define the start state, the end state, and the transition rules in your style sheet.
Key Takeaway: Use USS transition properties to define how UI elements interpolate between style states, turning abrupt changes into polished, high-performance animations.