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.
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!
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:Scripts
folder and name it GUI as well. Then we open it in Monodevelop.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>(); }
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); } }
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.
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.
3.146.35.72