Search Unity

Centering GUI Group (for any Screen resolution)

Discussion in 'Immediate Mode GUI (IMGUI)' started by RIw, May 15, 2015.

  1. RIw

    RIw

    Joined:
    Jan 2, 2015
    Posts:
    90
    Hi
    I've made a simple GUI,I want it to be centered in any Screen Resolution.
    I've tried to use these tutorials but nothing has changed :(
    http://forum.unity3d.com/threads/centering-gui.113438/

    Here is my Source Code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class QuickMenu : MonoBehaviour {
    5.  
    6.     /// <summary>
    7.     /// Prostokąt określający wymiary tła QuickMenu
    8.     /// </summary>
    9.     private Rect QuickBackground;
    10.     /// <summary>
    11.     /// Prostokąt określający wymiary przycisku wyjścia
    12.     /// </summary>
    13.     private Rect QuitRect;
    14.  
    15.     /// <summary>
    16.     /// Lewy Górny prostokąt
    17.     /// </summary>
    18.     private Rect LURect;
    19.     /// <summary>
    20.     /// Lewy Dolny prostokąt
    21.     /// </summary>
    22.     private Rect LDRect;
    23.     /// <summary>
    24.     /// Prawy Górny prostokąt
    25.     /// </summary>
    26.     private Rect RURect;
    27.     /// <summary>
    28.     /// Prawy Dolny prostokąt
    29.     /// </summary>
    30.     private Rect RDRect;
    31.  
    32.     /// <summary>
    33.     /// Przycisk Play
    34.     /// </summary>
    35.     private Rect PRect;
    36.    
    37.     /// <summary>
    38.     /// Customowy pakiet stylów dla QuickGUI
    39.     /// </summary>
    40.     public GUISkin skin;
    41.  
    42.  
    43.    
    44.  
    45.     /// <summary>
    46.     /// Sprawdza czy QuickMenu jest włączone
    47.     /// </summary>
    48.     private bool QuickEnabled;
    49.  
    50.     // Use this for initialization
    51.     void Start () {
    52.         QuickEnabled = false; //Domyślnie QuickMenu schowane
    53.         QuitRect = new Rect(Screen.width/2 - 100,Screen.height/2 - 100,200,200);
    54.      
    55.         LURect = new Rect(0,0,80,80);
    56.         RURect = new Rect(100, 0, 80, 80);
    57.         LDRect = new Rect(0, 100, 80, 80);
    58.         RDRect = new Rect(100, 100, 80, 80);
    59.  
    60.         PRect = new Rect(40,35,100,100);
    61.     }
    62.    
    63.     // Update is called once per frame
    64.     void Update ()
    65.     {
    66.         if (Input.GetKeyDown(KeyCode.Q))
    67.         {
    68.             QuickEnabled = !QuickEnabled;
    69.         }
    70.     }
    71.  
    72.     void OnGUI()
    73.     {
    74.         GUI.skin = skin;
    75.         if(QuickEnabled)
    76.         {
    77.         PrepareBackground();
    78.         }
    79.      
    80.     }
    81.  
    82.     /// <summary>
    83.     /// Tworzy tło
    84.     /// </summary>
    85.     void PrepareBackground()
    86.     {
    87.         GUI.BeginGroup(QuitRect,"",skin.GetStyle("Center"));
    88.             GUI.Button(LURect,"",skin.GetStyle("LU"));
    89.             GUI.Button(LDRect, "", skin.GetStyle("LD"));
    90.             GUI.Button(RURect, "", skin.GetStyle("RU"));
    91.             GUI.Button(RDRect, "", skin.GetStyle("RD"));
    92.             GUI.Button(PRect, "", skin.GetStyle("Play"));
    93.         GUI.EndGroup();
    94.     }
    95. }
    96.