USS transform
Understand the USS transform properties, their syntax, and accepted values.
読み終わるまでの所要時間 7 分最終更新 22日前
The transform properties apply a 2D transformation to a visual element. You can use them to rotate, scale, or move a visual element.
If you change the layout of an element, Unity recalculates the layout of other elements in the same hierarchy. This recalculation might reduce an animation's frame rate. Applying transform to an element reduces recalculations because it doesn't change the layout of other elements in the hierarchy.
It's possible to use transform to define the static appearance of a visual element. However, transform is best used for changes and animations. For example, if you want to make a visual element shake when an event happens in an application, set the position of the visual element using the regular layout properties such as and , and then use to align an offset relative to the initial position.
toplefttranslateTransform includes the following properties:
Property | USS syntax | Description |
|---|---|---|
| Transform Origin | | Represents the point of origin where rotation, scaling, and translation occur. |
| Translate | | Repositions the visual element in horizontal or vertical directions. |
| Scale | | Changes the apparent size, padding, border, and margins of a visual element. Negative values flip visual elements along the scale axis. |
| Rotate | | Rotates a visual element. Positive values represent clockwise rotation and negative values represent counterclockwise rotation. You can set rotation with |
- Scale
- Rotate
- Translate
You can set transform properties for a visual element using the controls in the UI Builder, within a USS file, or using a C# script.
Transform controls in the UI Builder
You can use the controls in the Transform section of the in the UI Builder to set the transform properties of a visual element.
Inspector
?
Pivot Origin
The Pivot Origin widget sets the transform origin property. To use it, do one of the following:
- Click a point in the widget to set the origin to a corner, the center of an edge, or the center. You can also define the values using the keyboard. When the widget is in focus, use the arrow keys to specify a point in the widget.
- Enter values for X and Y and specify the unit.
If you use percentages for both the X and Y values, the widget shows the new origin point when you edit the X and Y text boxes.
If you specify a transform origin point outside the element, such as having a value less than 0% or greater than 100%, the widget shows the directions of the X and Y axes.

A transform origin with an X value less than 0, and a Y value greater than 100. The pivot origin is highlighted in the bottom left corner.
Translate
The Translate control sets the translate property. To use it, enter values in the X and Y boxes and specify the unit.
Scale
The Scale control sets the scale property. To use it, enter values in the X and Y boxes and specify the unit.
Rotate
The Rotate control sets the rotate property. To use it, set the Axis values in the X, Y, and Z boxes, and set the angle value in the Angle box.
USS transform properties
You can use styling rules to set the transform properties for a visual element. You can set the styling rules within a USS file or inline in a UXML file.
transform-origin
transform-originThe property sets the transform origin along the X and Y axes in pixels or percentages.
transform-originYou can also use keywords to set the value of the attribute. These keywords match the dots in the widget in the UI Builder. The following keywords are supported:
transform-originPivot point | Keywords |
|---|---|
| Center |
|
| Center of left edge |
|
| Center of right edge |
|
| Center of top edge |
|
| Center of bottom edge |
|
| Top-left corner |
|
| Top-right corner |
|
| Bottom-left corner |
|
| Bottom-right corner |
|
Examples
transform-origin: 0% 100%;transform-origin: 20px 10px;transform-origin: 0px 100%;transform-origin: 60% 10px;
translate
translateThe property sets the translation along the X and Y axes in pixels or percentages relative to the size of this visual element. You can omit Y if it equals X.
translateExamples
translate: 80%;translate: 35px;translate: 5% 10px;translate: 24px 0%;
scale
scaleThe property sets the scale along the X and Y axes in pixels or percentages. You can omit Y if it equals X.
scaleThe keyword sets no scale.
noneExamples
scale: 2.5;scale: -1 1;scale: none;
rotate
rotateThe property sets the rotation using a number or a unit.
rotateThe keyword sets no rotation.
noneExamples
rotate: 45deg;rotate: -100grad;rotate: -3.14rad;rotate: 0.75turn;rotate: none;
Transform C# properties
You can set the transform properties for a visual element in a C# script.
IStyle.transformOrigin
IStyle.transformOriginThe property sets the transform origin.
IStyle.transformOriginThe property of the is of type . Its constructor takes a as an argument. You can construct a new using an X value and a Y value. The X value and the Y value are of type .
transformOriginstyleStyleTransformOriginTransformOriginTransformOriginLengthExamples
//This example sets the transform origin of the element to be 100 pixels from the left edge and 50% of the way down from the top edge.element.style.transformOrigin = new StyleTransformOrigin(new TransformOrigin(new Length(100f, LengthUnit.Pixel), new Length(50f, LengthUnit.Percent)));
You can simplify the above code as follows using implicit conversions:
element.style.transformOrigin = new TransformOrigin(100, Length.Percent(50));
IStyle.translate
IStyle.translateThe property sets the translation.
IStyle.translateIStyle.translateStyleTranslateTranslateTranslateLengthExamples
//This example sets the translation of the element. The X-axis is 10% and the Y-axis is 50 pixels.element.style.translate = new StyleTranslate(new Translate(new Length(10f, LengthUnit.Percent), new Length(50f, LengthUnit.Pixel)));
You can simplify the above code as follows using implicit conversions:
element.style.translate = new Translate(Length.Percent(10), 50);
IStyle.scale
IStyle.scaleThe property sets the scale.
IStyle.scaleIStyle.scaleStyleScaleStyleScaleScaleVector2Examples
element.style.scale = new StyleScale(new Scale(new Vector2(0.5f, -1f)));
You can simplify the code above as follows using implicit conversions:
element.style.scale = new Scale(new Vector2(0.5f, -1));
IStyle.rotate
IStyle.rotateThe property sets the rotation.
IStyle.rotateThe property is of type . The 's constructor takes a as an argument. You can construct this with an . You can construct an with a float and an optional enum, or you can use static methods , , , and .
IStyle.rotateStyleRotateStyleRotateRotateRotateAngleAngleAngleUnitDegrees()Gradians()Radians()Turns()Examples
//Rotate by 180 degreeselements[0].style.rotate = new StyleRotate(new Rotate(new Angle(180f, AngleUnit.Degree)));//Rotate by 200 gradianselements[1].style.rotate = new StyleRotate(new Rotate(new Angle(200f, AngleUnit.Gradian)));//Rotate by pi radianselements[2].style.rotate = new StyleRotate(new Rotate(new Angle(Mathf.PI, AngleUnit.Radian)));//Rotate by half a turnelements[3].style.rotate = new StyleRotate(new Rotate(new Angle(0.5f, AngleUnit.Turn)));
You can simplify the above code as follows:
//Rotate by 180 degreeselements[0].style.rotate = new Rotate(180);//Rotate by 200 gradianselements[1].style.rotate = new Rotate(Angle.Gradians(200));//Rotate by pi radianselements[2].style.rotate = new Rotate(Angle.Radians(Mathf.PI));//Rotate by half a turnelements[3].style.rotate = new Rotate(Angle.Turns(0.5f));
Additional resources
- Layout engine
- CSS transform-origin property (Mozilla developer documentation)
- CSS translate property (Mozilla developer documentation)
- CSS rotate property (Mozilla developer documentation)
- CSS scale property (Mozilla developer documentation)