Convert a Transform to a RectTransform
Solution 1
You can't cast Transform
to RectTransform
. That's what GetComponent
is use for. Use GetComponent
on the btnExit variable and you will be able to get the RectTransform
from the Button.
RectTransform myRectTransform = btnExit.GetComponent<RectTransform>();
Note:
You should only do this after calling SetParent
, since SetParent
will make Unity automatically attach RectTransform
to the btnExit
variable if the Parent Object is a UI Object such as Canvas. You can also use AddComponent
to attach RectTransform
to that GameObject.
If you don't do any of these, you will get null
since RectTransform
is not attached to the Button
.
Solution 2
RectTransform rectTransform = transform.GetComponent<RectTransform>();
RectTransform rectTransform = (transform as RectTransform);
RectTransform rectTransform = (RectTransform)transform;
As long as your object has a RectTransform, these are all valid ways to get the RectTransform from the built-in transform
reference.
Kaito Kid
Updated on June 13, 2022Comments
-
Kaito Kid almost 2 years
In my Unity project, I am creating objects dynamically through scripts.
var btnExit = new GameObject("Player " + ID + "'s Exit Button"); btnExit.transform.SetParent(UI.transform);
I need to set the anchors and pivot of the object. I should be able to do that using its RectTransform component, as I do it when I create objects in the scene editor.
myRectTransform.anchorMin = new Vector2(1, 0); myRectTransform.anchorMax = new Vector2(0, 1); myRectTransform.pivot = new Vector2(0.5f, 0.5f);
But the object's transform component is not a RectTransform, just the normal Transform. So I can't cast it to use those properties I need.
RectTransform myRectTransform = (RectTransform)btnExit.transform;
So how can I properly use the power of the RectTransform class on an object that I initialise through scripts instead of in the scene editor?
-
sh code over 5 years1. this should be the accepted answer. 2. but do you have some info on performance of GetComponent vs casting transform? From what I know, GetComponent is usually relatively slow, and I'd assume transform uses a shortcut (being the component each gameobject has to have), so I'd think casting transform could be faster? I should test it out, but I don't have the time now :/
-
nopunintendo over 5 yearsIt's my understanding that "transform" is a cached reference. I haven't dug into the performance costs of each option listed, but I think it's safe to assume that the cast will be faster. It definitely won't be slower in any case. I always cast instead of using GetComponent for that reason, but I have never confirmed that assumption.
-
Bip901 about 4 years@shcode I've just tested it. GetComponent is 7 times slower than casting or using
as
. Here are the results after calling both 10000 times:GetComponent: 0.4623ms
as: 0.065ms
-
Bas about 4 yearsWait, why is this marked as the correct answer? You -can- cast transform to RectTransform, if the gameobject indeed has a RectTransform component.
-
sh code about 4 years@Bip901 that is... VERY strange to me. both differences, getcomponent vs casting as well as "as" vs casting... the first one for the reason I already said, the second one because I thought "as" was just a syntactic sugar with "return null instead of throwing an exception", so I'd expect them to be either almost the same speed, or the "as" to be the (slightly) slower one... in any case, thank you for testing :)
-
Bip901 about 4 years@shcode I think you misread my comment. The results were exactly as you expected. Also, I didn't include the casting in my comment, but yeah, it's just a tad faster than
as
. -
sh code about 4 years@Bip901 oh wow, yes, i DID misread it. Several times over the course of two or three months before I decided to comment on it after all. Interesting. Embarrassing. Thank you for pointing it out =D