Game Development Reference
In-Depth Information
Figure 8-1. Configuring a dedicated UI camera
Notice that I've grouped the GUI camera as a child object beneath an empty game object named
GUI to organize and group together all GUI elements in the scene. This is not essential, but
recommended. Take care to assign the GUI camera a unique and highest value for Depth (I've used
two here) to ensure it renders on top or in front of all other scene objects. This is important because
GUI objects should appear in front of everything else in-game, except for a mouse cursor graphic
and other input helpers (like gizmos), if your game supports them.
We've now created an orthographic camera in the scene for displaying a GUI. One of the most
notable features of a GUI camera is that all GUI graphics will be rendered in screen space in pixels.
To make this kind of GUI-rendering easier and more intuitive, it'll be useful to customize the camera
further through a C# script, in a GUICam class. First, since it's possible for the game window to
change size during gameplay (such as when the user resizes the window), we'll want to update the
Orthographic Size field of the camera on each frame , to ensure that its orthographic size will always
render world units to pixels at a 1:1 ratio. Second, because all GUI elements (such as buttons and
windows) will render together in a single orthographic coordinate system, it'll be useful to set the
origin of the coordinate space (0,0) at the top-left corner of the screen, as opposed to the center.
This means the bottom-right corner will always be screen width, screen height (whatever the screen
width and height is). To achieve this behavior, consider Listing 8-1 for GUICam.cs , which is attached
as a component to the GUI camera.
Listing 8-1. GUICam.cs: Customizing a GUI Camera
01 //-------------------------------------------------------------
02 using UnityEngine;
03 using System.Collections;
04 [ExecuteInEditMode]
05 //-------------------------------------------------------------
06 public class GUICam : MonoBehaviour
07 {
08 //Camera Component
09 private Camera Cam = null;
10
Search WWH ::




Custom Search