Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a Transform to a RectTransform

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?

like image 575
Kaito Kid Avatar asked Jun 12 '17 22:06

Kaito Kid


People also ask

How do I change rect transform to unity?

Go to the RectTransform component and delete it. When you do, it reverts to a Transform.

What is RectTransform?

The Rect Transform component is the 2D layout counterpart of the Transform component. Where Transform represents a single point, Rect Transform represent a rectangle that a UI element can be placed inside.

How do I change the rect transform position in unity?

For a non-stretching Rect Transform, the position is set most easily by setting the anchoredPosition and the sizeDelta properties. The anchoredPosition specifies the position of the pivot relative to the anchors. The sizeDelta is just the same as the size when there's no stretching. Save this answer.

What is anchored position unity?

The Anchored Position is the position of the pivot of the RectTransform taking into consideration the anchor reference point. The anchor reference point is the position of the anchors. If the anchors are not together, Unity estimates the four anchor positions using the pivot placement as a reference.


2 Answers

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.

like image 128
nopunintendo Avatar answered Nov 14 '22 21:11

nopunintendo


You can directly add a RectTransform component to the gameobject, and the Transform component will change to Rect Transform.

For example:

var btnExit = new GameObject("Player " + ID + "'s Exit Button");
btnExit.AddComponent<RectTransform>();

Then the cast will work:

RectTransform myRectTransform = (RectTransform)btnExit.transform;

P.S.

Without adding the component, the above cast will throw error

InvalidCastException: Cannot cast from source type to destination type.

But with the component added, the cast works fine. Above code is tested in Unity 2018.2.5f1 (64bit).

like image 37
Hustlion Avatar answered Nov 14 '22 21:11

Hustlion