Unity - 编辑器窗口中的Event.current不更新

时间:2016-02-05 20:51:23

标签: c# window editor unity5

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

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

我称之为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。但如果我点击编辑器窗口中的任何其他位置,它将会更新。

如果你知道为什么会这样,我很想听听你的想法!任何和所有的帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

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

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

我希望这有用!