Search Unity

XGUI - small GUI helper

Discussion in 'Immediate Mode GUI (IMGUI)' started by Tom163, Mar 16, 2011.

  1. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    Here's a script I've written for myself to make GUI handling a bit easier. Basically, it does two things:
    It bundles whatever you want to do to your GUI system into one call to XGUI.Setup(), where all the skin assignment, GUI matrix things, etc. are done.
    It simplifies those rectangle calculations, especially for different aspect ratios.

    Basically, a 16:9 screen is wider than a 4:3 screen, so the GUI elements needs to be shifted outwards a bit to give the same visual impression. Using simply GUI.matrix will scale them, changing their own aspect ratio. For text and simple elements like buttons that might work, but if you're using textures, their aspect ratios will become distorted. That's the problem this solves.

    In addition, it auto-detects if you mean absolute pixels or fractions of screen width/height (by the simple assumption that if you're using a value <= 1.0 you mean screen fractions). So instead of writing stuff like
    Code (csharp):
    1. GUI.Label(Rect(Screen.width*0.1, Screen.height*0.5, Screen.width*0.8, 120))
    you can write
    Code (csharp):
    1. GUI.Label(XGUI.SRect(0.1, 0.5, 0.8, 120))

    Here it is, if you find it useful, it's yours:
    Code (csharp):
    1.  
    2. /*
    3.     GUI functions helper
    4.     (C)2011 by Tom Vogt <tom@lemuria.org>
    5.    
    6.     "Manual":
    7.     * you'll usually want to call XGUI.Setup() at the beginning of your OnGUI() functions
    8.     * replace your Rect(x,y,w,h) calls with XGUI.SRect() calls
    9.       * you can use pixel values or fractions, fractions <= 1.0 will be interpreted as fractions of the screen, so
    10.         GUI.Label(XGUI.SRect(0.1, 0.5, 200, 30), "Test");
    11.         will be interpreted as putting a 200x30 label at the position x = 0.1*Screen.width and y = 0.5*Screen.height
    12.     * XGUI automatically applies horizontal shifting (but not scaling, so no distortion) to fix aspect ratios
    13.  
    14. */
    15. static var Skin:GUISkin;
    16. var MySkin:GUISkin;
    17.  
    18. static var Spacer:Texture2D;
    19. var MySpacer:Texture2D;
    20.  
    21. static var ScreenWidth:float = 1024;
    22. static var ScreenHeight:float = 768;
    23.  
    24. function Awake() {
    25.     DontDestroyOnLoad(gameObject);
    26.     Skin = MySkin;
    27.     spacer = MySpacer;
    28. }
    29.  
    30. static function Setup() {
    31.     GUI.skin = Skin;
    32.    
    33.     var Ratio:Vector3 = Vector3(Screen.height / ScreenHeight, Screen.height / ScreenHeight, 1);
    34.     GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, Ratio);
    35. }
    36.  
    37. static function Offset(rect:Rect) {
    38.     return Offset(rect.x,rect.y,rect.width,rect.height);
    39. }
    40. static function Offset(x:float, y:float, width:float, height:float) {
    41.     return Offset(x,y,width,height, x+(width/2));
    42. }
    43. static function Offset(x:float, y:float, width:float, height:float, center:float) {
    44.     var w:float = Screen.width; var h:float = Screen.height;
    45.     var screen_ratio:float = w/h;
    46.     var correction:float = screen_ratio / (ScreenWidth/ScreenHeight);
    47.  
    48.     var my_x = x;
    49.     if (center<ScreenWidth*1/3) {
    50.         my_x = x*correction;
    51.     } else if (center>ScreenWidth*2/3) {
    52.         my_x = (x+width)*correction - width;
    53.     } else {
    54.         my_x = (((x+width)*correction - width) + (x*correction))/2;
    55.     }
    56.     return my_x - x;
    57. }
    58.  
    59. static function SRect(x:float, y:float, width:float, height:float) {
    60.     if (x<=1.0) x*=ScreenWidth;     if (width<=1.0) width*=ScreenWidth;
    61.     if (y<=1.0) y*=ScreenHeight;    if (height<=1.0) height*=ScreenHeight;
    62.     return SRect(x,y,width,height, x+(width/2));
    63. }
    64. static function SRect(x:float, y:float, width:float, height:float, center:float) {
    65.     if (x<=1.0) x*=ScreenWidth;     if (width<=1.0) width*=ScreenWidth;
    66.     if (y<=1.0) y*=ScreenHeight;    if (height<=1.0) height*=ScreenHeight;
    67.     if (center<=1.0) center*=ScreenWidth;
    68.     var shift = Offset(x, y, width, height, center);
    69.     return Rect(x+shift, y, width, height);
    70. }
    71.  
    72. static function Shift(rect:Rect, xoff:float) {
    73.     return Rect(rect.x+xoff, rect.y, rect.width, rect.height);
    74. }
    75. static function Shift(rect:Rect, xoff:float, yoff:float) {
    76.     return Rect(rect.x+xoff, rect.y+yoff, rect.width, rect.height);
    77. }
    78.  
    79.