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
1
Question by olliej777 · Aug 24, 2012 at 03:22 PM · guiscalescreenmatrix

GUI - Scale GUIs According to Resolution

Hello,

I have a few GUIs in my game, and I'm trying to get it that the GUIs either scale up or scale down in size depending on my screen resolution.

By default, my GUIs stay the same size no matter what size my resolution is. I want to be able to scale them. So right now, I'm just starting with a simple texture that will scale:

 function OnGUI(){
 
    GUI.DrawTexture(Rect(100, 100, 100, 100), GuiTexture);
 
 }

I believe I want to be using a 'GUI.matrix', but the documentation gives no, to little explanation on how to use it.

Also, the screen ratio that I am supporting is only 16:10, any ideas?

Thanks

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

6 Replies

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

Answer by Lemo Dev · Aug 24, 2012 at 04:46 PM

Try this

C#

 void OnGUI()
 {
     GUI.DrawTexture(ResizeGUI(Rect(100, 100, 100, 100)), GuiTexture);
 }
 
 Rect ResizeGUI(Rect _rect)
 {
     float FilScreenWidth = _rect.width / 800;
     float rectWidth = FilScreenWidth * Screen.width;
     float FilScreenHeight = _rect.height / 600;
     float rectHeight = FilScreenHeight * Screen.height;
     float rectX = (_rect.x / 800) * Screen.width;
     float rectY = (_rect.y / 600) * Screen.height;
     
     return new Rect(rectX,rectY,rectWidth,rectHeight);
 }



Java

 var GuiTexture : Texture2D;
 
 function OnGUI()
 {
     GUI.DrawTexture(ResizeGUI(Rect(100, 100, 100, 100)), GuiTexture);
 }    
 
 function ResizeGUI(_rect : Rect) : Rect
 {
     var FilScreenWidth = _rect.width / 800;
     var rectWidth = FilScreenWidth * Screen.width;
     var FilScreenHeight = _rect.height / 600;
     var rectHeight = FilScreenHeight * Screen.height;
     var rectX = (_rect.x / 800) * Screen.width;
     var rectY = (_rect.y / 600) * Screen.height;
 
     return Rect(rectX,rectY,rectWidth,rectHeight);
 }
Comment
Add comment · Show 11 · 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 olliej777 · Aug 24, 2012 at 04:48 PM 0
Share

Sorry, I don't know C++, is it possible to do one in JS too? Thanks!

avatar image macrian olliej777 · Jul 28, 2016 at 12:05 PM 0
Share

It's not c++ it's C#

avatar image Lemo Dev · Aug 24, 2012 at 05:26 PM 0
Share

I have added java now

avatar image olliej777 · Aug 24, 2012 at 05:48 PM 0
Share

This works great, is it possible to apply this to a group? Ins$$anonymous$$d of having to redo all my GUI classes?

avatar image Lemo Dev · Aug 24, 2012 at 05:54 PM 0
Share

No you need to redo it every time

avatar image olliej777 · Aug 24, 2012 at 05:59 PM 0
Share

Okay, I've began doing that, but now my GUIs begin the lose their centering, so if the screen is small, the GUIs move the the upper left, and if its large, it moves to the lower right.

Show more comments
avatar image
0

Answer by mabit · Jan 19, 2013 at 12:57 PM

I know this question is quite old but I have been making a few games for Android and I used to use the answer given in your best answer.

IMHO using GUITextures with no Pixel Inset Values and using the Transform Position and Scale in the inspector has been the best way. Everything then auto scales much easier and the CPU Cycles saved Managed to get me an extra 10 FPS on low spec devices.

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 OperationDogBird · Aug 24, 2012 at 04:18 PM

Rather than using set values you should base the size and position based on the Screen.width and Screen.height.

 function OnGUI(){
 
    GUI.DrawTexture(Rect(Screen.width/3,Screen.width/3, Screen.width/10, Screen.height/10), GuiTexture);
 
 }

This will be the same relative size no matter what the end resolution is. I normally store the Screen.width and Screen.height values in variables for short, quicker reference.

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 Caliber Mengsk · Aug 24, 2012 at 07:34 PM

Should state that when using lots of GUI objects, calling screen.width and screen.height a lot can slow things down considerably. It would be best to set them to variables and call those instead. It's even more important on things like mobile devices with slower processors.

You can also use a basic scaling system. Make your GUI with the optimal size, then multiply by a scale. Like this:

 var optimalHeight:int = 1024; //just an example
 var optimalWidth:int = 768;
 var screen:Vector2;
 
 private var scale:Vector2;
 
 function Start()
 {
     screen = new Vector2(Screen.Width, Screen.Height); //width is x, height is y
     scale = new Vector2(screen.x/optimalWidth, screen.y/optimalHeight);
             //NOTE: We used small s on screen to not call the screen command again.
 }
 
 function OnGUI()
 {
    GUI.DrawTexture(
             Rect(
                 100*scale.x,//left
                 100*scale.y,//top
                 (screen.x - 200) *scale.x,//width
                 (screen.y - 200) *scale.y)//height
             , GuiTexture);
     //The width and height are set up to use padding, so it will be 100pixels less
     //then the width of the screen. (you have to include the size of the left side
     //otherwise it goes to the edge of the screen)
     
     //in this case, 100 pixel border on the texture.
 }

This isn't tested, but it should work. This should work whether the scaling is larger or smaller then the optimal height and width. Hope this helps some.

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 RickP · Jul 06, 2013 at 01:38 PM

Correct me if I'm wrong but the problem with the above examples would be that although it's resolution independent it's still aspect ratio dependent? I know he states he's only supporting "16:10" but wouldn't going to a 3D GUI solve screen resolution AND aspect ratio easier?

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
  • 1
  • 2
  • ›

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

15 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

Related Questions

Scaling the GUI using a matrix issues 2 Answers

Is there a way to set a GUI.matrix that will be used by all OnGui() functions? 1 Answer

GUI adapting to screen resolution? 3 Answers

How to maintain high resolution custom background images for GUI elements on different screen sizes? 0 Answers

GUI and Screen Resolution 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