简体   繁体   English

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

[英]Unity - Event.current in Editor Window not updating

So I'm working on a Custom Editor Window and trying to get when the user has clicked the left mouse button. 因此,我正在“自定义编辑器”窗口上进行操作,并尝试获取用户单击鼠标左键时的信息。 The problem I'm having is that whenever I click on a GUI element that I have placed, in this case an EditorGUI.Foldout element, the Event.current doesn't get updated. 我遇到的问题是,每当我单击放置的GUI元素(在本例中为EditorGUI.Foldout元素)时,Event.current都不会更新。 So when I try to check if the user is clicking the left mouse button it does nothing whenever it should be. 因此,当我尝试检查用户是否单击鼠标左键时,它什么也没做。 But if I'm not clicking in that area it detects it! 但是,如果我不单击该区域,它将检测到它!

So my question to you is what exactly am I doing wrong here? 所以我对你的问题是我在这里到底在做什么错? I'm collecting the Event.current at the very first line of void OnGUI() and no where else. 我在void OnGUI()的第一行收集Event.current,在其他地方则没有。 I'll add in some code snippets that will hopefully help in finding a solution! 我将添加一些代码段,希望它们有助于找到解决方案!

Where I call Event.Current: 我在哪里调用Event.Current:

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

When I'm trying to access 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;
    }
}

This is where I'm currently having troubles. 这是我目前遇到麻烦的地方。 Whenever I click on the area where the EditorGUI.Foldout is at it should be detected and be setup to be selected. 每当我单击EditorGUI.Foldout所在的区域时,都应该检测到该区域并将其设置为选中状态。 But it doesn't seem to update Event.current whenever I click in the Foldout's area. 但是,每当我单击“折叠”区域时,似乎都不会更新Event.current。 But it will update if I click anywhere else in the Editor Window. 但是,如果我在“编辑器”窗口中单击其他任何位置,它将更新。

If you have any idea why this would be happening I would love to hear your ideas! 如果您有任何想法,为什么会发生这种情况,我很想听听您的想法! Any and all help will be appreciated! 任何和所有帮助将不胜感激!

Another way do detect someone clicking the left mouse button is with the Input.GetKeyDown function within the Update function, like so: 检测到有人单击鼠标左键的另一种方法是使用Update函数中的Input.GetKeyDown函数,如下所示:

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

I hope this is helpful! 我希望这是有帮助的!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM