Time for action - create a button UI control

We're going to add a button to the title screen through code, using Unity's built-in OnGUI function.

  1. Double-click the TitleGUI script, or switch over to the script editor if it's already open.
  2. We won't need the Update function any longer. Instead, we'll be using the built-in OnGUI function. Change the word Update to OnGUI:
    var customSkin:GUISkin;
    function OnGUI() {
    }
    
  3. Add a button-creating chunk of code inside the OnGUI function:
    var customSkin:GUISkin;
    function OnGUI() {
    if(GUI.Button(Rect(0,0,100,50),"Play Game")) {
    print("You clicked me!");
    }
    }
    
  4. Save the script and press the Unity Play button to test your game.

A button labeled Play Game appears at the top-left of the screen. When you click on it, ...

Get Unity 3D Game Development by Example now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.