- Home /
How do I get the literal width of a RectTransform?
I'm trying to use the the width of a UI object to define the size of future UI objects. I have read other questions which have given the answer RectTransform.rect.width. This returns a negative value in my case (because of anchoring). Is there a function to get the real / literal width of the object I am looking at or does it need to be calculated based on the anchor positions?
If you mean the width of object without getting anchors stretching into count then this is what you're looking for:
someObjectRectTransform.sizeDelta.x
This returns the same value - which is also negative.
Then your UI object width is actually -something and your object should be invisible then. Your $$anonymous$$ achors should be $$anonymous$$in and $$anonymous$$ax should be $$anonymous$$ax not vice versa.
P.S. Though if you want that object to be invisible but get positive width then use Absolute value
With this test script I get the following output:
void Start () {
RectTransform t = gameObject.GetComponent<RectTransform>();
Debug.Log(gameObject.name + " width " + t.rect.width + " delta x " + t.sizeDelta.x);
}
ObjectName width -27.86328 delta x -27.86328
I suspect it is invisible during the period where Start() executes and then gets rescaled at some point after in the objects initialization.
Answer by Elum224 · Apr 14, 2015 at 09:15 AM
My solution is to use a coroutine to wait until the next frame for when the objects have finished resizing themselves and use RectTransform.sizeDelta.x or RectTransform.rect.width according to need.
public IEnumerator SetGridWidth() {
yield return new WaitForEndOfFrame();
GridWidth = transform.GetComponent<RectTransform> ().sizeDelta.x;
}
That helped a ton! Thanks! That was so not obvious. I would have no idea what is happening if not your explanation
Answer by DiegoSLTS · Apr 13, 2015 at 06:21 PM
http://docs.unity3d.com/ScriptReference/RectTransform-rect.html
aRectTransformReference.rect.width
I'm using a Canvas with Render $$anonymous$$ode: Screen Space - Camera and this works perfectly for me
Answer by desman · Jan 25, 2016 at 07:54 AM
rect.width only works if RectTransform.anchorMin.x and RectTransform.anchorMax.x are the same (overlapping). If they are different (i.e., if the object stretches on X when its parent changes width) then rect.width will contain the width of the object relative to its parent width. Since the object is typically smaller than its parent, this value will be negative.
You could try getting the bounds of the game object:
Renderer renderer = obj.GetComponent<Renderer>();
Bounds bounds = renderer.bounds;
float width = bounds.size.x;
but for a UI Panel object, the GetComponent() call returns null, even if the panel has an Image component in it and that image component is enabled.
Alternately, you could try calling RectTransform.GetWorldCorners(). You pass it an array of four Vector3s and you get back the world coordinates of the four corners. You can then subtract X coords to get the width.
Be aware that this stuff only works if the object is in the scene or if the object does not stretch. If your script is attached to a stretchable object that gets dynamically instantiated at run time, you can't get its dimensions in Start() because it doesn't have a parent object yet and consequently hasn't been sized to fit anything (you'll just get the original width or height of the prefab).
Answer by strider2k · May 07, 2015 at 06:26 AM
The width and height of a RectTransform can be <= 0 if stretching by width or height is set in the Inspector. The rect and sizeDelta will return values such as: rect: (x:0.00, y:36.00, width:0.00, height:-72.00) sizeDelta: (0.0, -72.0)
even after Canvas.ForceUpdateCanvases() and calling a coroutine.
Answer by fermmmm · May 12, 2016 at 07:56 AM
Size is always relative to anchors does not matter if it comes from RectTransform.rect.width or RectTransform.SizeDelta. Try the "Rect Transform Extended" asset. You can get the size relative to the parent size (ignoring anchors) and other stuff that should come with Unity:
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Get width and height of Image in Unity 4.6 1 Answer
Is it possible to get the width (in unity units) of a GameObject including all of its children? 1 Answer
[UNSOLVED] Get a GameObject width and height in pixels (JS) 3 Answers
Object width in treadmill script 0 Answers