O'Reilly logo

Unity 3D Game Development by Example by Ryan Henson Creighton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required