How to Wrestle Down GUIs in Unity 3D

GUIs are not exactly easy to program in Unity 3D, since there is only one method available (OnGUI()) that obviously has to take up everything.

This becomes a problem as soon you try to create different menu screens that have to be switched out. One way to do it would probably be to use different scenes (which would be cumbersome); another way to use a switch, which would be just as cumbersome.

My way of doing it is using delegates, which allows to keep each screen encapsulated in a distinct method, while still retaining the same scene all the time.

  1. using UnityEngine;
  2. using System.Collections;
  3. using System; //Necessary to access Action
  4.  
  5. /// <summary>
  6. /// An example for using delegates to show different menu screens.
  7. /// </summary>
  8. public class DelegateGUI : MonoBehaviour {
  9.  
  10.   #region Setup
  11.  
  12.  /// <summary>
  13.     /// The delegate that is going to be used.
  14.  /// </summary>
  15.    private Action OnGUIMethod;
  16.  
  17.    /// <summary>
  18.     /// The standard GUI method, as inherited by MonoBehaviour.
  19.     /// </summary>
  20.    private void OnGUI () 
  21.  {
  22.       if (OnGUIMethod != null) {
  23.          OnGUIMethod();
  24.      }
  25.   }
  26.  
  27.  /// <summary>
  28.     /// Define what screen to show first.
  29.   /// </summary>
  30.    private void Start () {
  31.         OnGUIMethod = Screen1;
  32.  }
  33.  
  34.  #endregion
  35.  
  36.     #region Screens
  37.  
  38.    /// <summary>
  39.     /// A very simple function that will theoretically show two buttons,
  40.    /// which in turn show the other screens. The following functions
  41.   /// work similarly.
  42.     /// </summary>
  43.    private void Screen1 () {
  44.       // Show all that appears on the first screen – some Buttons might do
  45.  
  46.       if (GUI.Button(/* GUI.Button stuff comes here <em>/)) {
  47.            // Do stuff here
  48.            OnGUIMethod = Screen2;
  49.      }
  50.  
  51.      if (GUI.Button(/</em> GUI.Button stuff comes here <em>/)) {
  52.            // Do stuff here
  53.            OnGUIMethod = Screen3;
  54.      }
  55.  
  56.  }
  57.  
  58.  private void Screen2 () {
  59.       // Show all that appears on the second screen – some Buttons might do
  60.  
  61.      if (GUI.Button(/</em> GUI.Button stuff comes here <em>/)) {
  62.            // Do stuff here
  63.            OnGUIMethod = Screen3;
  64.      }
  65.  
  66.      if (GUI.Button(/</em> GUI.Button stuff comes here <em>/)) {
  67.            // Do stuff here
  68.            OnGUIMethod = Screen1;
  69.      }
  70.   }
  71.  
  72.  private void Screen3 () {
  73.       // Show all that appears on the third screen – some Buttons might do
  74.  
  75.       if (GUI.Button(/</em> GUI.Button stuff comes here <em>/)) {
  76.            // Do stuff here
  77.            OnGUIMethod = Screen2;
  78.      }
  79.  
  80.      if (GUI.Button(/</em> GUI.Button stuff comes here */)) {
  81.            // Do stuff here
  82.            OnGUIMethod = Screen1;
  83.      }
  84.   }
  85.  
  86.  #endregion
  87. }

Of course, using the coroutine pattern shown here, you could also add your own fade-ins and -outs here.

Social Tags: Product:

Comments

Great idea. Would you have any suggestions for patterns that would compass a transition between screens (say fade out/fade in) ?

Very nice.

I ran across your delegate method and it is perfect for a GUI element I was trying to implement.

Thank you.

Add new comment