Using OnGUI() to display game data

As we are gathering game data about the player's performance, we can display that information on screen to the player's advantage.

Unity offers a collection of functions and constructs to display game data on the screen and create the Graphic User Interface (GUI) of your game. OnGUI() is an important method available in Unity and used to create and control GUI elements to be displayed on the screen. Inside the function, it is possible to put lines of code that create interface controls such as text fields, buttons, and sliders at runtime. We advise you to check out the following link:

http://docs.unity3d.com/Manual/gui-Basics.html

In this recipe, we will use the OnGUI() function to display game data. We will create a script to access the number of lives and the items collected, and display them on the screen.

We need a couple of screenshots to be used as icons too, so get ready to follow our instructions.

Getting ready

The first step is to import the images to be used to display the information on the screen. The plan is to have a small character icon in the bottom-left corner for every available life, and an icon and a number in the top-right corner to display the number of collected items.

To display both of these pieces of information on screen we make use of a method called Graphics.DrawTexture(). It creates a rectangular area on the screen and draws a texture inside it. The texture to be displayed can be passed as a global Texture type variable. Several extra parameters can be set for the rectangle area, and their details have been explained in the manual available at http://docs.unity3d.com/ScriptReference/Graphics.DrawTexture.html.

Let's do it!

How to do it…

  1. Access the Texture folder in your project and right-click anywhere inside the window to open the menu. Select Import New Asset, as shown in this screenshot:
    How to do it…
  2. Select the images named lifeIcon and collIcon from the package provided with this book. Alternatively, you can create your own images to use. In that case, remember to save them in PNG format.
  3. Select lifeIcon from the folder and check its Alpha is Transparency property, as shown in the following screenshot:
    How to do it…
  4. Repeat the operation to import and set up the collIcon PNG image.
  5. Next, we create a new script in the Scripts folder and name it GUI as well. Then we open it in Monodevelop.
  6. We begin by declaring two public variables of the Texture type to store the references to these pics, and two private variables to store the references to the scripts where we take the data to be displayed on screen. Add the following lines to the script:
      public Texture lifeIcon;
      public Texture collIcon;
      private PlatManager platScript;
      private Runner runScript;

    In the Start() function, as usual, we initialize the variables by getting the plat_manager and runner references. The following instructions must be added to the script:

      // Use this for initialization
      void Start () {
      platScript = GameObject.Find("plat_manager").GetComponent<PlatManager>();
        runScript = GameObject.Find ("runner").GetComponent<Runner>();
      }
  7. The next step is to use the OnGUI() function to draw the icons on the screen. We use a for loop to draw a life icon for each available life. Add the following lines to the script:
      void OnGUI(){
        for(int i=0; i<platScript.lives; i++){
          Graphics.DrawTexture(new Rect(10 + (60*i), 600, 50, 50), lifeIcon);
        }
      }

How it works…

The logic to display a number of icons based on a numeric parameter is pretty straightforward; we loop between the number of lives in the OnGUI() function itself. As OnGUI() is called at least once per frame (actually, it can be called more than once per cycle), the number of icons displayed on screen is always updated with the actual number of lives.

There's more...

The OnGUI() function is called before the GUI elements on the screen are rendered, and after events such as an input from the player (mouse, buttons, and so on). As a consequence, OnGUI() may be called several times during an update cycle. For that reason, it is not advisable to put game controls in the OnGUI() function. There is an interesting article about this available at http://answers.unity3d.com/questions/197798/clarification-on-updates-physics-events-order-and.html.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.35.72