usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;usingUnityEngine.InputSystem;publicclassLesson3:MonoBehaviour{// Start is called before the first frame updatevoidStart(){#region 知识点一 获取当前鼠标设备(需要引用命名空间)//和前面的键盘一样 要先获取我们使用的鼠标Mouse mouse = Mouse.current;#endregion#region 知识点二 鼠标各个键位 按下 抬起 长按//鼠标左键//mouse.leftButton;//鼠标右键//mouse.rightButton;//鼠标中键//mouse.middleButton;//鼠标侧键(前后)//mouse.forwardButton;//mouse.backButton;if(mouse.leftButton.wasPressedThisFrame){}if(mouse.leftButton.wasReleasedThisFrame){}if(mouse.leftButton.isPressed){}#endregion#region 知识点三 鼠标位置相关//获取鼠标当前位置//注意这个position是Mouse封装了的 如果要得到具体位置的值 需要.ReadValue();
mouse.position.ReadValue();//获得鼠标这一帧 和上一帧的偏移量
mouse.delta.ReadValue();//获得鼠标滚轮的值(方向向量)
mouse.scroll.ReadValue();#endregion}// Update is called once per framevoidUpdate(){if(Mouse.current.leftButton.wasPressedThisFrame){print("左键按下");}if(Mouse.current.leftButton.wasReleasedThisFrame){print("左键抬起");}if(Mouse.current.rightButton.isPressed){print("右键长按");}if(Mouse.current.forwardButton.isPressed){print("前侧键长按");}//print(Mouse.current.position.ReadValue());//print(Mouse.current.delta.ReadValue());print(Mouse.current.scroll.ReadValue());}}
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
class="hide-preCode-box">
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
Lesson4-触屏输入
usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;usingUnityEngine.InputSystem.Controls;usingUnityEngine.InputSystem;publicclassLesson4:MonoBehaviour{// Start is called before the first frame updatevoidStart(){#region 知识点一 获取当前触屏设备Touchscreen ts = Touchscreen.current;//由于触屏相关都是在移动平台或提供触屏的设备上使用//所以在使用时最好做一次判空if(ts ==null)return;#endregion#region 知识点二 得到触屏手指信息//得到触屏手指数量print(ts.touches.Count);//得到单个触屏手指print(ts.touches[0]);//得到所有触屏手指foreach(var t in ts.touches){}#endregion#region 知识点三 手指按下 抬起 长按 点击//获取指定索引手指print(ts.touches[0]);//要判断是否抬起等等 要先获得这根手指的信息TouchControl tc = ts.touches[0];//按下 抬起if(tc.press.wasPressedThisFrame){}if(tc.press.wasReleasedThisFrame){}//长按if(tc.press.isPressed){}//点击手势if(tc.tap.isPressed){}//连续点击次数print(tc.tapCount);#endregion#region 知识点四 手指位置等相关信息//位置print(tc.position.ReadValue());//第一次接触时位置print(tc.startPosition.ReadValue());//接触区域大小print(tc.radius.ReadValue());//和上一帧的偏移位置print(tc.delta.ReadValue());//得到当前手指的 状态(阶段)UnityEngine.InputSystem.TouchPhase tp = tc.phase.ReadValue();switch(tp){//无状态case UnityEngine.InputSystem.TouchPhase.None:break;//手指开始接触屏幕case UnityEngine.InputSystem.TouchPhase.Began:break;//手指开始在屏幕上移动case UnityEngine.InputSystem.TouchPhase.Moved:break;//手指结束移动case UnityEngine.InputSystem.TouchPhase.Ended:break;//手指取消移动case UnityEngine.InputSystem.TouchPhase.Canceled:break;//静止 手指没动了case UnityEngine.InputSystem.TouchPhase.Stationary:break;default:break;}#endregion}// Update is called once per framevoidUpdate(){}}
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
class="hide-preCode-box">
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
Lesson5-手柄输入
usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;usingUnityEngine.InputSystem;publicclassLesson5:MonoBehaviour{// Start is called before the first frame updatevoidStart(){#region 知识点一 获取当前手柄Gamepad gp = Gamepad.current;//判断是否得到手柄if(gp ==null){return;}#endregion#region 知识点二 手柄摇杆//摇杆方向//左摇杆print(gp.leftStick.ReadValue());//右摇杆print(gp.rightStick.ReadValue());//摇杆按下 抬起 长按//左摇杆if(gp.leftStickButton.wasPressedThisFrame){}if(gp.leftStickButton.wasReleasedThisFrame){}if(gp.leftStickButton.isPressed){}//右摇杆#endregion#region 知识点三 手柄方向键//对应手柄上4个方向键 上下左右//gamePad.dpad.left//gamePad.dpad.right//gamePad.dpad.up//gamePad.dpad.down//按下 抬起 长按if(gp.dpad.left.wasPressedThisFrame){}if(gp.dpad.left.wasReleasedThisFrame){}if(gp.dpad.left.isPressed){}#endregion#region 知识点四 手柄右侧按键//通用//东西南北//Y、△//北方(上方) gp.buttonNorth//A、X//南方(下方) gp.buttonSouth//X、□//西方(左方) gp.buttonWest//B、○//东方(右方) gp.buttonEast//它们的按下抬起长按和上面一致//wasPressedThisFrame//wasReleasedThisFrame//isPressed//不建议这样使用 因为不同手柄的名称都是不一样的 而上面的东西南北是通用的//手柄右侧按钮 x ○ △ □ A B Y //○//gamePad.circleButton//△//gamePad.triangleButton//□//gamePad.squareButton//X//gamePad.crossButton//x//gamePad.xButton//a//gamePad.aButton//b//gamePad.bButton//Y//gamePad.yButton#endregion#region 知识点五 手柄中央按键//中央键//gp.startButton;//gp.selectButton;//wasPressedThisFrame//wasReleasedThisFrame//isPressed#endregion#region 知识点六 手柄肩部按键//左上右上 肩部键位//左右前方肩部键//gp.leftShoulder;//gp.rightShoulder;//左右后方触发键(油门键)//gamePad.leftTrigger//gamePad.rightTrigger//wasPressedThisFrame//wasReleasedThisFrame//isPressed#endregion}// Update is called once per framevoidUpdate(){}}
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}"> class="hide-preCode-box">
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
Lesson6-其他输入
usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;publicclassLesson6:MonoBehaviour{// Start is called before the first frame updatevoidStart(){#region 知识点一 新输入系统中的输入设备类//常用的//Keyboard—键盘//Mouse—鼠标//Touchscreen—触屏//Gamepad—手柄//其它//Joystick—摇杆//Pen—电子笔//Sensor(传感器)//https://docs.unity3d.com/Packages/[email protected]/manual/Sensors.html#accelerometer//Gyroscope—陀螺仪//GravitySensor—重力传感器//加速传感器//光照传感器//等等#endregion#region 知识点二 关于没有细讲的设备//对于我们没有细讲的设备//平时在开发中不是特别常用//如果想要学习他们的使用//最直接的方式就是看官方的文档//或者直接进到类的内部查看具体成员//通过查看变量名和方法名即可了解使用方式//UnityEngine.InputSystem.Gyroscope g = UnityEngine.InputSystem.Gyroscope.current;//g.angularVelocity.ReadValue()#endregion#region 知识点三 注意事项//新输入系统的设计初衷就是想提升开发者的开发效率//我们不提倡写代码来处理输入逻辑//之后我们学了配置文件相关知识后//都是通过配置文件来设置监听(监视窃听)的输入事件类型//我们只需要把工作重心放在输入触发后的逻辑处理//所以我们目前讲解的知识都是为了之后最准备//实际开发中使用较少#endregion}// Update is called once per framevoidUpdate(){}}
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}"> class="hide-preCode-box">
usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;publicclassLesson8:MonoBehaviour{// Start is called before the first frame updatevoidStart(){#region 知识点一 什么是输入配置文件?//输入系统中提供了一种输入配置文件//你可以理解它是InputAction的集合//可以在一个文件中编辑多个InputAction的信息//里面记录了想要处理的行为和动作(也就是InputAction的相关信息)//我们可以在其中自己定义 InputAction(比如:开火、移动、旋转等)//然后为这个InputAction关联对应的输入动作//之后将该配置文件和PlayerInput进行关联//PlayerInput会自动帮助我们解析该文件//当触发这些InputAction输入动作时会以分发事件的形式通知我们执行行为#endregion#region 知识点二 编辑输入配置文件//1.在Project窗口右键Create创建InputActions配置文件//2.双击创建出的文件//3.进行配置#endregion#region 知识点三 配置窗口参数相关#endregion}// Update is called once per framevoidUpdate(){}}
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}"> class="hide-preCode-box">
usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;publicclassLesson9:MonoBehaviour{Lesson9Input input;// Start is called before the first frame updatevoidStart(){#region 知识点一 根据配置文件生成C#脚本//1.选择InputActions文件//2.在Inspector窗口设置生成路径,类名,命名空间//3.应用后生成C#脚本#endregion#region 知识点二 使用C#代码进行监听//我们为InputActions生成了对应的C#脚本 接下来的使用和InputAction的使用类似//1.创建生成的脚本类对象
input =newLesson9Input();//2.激活输入
input.Enable();//3.事件监听//因为InputActions中有多套动作行为//所以先使用声明的类对象.出来是使用的哪套输入动作//再.出来 要使用的是哪个动作行为//再.出来三个事件进行使用
input.Action1.Fire.performed +=(callback)=>{print("开火了");};
input.Action2.Space.performed +=(callback)=>{print("跳跃了");};#endregion}// Update is called once per framevoidUpdate(){print(input.Action1.Move.ReadValue<Vector2>());}}
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}"> class="hide-preCode-box">
评论记录:
回复评论: