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 /
This question was closed May 23, 2014 at 01:08 AM by Lo0NuhtiK for the following reason:

Other

avatar image
0
Question by hkessock · May 22, 2014 at 09:13 PM · resolutionaspect ratio

Anyone looking to do resolution and aspect ratio independent GUI work

I recently had to start supporting Android devices in a product I'd built initial for iOS.

This was a scary proposition as there was a lot of GUI UI code that was pretty carefully laid out.

I'd partially implemented a solution before because I needed to support various versions of the iPad (which have various resolutions) and I wanted to design for the highest resolution (iPad Air) rather than stretch items from the lowest to the highest.

Anyhow, I found myself playing with GUI.matrix and solved my iOS needs in that fashion by modifying the Scale vector and leaving transform and rotation alone.

Now that I'm running on Android too, I found that I could simply extend my GUI matrix modifying code so that it was both resolution and aspect ratio independent.

I had to add two new things: Adjust the scale vector to account for scenarios when the actual aspect ratio was either greater or smaller than the 'target' aspect ratio. Add a translation component to the GUI.matrix so that the center of the screen was still the actual center of the screen.

I have been beating on it a bit this afternoon and so far it seems to solve the job for me (as long as I'm okay with non 4:3 aspect ratios have bars on the side - which I hide with camera color anyhow.)

It doesn't currently help with fullscreen textures (unless you can set the camera color to the base color of the texture) because you'll get vertical bars, but I'm not sure if that can be overcome and remain aspect ratio neutral...

Hopefully someone finds this useful.

 //2D UI Scaling Related (treat all GUI code like it is running on iPad 4)
 public const int m_nGUITargetResolutionWidth = 2048;
 public const int m_nGUITargetResolutionHeight = 1536;
 static private float m_nGUITranslationOffset = 0;
 
 //Call some time before OnGUI in your classes that use OnGUI
 public static void ScaleGUIMatrixToResolution()
 {
     float l_nScreenWidth = (float)Screen.width;
     float l_nScreenHeight = (float)Screen.height;
     float l_nAspectRatio = l_nScreenWidth / l_nScreenHeight;
     float l_nTargetAspectRatio = (float)m_nGUITargetResolutionWidth / (float)m_nGUITargetResolutionHeight;
 
     //Remember not to directly compare two floats
     if( true == Mathf.Approximately( l_nAspectRatio, l_nTargetAspectRatio ) )
     {
         //Pretty much the same aspect ratio, so scale simply and leave translation alone
         Vector3 l_oScale = new Vector3( ( l_nScreenWidth / (float)m_nGUITargetResolutionWidth ), ( l_nScreenHeight / (float)m_nGUITargetResolutionHeight ), 1.0f );
         GUI.matrix = Matrix4x4.TRS( Vector3.zero, Quaternion.identity, l_oScale );
     }
     else
     {
         Vector3 l_oTranslate;
         Vector3 l_oScale;
 
         //Different aspect ratio, scale to fit the target region within the screen, compute translation to move the center of the GUI back to the actual screen center
         if( l_nAspectRatio > l_nTargetAspectRatio )
         {
             l_oScale = new Vector3( ( l_nScreenWidth / (float)m_nGUITargetResolutionWidth ) * ( l_nTargetAspectRatio / l_nAspectRatio ), ( l_nScreenHeight / (float)m_nGUITargetResolutionHeight ), 1.0f );
             m_nGUITranslationOffset = l_nScreenWidth - ( l_nScreenWidth * ( l_nTargetAspectRatio / l_nAspectRatio ) );
             l_oTranslate = new Vector3( m_nGUITranslationOffset / 2.0f, 0.0f, 0.0f );
         }
         else
         {
             l_oScale = new Vector3( ( l_nScreenWidth / (float)m_nGUITargetResolutionWidth ), ( l_nScreenHeight / (float)m_nGUITargetResolutionHeight ) * ( l_nAspectRatio / l_nTargetAspectRatio ), 1.0f );
             m_nGUITranslationOffset = l_nScreenHeight - ( l_nScreenHeight * ( l_nAspectRatio / l_nTargetAspectRatio ) );
             l_oTranslate = new Vector3( 0.0f, m_nGUITranslationOffset / 2.0f, 0.0f );
         }
 
         GUI.matrix = Matrix4x4.TRS( l_oTranslate, Quaternion.identity, l_oScale );
     }        
 }
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 ozone · May 23, 2014 at 12:09 AM 0
Share

This does not appear to be a question. Unless you are seeking assistance, please relocate this to the forums.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

21 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

Related Questions

Developing on multiple resolutions 1 Answer

Forcing an resolution for all android and iphone screens 1 Answer

Game Window Resolution Accurate? 1 Answer

Unity 2D: Pixel Perfect Camera + Resolution Help 0 Answers

Is this the correct way to scale my GUI? 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