Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
8
Question by Elum224 · Apr 13, 2015 at 05:16 PM · gameobjectwidth

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?

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DoTA_KAMIKADzE · Apr 13, 2015 at 05:44 PM 0
Share

If you mean the width of object without getting anchors stretching into count then this is what you're looking for:

 someObjectRectTransform.sizeDelta.x
avatar image Elum224 DoTA_KAMIKADzE · Apr 13, 2015 at 05:48 PM 0
Share

This returns the same value - which is also negative.

avatar image DoTA_KAMIKADzE DoTA_KAMIKADzE · Apr 13, 2015 at 06:01 PM 0
Share

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

avatar image Elum224 DoTA_KAMIKADzE · Apr 14, 2015 at 09:08 AM 0
Share

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.

6 Replies

· Add your reply
  • Sort: 
avatar image
16
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dappledore · Oct 22, 2016 at 10:52 PM 0
Share
 public IEnumerator SetGridWidth() {
         yield return new WaitForEndOfFrame();
         GridWidth = transform.GetComponent<RectTransform> ().sizeDelta.x;
     }
avatar image Reaper93 · Mar 15, 2019 at 11:50 AM 0
Share

That helped a ton! Thanks! That was so not obvious. I would have no idea what is happening if not your explanation

avatar image
17

Answer by DiegoSLTS · Apr 13, 2015 at 06:21 PM

http://docs.unity3d.com/ScriptReference/RectTransform-rect.html

 aRectTransformReference.rect.width
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BryanTaiNexusMedia · Mar 20, 2019 at 06:15 PM 0
Share

I'm using a Canvas with Render $$anonymous$$ode: Screen Space - Camera and this works perfectly for me

avatar image
6

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).

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
3

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
-4

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:

https://www.assetstore.unity3d.com/#!/content/41927

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image meat5000 ♦ · May 12, 2016 at 07:59 AM 0
Share

No more Adverts please @fermmmm

  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

GameObject width in screen space 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges