Search Unity

Best practices for a GUI-centered (menu driven) game?

Discussion in 'Immediate Mode GUI (IMGUI)' started by VBenincasa, Feb 13, 2014.

  1. VBenincasa

    VBenincasa

    Joined:
    Feb 13, 2014
    Posts:
    12
    I would like to know what would be an optimal way to organize a menu driven game in Unity. I mean games akin to Management style (Football Manager itself is a good example). Basically almost the entire game would be made up of GUI elements.

    Any tips?

    Thanks in advance!
     
  2. Patico

    Patico

    Joined:
    May 21, 2013
    Posts:
    886
    If you decided to draw GUI from scratch, then I could recommend you to make your own analog of Rect class (for example CenterRect), which always will place rectanlge at center of screen.

    See my implementation of such class bellow:
    Code (csharp):
    1.  
    2. /// <summary>
    3. /// Centered Rectangle
    4. /// </summary>
    5. public class CenterRect : PosRect
    6. {
    7.     public CenterRect(float x, float y, float horizontalOffset = 0, float verticalOffset = 0) : base(x, y, horizontalOffset, verticalOffset) { ; }
    8.     public CenterRect(Vector2 size, float horizontalOffset = 0, float verticalOffset = 0): base(size.x, size.y, horizontalOffset, verticalOffset) { ;}
    9.  
    10.     protected override Rect GetRect(float width, float height)
    11.     {
    12.         return new Rect((width - _width) * 0.5f + _horizontalOffset, (height - _height) * 0.5f + _verticalOffset, _width, _height);
    13.     }
    14. }
    15.  
    Base class
    Code (csharp):
    1.  
    2. public abstract class PosRect
    3. {
    4.     protected float _width;
    5.     protected float _height;
    6.     protected float _horizontalOffset;
    7.     protected float _verticalOffset;
    8.     protected Rect? _container = null;
    9.  
    10.     /// <summary>
    11.     /// Default constructor
    12.     /// </summary>
    13.     /// <param name="width">Width of the rectangle</param>
    14.     /// <param name="height">Height of the rectangle</param>
    15.     /// <param name="horizontalOffset">Offset along the horizontal direction</param>
    16.     /// <param name="verticalOffset">Offset along the vertical direction</param>
    17.     public PosRect(float width, float height, float horizontalOffset = 0, float verticalOffset = 0)
    18.     {
    19.         _width = width;
    20.         _height = height;
    21.         _horizontalOffset = horizontalOffset;
    22.         _verticalOffset = verticalOffset;
    23.     }
    24.     /// <summary>
    25.     /// Alternative constructor
    26.     /// </summary>
    27.     /// <param name="size">Width(x) and height(y) of the rectangle</param>
    28.     /// <param name="horizontalOffset">Offset along the horizontal direction</param>
    29.     /// <param name="verticalOffset">Offset along the vertical direction</param>
    30.     public PosRect(Vector2 size, float horizontalOffset = 0, float verticalOffset = 0)
    31.         : this(size.x, size.y, horizontalOffset, verticalOffset)
    32.     {
    33.         ;
    34.     }
    35.  
    36.     /// <summary>
    37.     /// Sets offset of the rectangle along the horizontal direction.
    38.     /// </summary>
    39.     /// <param name="offset">Ofset value</param>
    40.     public void SetHorizontalOffset(float offset) { _horizontalOffset = offset; }
    41.    
    42.     /// <summary>
    43.     /// Sets offset of the rectangle along the vertical direction.
    44.     /// </summary>
    45.     /// <param name="offset">Ofset value</param>
    46.     public void SetVerticalOffset(float offset) { _verticalOffset = offset; }
    47.  
    48.     /// <summary>
    49.     /// Sets root container
    50.     /// </summary>
    51.     /// <param name="groupRectangle">Rectangle of the root element (GUI.BeginGroup)</param>
    52.     public void SetContainer(Rect groupRectangle)
    53.     {
    54.         _container = groupRectangle;
    55.     }
    56.     public void RemoveContainer()
    57.     {
    58.         _container = null;
    59.     }
    60.     public bool IsInContainer()
    61.     {
    62.         return _container.HasValue;
    63.     }
    64.  
    65.     /// <summary>
    66.     /// Returns centered rectangle
    67.     /// </summary>
    68.     public Rect Rect
    69.     {
    70.         get
    71.         {
    72.             if (!IsInContainer())
    73.                 return GetRect(Screen.width, Screen.height);
    74.             else
    75.                 return GetRect(_container.Value.width, _container.Value.height);
    76.         }
    77.     }
    78.  
    79.     protected abstract Rect GetRect(float width, float height);
    80. }
    81.