- Unity Game Development Scripting
- Kyle D'Aoust
- 298字
- 2021-08-05 16:52:50
Let's switch!
Now, we'll create a way for the player to switch between PC and Xbox 360 Controller controls.
Creating control profiles
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.
Adding a profile switching function
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.
Adding the GUI interaction function
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.
- C#完全自學(xué)教程
- Hands-On Data Structures and Algorithms with JavaScript
- Android Application Development Cookbook(Second Edition)
- Quarkus實(shí)踐指南:構(gòu)建新一代的Kubernetes原生Java微服務(wù)
- FFmpeg入門詳解:音視頻原理及應(yīng)用
- Reactive Android Programming
- Learn React with TypeScript 3
- Jupyter數(shù)據(jù)科學(xué)實(shí)戰(zhàn)
- Extreme C
- Spring MVC+MyBatis開發(fā)從入門到項(xiàng)目實(shí)踐(超值版)
- Everyday Data Structures
- Programming Microsoft Dynamics? NAV 2015
- Data Science Algorithms in a Week
- Splunk Essentials
- 美麗洞察力:從化妝品行業(yè)看顧客需求洞察