Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
0
Question by Kamil1064 · Jul 13, 2015 at 11:38 AM · c#uibuttondimensions

Get UI Button dimensions in game via script C#

Hi, I tried to find how to get dimensions of UI button but I cannot find how. We can check rect information but they are saying dimensions from unity editor not from game (devices have got different resolution so they are differrent). alt text

Coce i used to get it is: print ("rect = " + movementJoyStick.GetComponent>RectTransform>().rect); At the moment I'm using some math to finally get them but I'm there must be simple way to get those information. When I'm testing them in editor they are 370 but from print function they are always 500 in Update function.

unity-button-dimensions.jpg (297.0 kB)
Comment
Add comment · Show 1
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 Kamil1064 · Mar 18, 2016 at 06:55 PM 0
Share

Refreshing :(

1 Reply

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

Answer by flashframe · Mar 18, 2016 at 07:19 PM

Try multiplying the width/height by the ScaleFactor of your Canvas to get the adjusted size

http://docs.unity3d.com/460/Documentation/ScriptReference/Canvas-scaleFactor.html

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 Kamil1064 · Mar 18, 2016 at 08:19 PM 0
Share

Hi @flashframe, thanks for respond. However what i I'm using UI Scale $$anonymous$$ode: Scale With Screen Size. I actually need to know if my finger contains Rect of UI Image but they are always the same size in RectTransform so in script I get true value event if my finger is over that Rect (f.e. Rect(0,0,500,500) in editor but on resized screen in game it's Rect(0,0,370,370)) I tried also to use ray from camera but the same result, it's taking those values.

avatar image flashframe Kamil1064 · Mar 18, 2016 at 08:26 PM 1
Share

You might need to use RectTransformUtility.RectangleContainsScreenPoint

http://docs.unity3d.com/ScriptReference/RectTransformUtility.RectangleContainsScreenPoint.html

(ps. Scale Factor and Canvas Scaler are different things.)

avatar image Kamil1064 flashframe · Mar 18, 2016 at 08:49 PM 0
Share

Thanks, it's working now :D

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

22 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

Related Questions

Changing anchor positions on UI? 0 Answers

Help making multiple buttons do a specific set of actions in any order pressed 1 Answer

UIButton highlighted color not working 0 Answers

scrollview buttons are overlapping 0 Answers

Help check click out of UI 2 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