Unity3D编辑器扩展——EditorWindow交互响应事件

重点:需要在OnGUI方法里监听事件


Unity的编辑器界面中由这么一些事件: EventType
        MouseDown //鼠标按下
        MouseUp //鼠标抬起

        MouseDrag//鼠标拖动
        KeyDown//按键按下
        KeyUp //按键抬起
        ScrollWheel//中轮滚动
        Repaint //每一帧重新渲染会发
        Layout //布局更新
        DragUpdated //拖拽的资源进入界面
        DragPerform //拖拽的资源放到了某个区域里
        Ignore //操作被忽略
        Used//操作已经被使用过了
        ValidateCommand //有某种操作被触发(例如复制和粘贴)
        ExecuteCommand//有某种操作被执行(例如复制和粘贴)
        DragExited //松开拖拽的资源
        ContextClick //右键点击

    private void OnGUI()
    {
        if (Event.current.type == EventType.MouseDown)
        {
            Debug.LogError(EventType.MouseDown);//鼠标按下
        }
        else if (Event.current.type == EventType.MouseUp)
        {
            Debug.LogError(EventType.MouseUp);//鼠标抬起
        }
        else if (Event.current.type == EventType.MouseMove)
        {
            Debug.LogError(EventType.MouseMove);
        }
        else if (Event.current.type == EventType.MouseDrag)
        {
            Debug.LogError(EventType.MouseDrag);//鼠标拖动
        }
        else if (Event.current.type == EventType.KeyDown)
        {
            Debug.LogError(EventType.KeyDown);//按键按下
        }
        else if (Event.current.type == EventType.KeyUp)
        {
            Debug.LogError(EventType.KeyUp);//按键抬起
        }
        else if (Event.current.type == EventType.ScrollWheel)
        {
            Debug.LogError(EventType.ScrollWheel);//中轮滚动
        }
        else if (Event.current.type == EventType.Repaint)
        {
            Debug.LogError(EventType.Repaint);//每一帧重新渲染会发
        }
        else if (Event.current.type == EventType.Layout)
        {
            Debug.LogError(EventType.Layout);
        }
        else if (Event.current.type == EventType.DragUpdated)
        {
            Debug.LogError(EventType.DragUpdated);//拖拽的资源进入界面
        }
        else if (Event.current.type == EventType.DragPerform)
        {
            Debug.LogError(EventType.DragPerform);//拖拽的资源放到了某个区域里
        }
        else if (Event.current.type == EventType.Ignore)
        {
            Debug.LogError(EventType.Ignore);//操作被忽略
        }
        else if (Event.current.type == EventType.Used)
        {
            Debug.LogError(EventType.Used);//操作已经被使用过了
        }
        else if (Event.current.type == EventType.ValidateCommand)
        {
            Debug.LogError(EventType.ValidateCommand);//有某种操作被触发(例如复制和粘贴)
        }
        else if (Event.current.type == EventType.ExecuteCommand)
        {
            Debug.LogError(EventType.ExecuteCommand);//有某种操作被执行(例如复制和粘贴)
        }
        else if (Event.current.type == EventType.DragExited)
        {
            Debug.LogError(EventType.DragExited);//松开拖拽的资源
        }
        else if (Event.current.type == EventType.ContextClick)
        {
            Debug.LogError(EventType.ContextClick);//右键点击
        }
        else if (Event.current.type == EventType.MouseEnterWindow)
        {
            Debug.LogError(EventType.MouseEnterWindow);
        }
        else if (Event.current.type == EventType.MouseLeaveWindow)
        {
            Debug.LogError(EventType.MouseLeaveWindow);
        }
    }

具体使用:

        右键显示菜单:

            if (Event.current.type == EventType.ContextClick)
            {
                
                GenericMenu menu = new GenericMenu();

                menu.AddItem(new GUIContent("1"), false, null, null);

                menu.AddSeparator("");

                menu.AddItem(new GUIContent("2"), false, null, null);

                menu.ShowAsContext();

                //设置该事件被使用

                Event.current.Use();
            }

有三个事件比较特殊,需要在OnGUI的方法的时候设置一个参数为true才能接收到

        MouseEnterWindow //鼠标进入窗体
        MouseLeaveWindow //鼠标离开窗体

        MouseMove //鼠标移动

   private void OnGUI()
    {
        wantsMouseMove = true;
        wantsMouseEnterLeaveWindow = true;
    }

猜你喜欢

转载自blog.csdn.net/qq_28474981/article/details/83037411