【问题标题】:Unity - Event.current in Editor Window not updatingUnity - 编辑器窗口中的 Event.current 未更新
【发布时间】:2016-02-05 20:51:22
【问题描述】:

所以我正在开发一个自定义编辑器窗口并尝试获取用户单击鼠标左键的时间。我遇到的问题是,每当我单击已放置的 GUI 元素(在本例中为 EditorGUI.Foldout 元素)时,Event.current 都不会更新。因此,当我尝试检查用户是否单击鼠标左键时,它应该什么都不做。但如果我没有点击该区域,它会检测到它!

所以我的问题是我在这里到底做错了什么?我在 void OnGUI() 的第一行收集 Event.current,没有其​​他地方。我将添加一些代码 sn-ps,希望有助于找到解决方案!

我在哪里调用 Event.Current:

void OnGUI()
{
    Event currentEvent = Event.current;
    ...
}

当我尝试访问 Event.Current 时:

void OnGUI()
{
    ...
    Rect baseLabelRect = new Rect();
    baseLabelRect.x = 0;
    baseLabelRect.y = 21;
    baseLabelRect.width = this.position.width;
    baseLabelRect.height = 16;

    if (selected)
        EditorGUI.DrawRect(baseLabelRect, selectedItemColor);

    EditorGUI.indentLevel = 1;

    GUI.contentColor = Color.black;
    GUI.backgroundColor = Color.grey;

    itemListFoldout[0] = EditorGUI.Foldout(baseLabelRect, itemListFoldout[0], "Fouldout test");

    GUI.contentColor = origContentColor;
    GUI.backgroundColor = origBackgroundColor;

    if (currentEvent.button == 0 && currentEvent.isMouse)
    {
        Debug.Log("left mouse button clicked");
        if (baseLabelRect.Contains(currentEvent.mousePosition))
        {
            selected = true;
            Debug.Log("rect clicked");
        }
        else
            selected = false;
    }
}

这是我目前遇到麻烦的地方。每当我单击 EditorGUI.Foldout 所在的区域时,都应该检测到它并设置为被选中。但是,每当我单击 Foldout 区域时,它似乎都不会更新 Event.current。但是,如果我单击编辑器窗口中的其他任何位置,它将更新。

如果您知道为什么会发生这种情况,我很想听听您的想法!任何和所有的帮助将不胜感激!

【问题讨论】:

    标签: c# window editor unity5


    【解决方案1】:

    另一种检测是否有人单击鼠标左键的方法是使用 Update 函数中的 Input.GetKeyDown 函数,如下所示:

    void Update(){
        if (Input.GetKeyDown(KeyCode.Mouse0)){
            Debug.Log("Left mouse button clicked.");
        }
    }
    

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 2020-01-05
      • 1970-01-01
      • 1970-01-01
      • 2018-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多