Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

relative scale with different screen size?

Discussion in 'Immediate Mode GUI (IMGUI)' started by MattFS, Aug 13, 2010.

  1. MattFS

    MattFS

    Joined:
    Jul 14, 2009
    Posts:
    219
    I've noticed inside the Editor and when running a standalone that the GUI will not scale itself and that placing GUI elements such as Labels are not relative to the screen but absolute desktop resolution offsets!

    So the end result here is that unless my application is used at precisely the resolution I built it inside the Editor, it will be all wrong.

    How are people dealing with this?
     
  2. menneske

    menneske

    Joined:
    Jan 23, 2009
    Posts:
    44
    If you want scaling buttons and labels, try using GUILayout with the ExpandWidth and/or ExpandHeight options.

    If you want GUI where the element positions adapt to the resolution, you have two main options:

    You can group GUI elements between the GUI.BeginGroup() and GUI.EndGroup() calls. Then you can move the group to adapt to different resolutions.

    Or, with the GUILayout, you have the GUILayout.FlexibleSpace(). Using this inserts space between your elements which will stretch or contract as the resolution changes.
     
  3. MattFS

    MattFS

    Joined:
    Jul 14, 2009
    Posts:
    219
    thanks for the reply, though maybe I wasn't entirely clear the problem is with the font rendering... it will always stay at the same size rather than scale relative to the application resolution

    Attached are two shots of the same GUI one at 1600x900 and the other at 640x480.

    So two end-users running your application at different resolutions will have entirely different GUI's - and you can see how this could quickly become a pile of garbage on the screen at certain resolutions.

    In other applications/engines I've used often a 'virtual' GUI canvas is defined, so numbers and scales and sizes are all relative to the virtual canvas... which allows for scaling no problems.

    But if this is how the Unity GUI's are, fair enough, then what are developers doing about it in their applications?
     

    Attached Files:

  4. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    Or you could use this line when you start your code :D

    Code (csharp):
    1.  
    2. function OnGUI () {
    3. GUI.matrix = Matrix4x4.TRS (Vector3.zero, Quaternion.identity,Vector3(Screen.width / 1024.0, Screen.height / 768.0, 1));//All one line
    4. ///@@@///==YOUR CODE HERE==///@@@///
    5. }
    6.  
    Just set the desired screen res in the screen h w.
    i think there is even a couple of other waus to auto scale with screen res. And there are a couple of scripts on the wiki for scaling a GUITexture if you need it.
     
  5. sybixsus2

    sybixsus2

    Joined:
    Feb 16, 2009
    Posts:
    943
    That's how I do it too, but better not to put that in your OnGUI method as it gets called several times per frame. Better to calculate it once when the screen resolution is changed/detected and cache it for use in the OnGUI function.
     
  6. Esa

    Esa

    Joined:
    Mar 18, 2010
    Posts:
    39
    What is the proper typing for GUI.matrix for C#?
     
  7. rouhee

    rouhee

    Joined:
    Dec 23, 2008
    Posts:
    194
    Like this?

    Code (csharp):
    1. //All one line C#
    2. GUI.matrix = Matrix4x4.TRS( Vector3.zero, Quaternion.identity, new Vector3( Screen.width / 1024.0f, Screen.height / 768.0f, 1.0f ) );
    3.  
     
  8. Esa

    Esa

    Joined:
    Mar 18, 2010
    Posts:
    39
    Thank you.
     
  9. Barbur

    Barbur

    Joined:
    Oct 30, 2009
    Posts:
    160
    Hello,

    The solution of using a GUI.matrix to adapt the GUI to any resolution seems to work perfect but when I use a panoramic 16:9 resolution all my GUI is expanded too much. Is there a solution for this? Maybe using another matrix when the aspect is 16:9?