-
Book Overview & Buying
-
Table Of Contents
Unity Game Development Scripting
By :
Now, we'll create a way for the player to switch between PC and Xbox 360 Controller controls.
To create our profiles, we'll need to add a new variable. Add the following enum to the top of our script, before the class declaration:
public enum ControlProfile { PC, Controller };Add it to your variables as well, like this:
public ControlProfile cProfile;
Finally, go to the DetectController() function. Add this line of code before the line of code where you call the IdentifyController() function in the if statement:
cProfile = ControlProfile.Controller;
After this, add an else statement to the if statement with another line of code after it:
else cProfile = ControlProfile.PC;
We are setting our enum variable in the DetectController() function to give us a default control profile. This is a fast and effective way to give our player the best control profile possible.
Next, we'll add a function that we can call to manually switch the control profile. Add this function to our code:
void SwitchProfile (ControlProfile Switcher)
{
cProfile = Switcher;
}We can call this function later to let the player choose between using the keyboard/mouse or the Xbox 360 Controller.
Now, we'll add a button to the bottom right of our controls page to let the player pick between the keyboard/mouse and Xbox 360 Controller. Add this code to your onGUI() function, just before the line where we end the group:
GUI.Label(new Rect(450, 345, 125, 20), "Current Controls");
if(GUI.Button(new Rect(425, 370, 135, 20), cProfile.ToString()))
{
if(cProfile == ControlProfile.Controller)
SwitchProfile(ControlProfile.PC);
else
SwitchProfile(ControlProfile.Controller);
}The text on the button will display which current control profile is being used. When the player clicks on the button, it will switch the control profile.
Change the font size
Change margin width
Change background colour