Unity编辑器拓展(六)-Event公共类

Event公共类相关内容

一、Event公共类是用来做什么的?

  • 它提供了许多属性和方法,允许你检查和处理用户输入
  • 主要用于在Unity编辑器拓展开发中
  • 因为Input相关内容需要在运行时才能监听输入
  • Event专门提供给编辑模式下使用,可以帮助我们检测鼠标键盘输入等事件相关操作
  • OnGUI 和 OnSceneView 中都能使用

二、 重要API

  1. 获取当前输入事件
1
Event.current
  1. alt键是否按下
1
Event.current.alt
  1. shift键是否按下
1
Event.current.shift
  1. ctrl键是否按下
1
Event.current.control
  1. 是否是鼠标事件
1
Event.current.isMouse
  1. 判断鼠标左中右键
1
Evnet.current.button (0,1,2 分别代表 左,右,中 如果大于2可能是其他鼠标按键)
  1. 鼠标位置
1
Event.curretn.mousePosition
  1. 判断是否是键盘输入
1
Event.current.isKey
  1. 获取键盘输入的字符
1
Event.current.character
  1. 获取键盘输入对应的KeyCode
1
Event.current.keyCode
  1. 判断输入类型
1
2
3
Event.current.type
EventType枚举和它比较即可
EventType中有常用的 鼠标按下抬起拖拽,键盘按下抬起等等类型
  1. 是否锁定大写 对应键盘上caps键是否开启
1
Event.current.capsLock
  1. Windows键或Command键是否按下
1
Event.current.command
  1. 键盘事件 字符串
1
Event.current.commandName
  • 可以用来判断是否触发了对应的键盘事件
  • 返回值:
    • Copy:拷贝
    • Paste:粘贴
    • Cut:剪切
  1. 鼠标间隔移动距离
1
Event.current.delta
  1. 是否是功能键输入
  • 功能键指小键盘中的 方向键, page up, page down, backspace等等
1
Event.current.functionKey
  1. 小键盘是否开启
1
Event.current.numeric
  1. 避免组合键冲突
  • 在处理完对应输入事件后,调用该方法,可以阻止事件继续派发,放置和Unity其他编辑器事件逻辑冲突
1
Event.current.Use()

三、 API使用示例

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
//1.获取当前事件
// Event.current
Event eve = Event.current;

//2.alt键是否按下
// Event.current.alt
if (eve.alt)
Debug.Log("alt键按下了");

//3.shift键是否按下
// Event.current.shift
if (eve.shift)
Debug.Log("shift键按下了");

//4.ctrl键是否按下
// Event.current.control
if (eve.control)
Debug.Log("control键按下了");

//5.是否是鼠标事件
// Event.current.isMouse
if (eve.isMouse)
{
Debug.Log("鼠标相关事件");
//6.判断鼠标左中右键
// Event.current.button (0,1,2 分别代表 左,右,中 如果大于2可能是其他鼠标按键)
Debug.Log(eve.button);
//7.鼠标位置
// Event.current.mousePosition
Debug.Log("鼠标位置" + eve.mousePosition);
}

//8.判断是否是键盘输入
// Event.current.isKey
if(eve.isKey)
{
Debug.Log("键盘相关事件");
//9.获取键盘输入的字符
// Event.current.character
Debug.Log(eve.character);
//10.获取键盘输入对应的KeyCode
// Event.current.keyCode
//Debug.Log(eve.keyCode);
switch (eve.keyCode)
{
case KeyCode.Space:
Debug.Log("空格键输入");
break;
}
}

//11.判断输入类型
// Event.current.type
// EventType枚举和它比较即可
// EventType中有常用的 鼠标按下抬起拖拽,键盘按下抬起等等类型
// 一般会配合它 来判断 比如 键盘 鼠标的抬起按下相关的操作

//12.是否锁定大写 对应键盘上caps键是否开启
// Event.current.capsLock
if (eve.capsLock)
Debug.Log("大小写锁定开启");
else
Debug.Log("大小写锁定关闭");

//13.Windows键或Command键是否按下
// Event.current.command
if (eve.command)
Debug.Log("PC win键按下 或 Mac Command键按下");

//14.键盘事件 字符串
// Event.current.commandName
// 可以用来判断是否触发了对应的键盘事件
// 返回值:
// Copy:拷贝
// Paste:粘贴
// Cut:剪切
if(eve.commandName == "Copy")
{
Debug.Log("按下了ctrl + c");
}
if (eve.commandName == "Paste")
{
Debug.Log("按下了ctrl + v");
}
if (eve.commandName == "Cut")
{
Debug.Log("按下了ctrl + x");
}

//15.鼠标间隔移动距离
// Event.current.delta

//Debug.Log(eve.delta);

//16.是否是功能键输入
// Event.current.functionKey
// 功能键指小键盘中的 方向键, page up, page down, backspace等等
if (eve.functionKey)
Debug.Log("有功能按键输入");

//17.小键盘是否开启
// Event.current.numeric
if(eve.numeric)
Debug.Log("小键盘是否开启");

//18.避免组合键冲突
// Event.current.Use()
// 在处理完对应输入事件后,调用该方法,可以阻止事件继续派发,防止和Unity其他编辑器事件逻辑冲突
eve.Use();

五、 更多内容

https://docs.unity3d.com/ScriptReference/Event.html