Game Development Reference
In-Depth Information
Next up are the buttons that will take your player into or out of the game. For those, you will once
again be using the Unity GUI.
1.
Create a new C# script in the Menu Scripts folder and name it StartMenuGUI .
2.
Create an Empty GameObject in the scene, and name it GUI Holder .
3.
Add the script to the new object.
One downside of the UnityGUI is that it does not automatically adjust to the screen size unless you
add the math to do so. When you are setting up both GUI Skins and the menu GUI, it is important
to have access to the Inspector during runtime. If you are authoring on a work station with multiple
monitors, you can easily tear off and arrange Unity's views to suit your needs. If you are mobile
and authoring on a laptop, where screen space is at a premium, you can get creative by adjusting
the layout in response to screen size. This will also allow you to let the player adjust the size of the
window (but not the aspect ratio).
The first step is to keep track of the screen size.
4.
Create the following variables:
public GUISkin newSkin; // custom skin to use
int screenW; // screen width
int screenH; // screen height
5.
Create the OnGUI function, and define the variables it will require:
void OnGUI () {
GUI.skin = newSkin;
//manage layout
print (Screen.height);
screenW = Screen.width;
screenH = Screen.height;
int yOffset = 30; //default offset
if (screenH < 800) yOffset = screenH / 50; // adjusted offset
}
6.
Save the script.
7.
With “Maximize on Play” on, click Play and adjust the Unity editor window
size (toggle out of maximize for the Unity editor if necessary), while watching
the printout in the console when you release the mouse button.
The console reports the window height each time you pause after resizing the Unity editor.
To make the buttons easier to position as a whole, you will be putting the buttons into a GUI Group.
This will allow you to shift the parent group to get the spacing right without having to adjust each
button individually.
Comment out the print line.
8.
 
Search WWH ::




Custom Search