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

Making decisions based on user input

Decisions always have to be made when the user provides input. The input can arrive from different types of interfaces depending on the platform that we are working on. For example, if we are developing games for mobile phones, we would use the touch screen as an input for swipe, touch, touch with two fingers, and so on, while for game consoles we would use the gamepad buttons. For each button, we can assign an action and it's possible to combine multiple buttons (inputs) to perform different actions. Another important aspect that we need to consider is that when we press a key or a button, three different inputs are happening at that instance, GetKey, GetKeyDown, and GetKeyUp:

As we can see in the preceding description, we can assign three different actions for the same key, so we are not limited to only one option. Previously in this chapter, we used an example where the user had to press the Enter key to call the AddTwoNumbers() method:

if(Input.GetKeyUp(Keycode.Return)) AddTwoNumbers(); 

The if statement's condition becomes true only when the Return key is released after being pressed down.

Notice that the code of AddTwoNumbers() isn't between two curly brackets. When there is only one line of code to execute for an if or else statement, you have the option of not using the curly brackets.

Here's a partial screenshot of the GetKeyUp() method, as shown in Unity's Scripting Reference:

主站蜘蛛池模板: 信宜市| 额尔古纳市| 昔阳县| 永安市| 汽车| 探索| 婺源县| 浦东新区| 石狮市| 昭苏县| 乌拉特中旗| 博乐市| 金湖县| 临沭县| 定结县| 洛浦县| 镶黄旗| 揭西县| 社会| 黄骅市| 镇平县| 简阳市| 甘肃省| 馆陶县| 高唐县| 方山县| 青田县| 姚安县| 汉源县| 尤溪县| 井陉县| 台东市| 安图县| 沙河市| 宁陕县| 辽阳市| 东宁县| 鄂托克前旗| 文安县| 永嘉县| 靖边县|