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
2
Question by darkhog · Jan 25, 2015 at 05:29 PM · uiheight

[4.6 gui] Getting dimensions of 2D UI object (button for example)

I need to create few buttons in code and make them a neat grid. But I have no idea how I can get width or height of a button once I spawn them so it'll be a neat grid. I'm using new GUI for the job and don't know how to place them.

Here's code I have so far:

 void Start () {
         GameObject tempObject;
         for (int i =0;i<maxCategoryNum;i++){
             tempObject = Instantiate(CategoryButton) as GameObject;
             tempObject.GetComponent<RectTransform>().parent = this;
             tempObject.GetComponent<RectTransform>().position.x=0;
             tempObject.GetComponent<RectTransform>().position.y=i*tempObject.GetComponent<RectTransform>().; //here should be height of the object
         }
     }

Unfortunately I have no idea how to get height of the object to place them correctly.

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

2 Replies

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

Answer by jenci1990 · Jan 25, 2015 at 05:48 PM

Try it:

 GetComponent<RectTransform>().rect.height;
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 desman · Jan 25, 2016 at 07:01 AM 0
Share

This solution only works if the anchors for the object in question overlap on Y (i.e., anchor$$anonymous$$in.y and anchor$$anonymous$$ax.y hold the same value). If anchor$$anonymous$$in.y and anchor$$anonymous$$ax.y are set to stretch the object vertically, reading rect.height is likely going to give you not only a wrong value but a negative value at that (it will hold something like the height of the object relative to its parent's height).

If anchor$$anonymous$$in and anchor$$anonymous$$ax are identical, then rect.width and rect.height will work. If they're not identical, then I don't know how to compute the width and height. I've been trying to get a simple answer to that for the last few hours. Searching for "Unity get object width" should help but everybody makes the same assumption that the anchors overlap.

avatar image nilsubimax desman · Oct 18, 2018 at 08:44 AM 0
Share

Thanks desman, was wondering about that as well. This should not be the accepted answer :/

avatar image
6

Answer by Teravisor · Jan 25, 2016 at 10:44 AM

The only solution I've found so far that includes both anchors and offsets in all cases is

Vector3[] corners = new Vector3[4];
rectTransform.GetWorldCorners(corners);
And then manipulate their values to find what you need. For height it will be float height = corners[2].y-corners[0].y;

If someone knows better solution that works in any case, that would be great.

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 daniazram · Mar 10, 2016 at 12:55 PM 0
Share

I might be late but your answer helped me alot, I think your reply should be marked as correct answer.

avatar image ilozen · May 29, 2018 at 01:13 PM 0
Share

Didn't work for me - for an element that takes roughly 1/5th of the screen the value got calculated as 4.43235 pixels...

avatar image nilsubimax ilozen · Oct 18, 2018 at 11:06 AM 0
Share

GetWorldCorner returns world coordinates. I'd guess your element was about 4.5 meters/units big. This is not returning pixels as far as I've found.

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

25 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

Related Questions

uGUI: Button that overlaps text 1 Answer

[4.6 UI] Buttons spawn from the bottom of the panel 1 Answer

[4.6 GUI] Resizing panel to fit it content 2 Answers

uGUI change depending on screen aspect ratio 0 Answers

Unity 4.6 New UI Button OnHoldPressed 0 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