Position of rect Transform in unity UI
Asked Answered
B

2

9

I found a very confusing issue in programming with Unity's UI rectTransform:

in the inspector, we can find that the RectTransform's position was set to (0,0,0) enter image description here

but in the code in Start(), when I print the position:

parentObject = transform.parent.gameObject;
Debug.Log("parentObject rectTransform :" + parentObject.GetComponent<RectTransform>().localPosition);

This will give me a position of:

parentObject rectTransform :Panel: (0.0, -562.5, 0.0)
UnityEngine.Debug:Log(Object)
NewWorkerController:Start() (at Assets/Scripts/NewWorkerController.cs:60)

Things will be even more confusing when we try to get the position instead of the localPosition:

parentObject rectTransform :Panel: (621.0, 1365.5, 0.0)
UnityEngine.Debug:Log(Object)
NewWorkerController:Start() (at Assets/Scripts/NewWorkerController.cs:60)

I have also checked the posX and posY of this rectTransform during playMode which indict that it has not been changed(kept showing (0,0,0))

What's wrong with that?

Berey answered 2/5, 2017 at 10:52 Comment(4)
The script is on which GameObject?Idle
@Smartis script was attached on newWorkPlace which is the child of Panel in the HierarchyBerey
@amnotstrong Use .GetComponent<RectTransform>().position instead of .GetComponent<RectTransform>().localPositionIdle
@Smartis using position will get an even more wired result.Berey
H
6

What you see in the RectTransform component is not the local position. The manual says :

Positioning the UI element

A UI Element is normally positioned using its Rect Transform. If the UI Element is a child of a Layout Group it will be automatically positioned and the positioning step can be skipped.

When positioning a Rect Transform it’s useful to first determine it has or should have any stretching behavior or not. Stretching behavior happens when the anchorMin and anchorMax properties are not identical.

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.

For a stretching Rect Transform, it can be simpler to set the position using the offsetMin and offsetMax properties. The offsetMin property specifies the corner of the lower left corner of the rect relative to the lower left anchor. The offsetMax property specifies the corner of the upper right corner of the rect relative to the upper right

Source : https://docs.unity3d.com/Manual/HOWTO-UICreateFromScripting.html

In your case, you have a non-stretching Rect Transform. Thus, try to print the anchored position :

parentObject = transform.parent.gameObject;
Debug.Log("parentObject rectTransform :" + parentObject.GetComponent<RectTransform>().anchoredPosition);
Hives answered 2/5, 2017 at 10:59 Comment(0)
B
2

One way to be less confused and better understand how changing the RectTransform affects values seen in script. (e.g. how changes in scene / inspector settings are reflected in script member variables)

  1. Create separate inspector property window: click the 3 dots in the RectTransform and select "Properties..."
  2. Turn on debug mode: in the new window that should have popped up... click the highest level 3 dots for the whole window and select "Debug" You can now play with the object that has the RectTransform and see how the member variables in the debug window (which are the same variables accessible in script) show your changes.
Biodynamics answered 30/11, 2021 at 16:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.