Game Development Reference
In-Depth Information
11 //Pixel to World Scale
12 public float PixelToWorldScale = 200.0f;
13
14 //Cached transform for camera
15 private Transform ThisTransform = null;
16 //-------------------------------------------------------------
17 // Use this for initialization
18 void Start ()
19 {
20 //Get camera component for GUI
21 Cam = GetComponent<Camera>();
22
23 //Get camera transform
24 ThisTransform = transform;
25 }
26 //-------------------------------------------------------------
27 // Update is called once per frame
28 void Update ()
29 {
30 //Update camera size
31 Cam.orthographicSize = Screen.height/2/PixelToWorldScale;
32
33 //Offset camera so top-left of screen is position (0,0) for game objects
34 ThisTransform.localPosition = new Vector3(Screen.width/2/PixelToWorldScale, -
(Screen.height/2/PixelToWorldScale), ThisTransform.localPosition.z);
35 }
36 //-------------------------------------------------------------
37 }
38 //-------------------------------------------------------------
Line 04. The [ExecuteInEditMode] directive allows all instances of the class in
the scene to run in Edit mode, as well as Play mode. Doing this for GUI classes
allows you to get real-time previews of your GUIs and arrangements from the
Game tab in the Editor, as we'll see later.
Line 34. The position of the camera in the scene is offset away from the local
origin (the origin of the parent object) so that any other sibling objects will be
rendered as though they were offset from the top-left corner of the screen.
Resolution Dependence and Independence
Now that we have a GUI camera in place in the scene, we can start thinking carefully about adding
GUI widgets to the mixture—that is, visual controls (such as buttons and images) constituting the GUI.
When considering this (specifically, considering how to position and draw GUI graphics on-screen),
a very significant problem arises relating to screen sizes. The problem is that different devices,
hardware, and systems support different resolutions (pixel dimensions), and so your game may not
always appear at single, default resolution. You can, of course, configure your game in Unity (through
Player Settings ) to run at only one resolution, rejecting all other resolutions, but this exclusionary
 
Search WWH ::




Custom Search