Search Unity

"Auto center" and "auto scale" elements

Discussion in 'Immediate Mode GUI (IMGUI)' started by bigkahuna, Dec 23, 2007.

  1. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    I've got a GUI.Window that re-scales / re-sizes depending on the user's screen resolution/aspect ratio. The window has a number of GUILayout elements that I'm positioning/scaling like this:
    Code (csharp):
    1.     GUILayout.BeginHorizontal();
    2.  
    3.     GUILayout.Space (10);
    4.  
    5.     GUILayout.BeginVertical();
    6.     GUILayout.Label ("Label1");
    7.     GUILayout.Label ("Label2");
    8.     GUILayout.EndVertical();
    9.  
    10.     GUILayout.BeginVertical();
    11.     GUILayout.Button ("Yes");
    12.     GUILayout.Button ("Yes");
    13.     GUILayout.EndVertical();
    14.  
    15.     GUILayout.Space (10);
    16.    
    17.     GUILayout.EndHorizontal();
    The problem is that at very high resolutions the labels and buttons are miles long and at very low resolutions they may not fit in the window. What I need is some way to scale GUILayout.Space depending on the screen resolution. Anyone have a nice/clean way of doing this? (My current solution is kinda ugly... )
     
  2. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    I'm still trying to figure this out. I've searched through the docs and I can't seem to find a function to -center- a chunk of GUI elements. This chunk is formatted the way I want it, but I haven't found a way to center it in a window (that will work for various screen resolutions), can anyone help?
    Code (csharp):
    1.     GUILayout.BeginHorizontal();
    2.     GUILayout.Space (10); // any way to make this variable for screen res?
    3.     GUILayout.BeginVertical(GUILayout.Width (100));
    4.     GUILayout.Label ("Label 1");
    5.     GUILayout.Label ("Label 2");
    6.     GUILayout.EndVertical();
    7.     GUILayout.BeginVertical(GUILayout.Width (100));
    8.     GUILayout.Button ("Yes");
    9.     GUILayout.Button ("No");
    10.     GUILayout.EndVertical();
    11.     GUILayout.Space (10);  // any way to make this variable for screen res?
    12.     GUILayout.EndHorizontal();
     
  3. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    I don't know if this is what you are looking for but why not use:

    var space = Screen.height/something;

    this will be in an update function so it changes if fullscreen is on or of for example.

    oh and i don't know if the screen.height was screen.height or if it was something else like screenres.height or something (don't got acces to unity or unitron right now)

    hope it will help.
     
  4. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    What is the container of this code - is it a window?
    If so, you should probably calculate the Rect of the window from the Screen size, then use that Rect to define the layout (spacing) of your GUI. Try and abstract as much of the ground level GUI into containers, it's much easier. Also, you can make static methods to help with some common calculations.
    For Example (I use this to simplify alignment):
    Code (csharp):
    1. public static Rect uihelperAlignToScreen(int WindowWidth, int WindowHeight, string AlignType)
    2.     {
    3.         Rect ReturnRect = new Rect();
    4.         switch (AlignType)
    5.         {
    6.             case "Center":
    7.                 ReturnRect = new Rect((Screen.width / 2) - (WindowWidth / 2), (Screen.height / 2) - (WindowHeight / 2), WindowWidth, WindowHeight);
    8.                 break;
    9.             case "TopRight":
    10.                 ReturnRect = new Rect(Screen.width - WindowWidth, 0, WindowWidth, WindowHeight);
    11.                 break;
    12.             case "BottomCenter":
    13.                 ReturnRect = new Rect((Screen.width / 2) - (WindowWidth / 2), Screen.height - WindowHeight, WindowWidth, WindowHeight);
    14.                 break;
    15.         }
    16.         return ReturnRect;
    17.     }
     
  5. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    Thanks Shaun, I'll have to study that a bit. My current UI uses 9 GUILayout.Window's, and 5 of those windows contain an assortment of buttons, boxes and labels. At present, I loose about 28 fps (in the editor) to the GUI (the scene runs at 50 fps with the GUI and 78 fps without) which seems to be an awfully high overhead for a GUI, so I'll be looking for ways to optimize this.
     
  6. nafonso

    nafonso

    Joined:
    Aug 10, 2006
    Posts:
    377
    I'm doing a little game that also uses extensively GUI elements, and I also feel that it is kind of "heavy". Can we expect it to become faster in the next release or should we start looking for little tricks to optimize it ourselves?

    Regards,
    Afonso
     
  7. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    Good question, but so far I haven't seen too many complaints about UnityGUI performance nor has UT announced any improvements in that area... so I suspect it's up to us to tweak things, at least for now.