官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 枝江市| 湘潭市| 新和县| 特克斯县| 甘泉县| 宁海县| 迁安市| 嘉善县| 色达县| 宣化县| 黔西| 富阳市| 沅江市| 和田县| 碌曲县| 沅陵县| 乌兰察布市| 息烽县| 中山市| 吉隆县| 仁寿县| 太谷县| 巴林左旗| 杭锦旗| 高淳县| 平邑县| 班玛县| 手机| 平遥县| 肥乡县| 西昌市| 石首市| 东阿县| 黄浦区| 西乡县| 芦山县| 社旗县| 六安市| 新闻| 台东县| 宜阳县|