[英]Unity - Event.current in Editor Window not updating
因此,我正在“自定義編輯器”窗口上進行操作,並嘗試獲取用戶單擊鼠標左鍵時的信息。 我遇到的問題是,每當我單擊放置的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所在的區域時,都應該檢測到該區域並將其設置為選中狀態。 但是,每當我單擊“折疊”區域時,似乎都不會更新Event.current。 但是,如果我在“編輯器”窗口中單擊其他任何位置,它將更新。
如果您有任何想法,為什么會發生這種情況,我很想聽聽您的想法! 任何和所有幫助將不勝感激!
檢測到有人單擊鼠標左鍵的另一種方法是使用Update函數中的Input.GetKeyDown函數,如下所示:
void Update(){
if (Input.GetKeyDown(KeyCode.Mouse0)){
Debug.Log("Left mouse button clicked.");
}
}
我希望這是有幫助的!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.