c# - 为什么 Unity Event 以错误的方式工作?还是我?

标签 c# unity3d editor

我尝试在我的 MonoBehabiour 脚本中的 OnGUI 函数中获取一个 MouseUp 事件。

但它总是返回“Layout”和“Repaint”。

    private void OnGUI() {
    Event e = Event.current;
    Debug.Log (e.type);
}

我的调试是这样的: 布局 重绘 布局 重绘 布局 重绘等

为什么会这样? 我怎么知道获取事件 MouseUp ?

编辑1:

Event e = Event.current;
    int s = e.button;

我可以知道何时单击鼠标按钮 0,但是我如何知道何时获得 MouseUp 事件?

编辑2:

我想在编辑器中而不是在播放模式中使用它。 可能吗?

最佳答案

您可以使用 if (Event.current.type == EventType.MouseUp) 获取事件 MouseUp 事件.

这是一个完整的代码示例:

 void OnGUI()
 {
     Event mEvent = Event.current;

     if (mEvent != null && mEvent.isMouse)
     {
         //Get Mouse Down
         if (mEvent.type == EventType.MouseDown)
         {

         }

         //Get Mouse Up
         if (mEvent.type == EventType.MouseUp)
         {

         }

         //Get Mouse Move
         if (mEvent.type == EventType.MouseMove)
         {

         }
     }
 }

现在,检测哪个鼠标按钮:

 //Left Mouse button
 if (mEvent.button == 0)
 {

 }

 //Right  Mouse button
 if (mEvent.button == 1)
 {

 }

 //Middle  Mouse button
 if (mEvent.button == 2)
 {

 }

你可以把它们放在你的 if (mEvent.type == EventType.MouseDown) 里面和 if (mEvent.type == EventType.MouseUp)代码。

关于c# - 为什么 Unity Event 以错误的方式工作?还是我?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40982664/

相关文章:

c# - 如何在不显式命名属性的情况下打印对象的所有属性值

C# 增加控制台日志大小

linux - 如何在基于 Unity 的 Linux 构建中设置默认命令行参数

c# - 控制灯光组件 C#

editor - 你如何在 sublime 3 中通过缩进折叠代码?

html - 支持在 Coda 或 Espresso 中实时预览 Haml?

c# - 为什么我不能更改代码中的音量?

c# - WebMethod c# return double[]

Unity3D UGUI (aka "new UI") 绘制调用优化

css - sass 去声明