• 2024-02-25 Unity 编辑器开发之编辑器拓展6 —— Event


    1 Event 介绍

    ​ Event 提供许多属性和方法,允许检查和处理用户输入,主要用于 Unity 编辑器拓展开发。

    ​ Input 相关内容需要在运行时才能监听输入,而 Event 专门提供给编辑模式下使用,可以帮助检测鼠标键盘输入等事件相关操作,在 OnGUI 和 OnSceneView 中都能使用。

    2 重要 API

    API说明
    Event.current获取当前事件。
    Event.current.altAlt 键是否按下。
    Event.current.shiftShift 键是否按下 。
    Event.current.controlCtrl 键是否按下。
    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.commandWindows 键或 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.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();
        }
    }
    
    • 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
  • 相关阅读:
    Scss--@extend--使用/实例
    新时代博物馆运营的智能管理与控制
    easyExcel 按照模板设置到处excel内容
    Vue3实现div拖拽改变宽高
    spring 什么是容器?什么是bean?
    LCR 171.训练计划 V
    mmdet之Loss模块详解
    如何将变量用作typescript的类型注解
    mpu6050移植DMP库
    Postman接口测试流程
  • 原文地址:https://blog.csdn.net/zheliku/article/details/136282157