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
0
Question by shadowsaint669 · May 21, 2012 at 06:17 PM · guirect

GUI Rect and 0.000 floats

Anyone explain to me why this is making my GUI rect not work?

new Rect (0,0,(persistence.sheight / 1.6), persistence.sheight);

The goal is to set up my GUI so that no matter what device it loads on it has some basic screen ratio built in and will stretch the rects to look decent size. The issues seems to be that my (persistence.sheight / 1.6) formula returns a double in the form of a ###.### value that goes out to the 3rd decimal position.

Comment
Add comment · Show 2
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 FLASHDENMARK · May 21, 2012 at 06:23 PM 0
Share

What is sheight? Is it a variable or a typo?

avatar image shadowsaint669 · May 21, 2012 at 06:24 PM 0
Share

in my persistence which is a data pass between scenes I push screen.height and screen.width to sheight and swidth floats.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · May 21, 2012 at 06:32 PM

I guess you use C# (guess from the new keyword). You should devide by a float value to get a float value:

 new Rect (0,0,(persistence.sheight / 1.6f), persistence.sheight);

However magic numbers inside the code should be avoided. Additionally multiplications are a little bit faster than divisions so i would predefine your factor like this:

 // C#
 float aspect = 1 / 1.6f;
 
 // [...]
 new Rect (0,0,(persistence.sheight * aspect), persistence.sheight);
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 Eric5h5 · May 22, 2012 at 12:56 AM 0
Share

I'm not sure that thing about division is true anymore on more recent CPUs. I've run benchmarks on my Xeon in Unity and + - / * are all apparently the same speed.

avatar image
0

Answer by whydoidoit · May 21, 2012 at 06:28 PM

You need to cast the double return value to a float to use with the rect:

 new Rect(0,0,(float)(persistence.sheight/1.6), (float)persistence.sheight);
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 shadowsaint669 · May 21, 2012 at 06:30 PM 0
Share

this worked thanks

avatar image whydoidoit · May 22, 2012 at 12:31 AM 0
Share

Can you mark it as answered? I can always use the $$anonymous$$arma ;) And it takes it off the list...

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

7 People are following this question.

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

Related Questions

How do I set a new Vector 2 for a GUI.Utility RotateAroundPivot? 0 Answers

Setting a Rect from another RectTransform 0 Answers

GUI label dimensions bug 2 Answers

Need help understanding GUI and Rect.Contains interaction 0 Answers

strange Rect behavior on Y axis 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