Game Development Reference
In-Depth Information
50 //Enable/Disable input
51 GameManager.Instance.InputAllowed = !bShow;
52
53 //Show/Hide menu graphics
54 SR.enabled = bShow;
55
56 //Enable/Disable button colliders
57 foreach(BoxCollider B in Colliders)
58 B.enabled = bShow;
59 }
60 //--------------------------------------------------------------
61 //Watch escape key input
62 void Update()
63 {
64 //If escape key pressed
65 if(Input.GetKeyDown(KeyCode.Escape))
66 SetOptionsVisible(!SR.enabled);
67 }
68 //--------------------------------------------------------------
69 }
Line 09. Features the main reference to a Sprite Renderer component for
drawing the main menu graphic to the screen. This will refer to a sprite from the
project atlas texture. The object reference is retrieved during the Start function,
at line 19.
Line 12. Features an array of collider objects, which will be used later in the
chapter to represent clickable areas for the menu buttons. A reference to a
collider array is required to disable the colliders when the menu is hidden, to
prevent the gamer from clicking buttons when the menu is off-screen.
Lines 45-59. The SetOptionsVisible function toggles the visibility of the menu.
Line 48 uses the TimeScale member of the native Time class to pause the game
when the menu is displayed. A TimeScale value of 1 represents normal speed,
a value of 0.5 represents half speed, and 0 means paused.
Note More information on the Time class and its TimeScale member can be found in the online Unity
documentation at http://docs.unity3d.com/Documentation/ScriptReference/Time-timeScale.html .
Let's give this class a test, along with the GUIObject class coded in the previous section. By
adding both components to a single menu object, we can create a menu that displays and hides
on the screen by pressing the Escape key. Be sure to add a sprite component to the object, too,
referencing the menu object ( spr_Menu ) in the atlas texture. You can add a sprite component to an
object manually by choosing Component Rendering Sprite Renderer from the application
menu. Then from the Object Inspector, set the Sprite field to spr_Menu (see Figure 8-5 ).
 
 
Search WWH ::




Custom Search