文章目录
- 1 Event 介绍
- 2 重要 API
- 3 代码示例
1 Event 介绍
Event 提供许多属性和方法,允许检查和处理用户输入,主要用于 Unity 编辑器拓展开发。
Input 相关内容需要在运行时才能监听输入,而 Event 专门提供给编辑模式下使用,可以帮助检测鼠标键盘输入等事件相关操作,在 OnGUI 和 OnSceneView 中都能使用。
2 重要 API
API | 说明 |
---|---|
Event.current | 获取当前事件。 |
Event.current.alt | Alt 键是否按下。 |
Event.current.shift | Shift 键是否按下 。 |
Event.current.control | Ctrl 键是否按下。 |
Event.current.isMouse | 是否是鼠标事件。 |
Event.current.button | 判断鼠标左中右键,“0/1/2” 分别代表 “左/右/中”。 如果大于 2 可能是其他鼠标按键。 |
Event.current.mousePosition | 鼠标位置。 |
Event.current.isKey | 判断是否是键盘输入。 |
Event.current.character | 获取键盘输入的字符。 |
Event.current.keyCode | 获取键盘输入对应的 KeyCode。 |
Event.current.type | 判断输入类型。EventType 中有常用的: 1. 鼠标按下抬起拖拽; 2. 键盘按下抬起 等等类型,一般进行比较判断相关的操作。 |
Event.current.capsLock | 是否锁定大写 对应键盘上 Caps 键是否开启。 |
Event.current.command | Windows 键或 Command 键是否按下。 |
Event.current.commandName | 键盘事件字符串,用来判断是否触发了对应的键盘事件,返回值: Copy:拷贝; Paste:粘贴; Cut:剪切。 |
Event.current.delta | 鼠标间隔移动距离。 |
Event.current.functionKey | 是否是功能键输入,即小键盘中的 1. 方向键; 2. page up; 3. page down; 4. backspace 等等。 |
Event.current.numeric | 小键盘是否开启。 |
Event.current.Use() | 避免组合键冲突。 在处理完对应输入事件后,调用该方法,可以阻止事件继续派发,防止和 Unity 其他编辑器事件逻辑冲突。 |
更多内容:https://docs.unity3d.com/ScriptReference/Event.html。
3 代码示例
public class Lesson21 : EditorWindow
{[MenuItem("Unity编辑器拓展/Lesson21/Event知识点学习")]private static void OpenLesson21() {Lesson21 win = EditorWindow.GetWindow<Lesson21>("Event知识学习");win.Show();}private void OnGUI() {//1.获取当前事件// Event.currentEvent eve = Event.current;//2.alt键是否按下// Event.current.altif (eve.alt)Debug.Log("alt键按下了");//3.shift键是否按下// Event.current.shiftif (eve.shift)Debug.Log("shift键按下了");//4.ctrl键是否按下// Event.current.controlif (eve.control)Debug.Log("control键按下了");//5.是否是鼠标事件// Event.current.isMouseif (eve.isMouse) {Debug.Log("鼠标相关事件");//6.判断鼠标左中右键// Event.current.button (0,1,2 分别代表 左,右,中 如果大于2可能是其他鼠标按键)Debug.Log(eve.button);//7.鼠标位置// Event.current.mousePositionDebug.Log("鼠标位置" + eve.mousePosition);}//8.判断是否是键盘输入// Event.current.isKeyif (eve.isKey) {Debug.Log("键盘相关事件");//9.获取键盘输入的字符// Event.current.characterDebug.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.capsLockif (eve.capsLock)Debug.Log("大小写锁定开启");elseDebug.Log("大小写锁定关闭");//13.Windows键或Command键是否按下// Event.current.commandif (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.numericif (eve.numeric)Debug.Log("小键盘是否开启");//18.避免组合键冲突// Event.current.Use()// 在处理完对应输入事件后,调用该方法,可以阻止事件继续派发,放置和Unity其他编辑器事件逻辑冲突eve.Use();}
}