Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
4
Question by patrik-org · May 22, 2015 at 07:28 PM · uiunity 4.6unity4.6

Reliable way to get width/height of UI element?

I'm creating UI elements that need to know their width and height in order to arrange their children correctly. I've been using the following to calculate the width and height of the element:

         var rectTransform = GetComponent<RectTransform>();
         float width = rectTransform.rect.width;
         float height = rectTransform.rect.height;

It works in many cases but there are some cases when the current element and its parent is stretched that rectTransform.rect.width returns a negative value. The rectTransform.rect.sizeDelta.x/y is also zero in these cases.

I wonder if there is a reliable way to calculate the current size of a UI element regardless how it is anchored?

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
7

Answer by ondere · Apr 12, 2018 at 11:01 PM

its sizeDelta

 var rectTransform = GetComponent<RectTransform>();
 float width = rectTransform.sizeDelta.x;
 float height = rectTransform.sizeDelta.y;
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 Sukonny · Sep 23, 2018 at 09:22 PM 0
Share

Help for me, Thanks!

avatar image marcin-matczak · Jan 28, 2020 at 03:52 PM 7
Share

This is not true. With stretched anchors and bottom/top/left/right offsets set to 0 sizeDelta of RectTransform is (0,0) even though the actual size of rect should be size of its parent. Using size.Delta is most unreliable answer possible.

avatar image
0

Answer by DoTA_KAMIKADzE · May 22, 2015 at 08:55 PM

Negative rect value means that your UI is not visible, because...well because its width/height is negative, pretty obvious? But if you want to get only absolute value for some calculations then use absolute value (Abs).

Also the difference between sizeDelta and rect is well described THERE.

Comment
Add comment · Show 3 · 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 patrik-org · May 22, 2015 at 09:27 PM 0
Share

The UI element is visible and renders as expected on the screen.

avatar image DoTA_KAMIKADzE · May 23, 2015 at 02:41 PM 0
Share

Without the code and screens of your UI setup/hierarchy I can't tell you why your UI element reports negative width even though that it has positive. If you want me to find that out then I'll need all of those.

avatar image monfeaz · Oct 10, 2015 at 12:49 AM 0
Share

I'm also interested in this problem. I am stretching a panel vertically and I'm trying to return its height somehow. The thing is that stretched elements in rect transform have no height.They are measured with distance to the anchor, so you can't assign or natively return the height because there is none.

Is it possible to measure the height in a more non-conventional way?

Thank you

avatar image
-1

Answer by skrad · Nov 13, 2015 at 09:53 PM

I have just found an ugly solution like this.

In the inspector navigate to your stretched Rect Transform and hard code the left, top, right, bottom values somewhere in your code.

Then simple do this:

float realWidth = (float) (Screen.width - hard_Code_Left - hard_Code_Right);

float realHeight = (float) (Screen.height - hard_Code_Top - hard_Code_Bottom)

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 demozbox · Jan 08, 2016 at 11:50 PM 0
Share

what means your hard_Code_Left, hard_Code_Right, hard_Code_Top, hard_Code_Bottom?

avatar image skrad demozbox · Jan 10, 2016 at 06:33 PM 0
Share

Hello!

Those are the Number values you need to check in your actual RectTransform using the inspector, and put those in variables.

for example: float hard_Code_Left = 115f;

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

Dual Stick Touch Shooter 0 Answers

Simple UI not appearing once deployed to iOS (4.6.2) 1 Answer

Unity 4.6 Text Misbehaves During Gameplay 1 Answer

Unity 4.6 GUI select button via script 1 Answer

Is there a way to move a UI mask without moving child content? 3 Answers


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