The transform properties apply a 2D transformation to a visual elementA node of a visual tree that instantiates or derives from the C# VisualElement
class. You can style the look, define the behaviour, and display it on screen as part of the UI. More info
See in Glossary. 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 top
and left
, and then use translate
to align an offset relative to the initial position.
Transform includes the following properties:
Property | USS syntax | Description |
---|---|---|
Transform Origin | transform-origin |
Represents the point of origin where rotation, scaling, and translation occur. |
Translate | translate |
Repositions the visual element in horizontal or vertical directions. |
Scale | scale |
Changes the apparent size, padding, border, and margins of a visual element. Negative values flip visual elements along the scale axis. |
Rotate | rotate |
Rotates a visual element. Positive values represent clockwise rotation and negative values represent counterclockwise rotation. You can set rotation with deg , grad , rad , or turn units. For more information on these units, see MDN Web Docs’s page on the <angle> CSS data type. |
Note: All transformations are performed in the following order:
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.
You can use the controls in the Transform section of the InspectorA Unity window that displays information about the currently selected GameObject, asset or project settings, allowing you to inspect and edit the values. More info
See in Glossary in the UI(User Interface) Allows a user to interact with your application. Unity currently supports three UI systems. More info
See in Glossary Builder to set the transform properties of a visual element.
The Pivot Origin widget sets the transform origin property. To use it, do one of the following:
Tip: You can enter %
or px
after values. This automatically changes the displayed unit in the unit selector. You can also drag to define the values in the X and Y boxes.
Note: The default value for the transform origin is center.
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.
The Translate control sets the translate property. To use it, enter values in the X and Y boxes and specify the unit.
Tip: You can enter %
or px
after values. This automatically changes the displayed unit in the unit selector. You can also drag to define the values in the X and Y boxes.
The Scale control sets the scale property. To use it, enter values in the X and Y boxes and specify the unit.
Tip: You can enter %
or px
after values. This automatically changes the displayed unit in the unit selector. You can also drag to define the values in the X and Y boxes.
The Rotate control sets the rotate property. To use it, enter a value and specify the unit.
Tip: You can type deg
, grad
, rad
, or turn
after a value in the Rotate box. This automatically changes the displayed unit in the unit selector.
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
The transform-origin
property sets the transform origin along the X and Y axes in pixelsThe smallest unit in a computer image. Pixel size depends on your screen resolution. Pixel lighting is calculated at every screen pixel. More info
See in Glossary or percentages.
You can also use keywords to set the value of the transform-origin
attribute. These keywords match the dots in the widget in the UI Builder. The following keywords are supported:
Pivot 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
The translate
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.
Examples
translate: 80%; translate: 35px; translate: 5% 10px; translate: 24px 0%;
scale
The scale
property sets the scale along the X and Y axes in pixels or percentages. You can omit Y if it equals X.
The keyword none
sets no scale.
Examples
scale: 2.5; scale: -1 1; scale: none;
rotate
The rotate
property sets the rotation using a number or a unit.
The keyword none
sets no rotation.
Examples
rotate: 45deg; rotate: -100grad; rotate: -3.14rad; rotate: 0.75turn; rotate: none;
You can set the transform properties for a visual element in a C# script.
IStyle.transformOrigin
The IStyle.transformOrigin
property sets the transform origin.
The transformOrigin
property of the style
is of type StyleTransformOrigin
. Its constructor takes a TransformOrigin
as an argument. You can construct a new TransformOrigin
using an X value and a Y value. The X value and the Y value are of type Length
.
Examples
//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
The IStyle.translate
property sets the translation.
IStyle.translate
is of type StyleTranslate
. Its constructor takes a Translate
as an argument. You can construct a new Translate
using an X value and a Y value. The X value and the Y value are of type Length
.
Examples
//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
The IStyle.scale
property sets the scale.
IStyle.scale
is of type StyleScale
. StyleScale
’s constructor takes a Scale as an argument. You can construct this Scale
with a Vector2
.
Examples
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
The IStyle.rotate
property sets the rotation.
The IStyle.rotate
property is of type StyleRotate
. The StyleRotate
’s constructor takes a Rotate
as an argument. You can construct this Rotate
with an Angle
. You can construct an Angle
with a float and an optional AngleUnit
enum, or you can use static methods Degrees()
, Gradians()
, Radians()
, and Turns()
.
Examples
//Rotate by 180 degrees elements[0].style.rotate = new StyleRotate(new Rotate(new Angle(180f, AngleUnit.Degree))); //Rotate by 200 gradians elements[1].style.rotate = new StyleRotate(new Rotate(new Angle(200f, AngleUnit.Gradian))); //Rotate by pi radians elements[2].style.rotate = new StyleRotate(new Rotate(new Angle(Mathf.PI, AngleUnit.Radian))); //Rotate by half a turn elements[3].style.rotate = new StyleRotate(new Rotate(new Angle(0.5f, AngleUnit.Turn)));
You can simplify the above code as follows:
//Rotate by 180 degrees elements[0].style.rotate = new Rotate(180); //Rotate by 200 gradians elements[1].style.rotate = new Rotate(Angle.Gradians(200)); //Rotate by pi radians elements[2].style.rotate = new Rotate(Angle.Radians(Mathf.PI)); //Rotate by half a turn elements[3].style.rotate = new Rotate(Angle.Turns(0.5f));
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.
When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.
More information
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising. Some 3rd party video providers do not allow video views without targeting cookies. If you are experiencing difficulty viewing a video, you will need to set your cookie preferences for targeting to yes if you wish to view videos from these providers. Unity does not control this.
These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.