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 Bluestrike · Jun 20, 2014 at 02:25 PM · guimatrix4x4.trs

GUI matrix Center on screen on different aspect ratio

How can I center the matrix on my screen?

alt text

Above image is created with below code and represents the screen in 16/9 aspect ratio. The box field is my interface area created in a 16/10 aspect ratio, so I need it to center in the other resolutions.

 public var gSkin :GUISkin ;
 
 // Resolution in wich the interface is designed.
 var nativeWidth        :float = 1680;
 var nativeHeight     :float = 1050;
 
 // I set these in a awake function based on desktop resolution
 var screenWidth        :float;
 var screenHeight    :float;
 
 function Awake()
 {
     screenHeight    = Screen.height;
     screenWidth        = Screen.width;
 }
 
 function OnGUI()
 {
     var hScale :float = screenHeight / nativeHeight;
     GUI.matrix = Matrix4x4.TRS (Vector3(0, 0, 0), Quaternion.identity, Vector3 (hScale, hScale, 1));
     
     GUI.skin = gSkin ;
     // Testing position
     GUI.Box(Rect(0,0,nativeWidth,nativeHeight), "box");
 }


How can I have the box/GUI matrix centered? I tried this with no luck:

 function OnGUI()
 {
     var hScale :float = screenHeight / nativeHeight;
     var wStart :float = (screenWidth - nativeWidth) /2;
     GUI.matrix = Matrix4x4.TRS (Vector3(wStart, 0, 0), Quaternion.identity, Vector3 (hScale, hScale, 1));
     
     GUI.skin = gSkin ;
     // Testing position
     GUI.Box(Rect(0,0,nativeWidth,nativeHeight), "box");
 }

And another attempt at the wStart value without the expected result:

 var wStart :float = (screenWidth - (nativeWidth/hScale)) /2;

gui.jpg (49.7 kB)
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
0
Best Answer

Answer by Bluestrike · Jun 21, 2014 at 07:36 AM

As usual, when giving up and seeking help I suddenly find the solution myself ;) When calculating the start position I should not divide by scale but multiply.

For people wou can use it, this scales the GUI based on the vertical hight of the screen, keeps the aspect ratio and centers it over the width of the screen. Native Width and Native Height is the resolution used when designing the GUI.

 public var gSkin :GUISkin ;
 
 // Resolution in wich the interface is designed.
 var nativeWidth        :float = 1680;
 var nativeHeight     :float = 1050;
 var screenWidth        :float;
 var screenHeight    :float;
 
 function Awake()
 {
     screenHeight    = Screen.height;
     screenWidth        = Screen.width;
 }
 
 
 function OnGUI()
 {
     var hScale :float = screenHeight / nativeHeight;
     var wStart :float = (screenWidth - (nativeWidth * hScale)) /2;
     GUI.matrix = Matrix4x4.TRS (Vector3(wStart, 0, 0), Quaternion.identity, Vector3 (hScale, hScale, 1));
     
     GUI.skin = gSkin ;
     // Testing position
     GUI.Box(Rect(0,0,nativeWidth,nativeHeight), "box");

     // Create the GUI based on the native values
 }
 
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
0

Answer by Vunpac · Jun 20, 2014 at 02:37 PM

to center anything on any resolution you would typically go (I'm not really familiar with js so I will do it in c#)

 // get the center of the screen then move half way up the size of the box
 float centerHeight = Screen.Height * 0.5f - nativeHeight * 0.5f
 
 //repeat for center width
 float centerWidth = Screen.Width * 0.5f - nativeWidth * 0.5f

by doing this you are taking the exact center position of the screen then subtracting half of the box from it, that will put the box exactly in the center

Also multiplying by 0.5f is the exact same as dividing by 2 but computers can multiply faster than they can divide

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

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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Setting Scroll View Width GUILayout 1 Answer

destroy gui after game paused 3 Answers

Not working gui buttons 1 Answer

Need help displaying score (JavaScript) 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