我可以通過EventSystem.current.IsPointerOverGameObject()
檢測鼠標是否懸停在任何UI元素上。如何獲取Unity3D中指針的UI元素?
但是我怎麼知道這究竟是哪個GameObjest
?
我曾嘗試:
if (EventSystem.current.IsPointerOverGameObject())
foreach (GameObject go in new PointerEventData(EventSystem.current).hovered)
print(go.name);
但在每一刻new PointerEventData(EventSystem.current).hovered
是空的我是否懸停或沒有。
我可以看到使用print(EventSystem.current);
所需的信息:
<b>Selected:</b>
<b>Pointer Input Module of type: </b>UnityEngine.EventSystems.StandaloneInputModule
<B>Pointer:</b> -1
<b>Position</b>: (746.0, 535.0)
<b>delta</b>: (60.0, -44.0)
<b>eligibleForClick</b>: False
<b>pointerEnter</b>: cursorInfoText (UnityEngine.GameObject)
<b>pointerPress</b>:
<b>lastPointerPress</b>:
<b>pointerDrag</b>:
<b>Use Drag Threshold</b>: True
<b>Current Rayast:</b>
Name: cursorInfoText (UnityEngine.GameObject)
module: Name: Canvas (UnityEngine.GameObject)
eventCamera:
sortOrderPriority: 0
renderOrderPriority: 0
module camera: null
distance: 0
index: 0
depth: 1
worldNormal: (0.0, 0.0, 0.0)
worldPosition: (0.0, 0.0, 0.0)
screenPosition: (746.0, 535.0)
module.sortOrderPriority: 0
module.renderOrderPriority: 0
sortingLayer: 0
sortingOrder: 0
<b>Press Rayast:</b>
<B>Pointer:</b> -2
<b>Position</b>: (746.0, 535.0)
<b>delta</b>: (60.0, -44.0)
<b>eligibleForClick</b>: False
<b>pointerEnter</b>: cursorInfoText (UnityEngine.GameObject)
<b>pointerPress</b>:
<b>lastPointerPress</b>:
<b>pointerDrag</b>:
<b>Use Drag Threshold</b>: True
<b>Current Rayast:</b>
Name: cursorInfoText (UnityEngine.GameObject)
module: Name: Canvas (UnityEngine.GameObject)
eventCamera:
sortOrderPriority: 0
renderOrderPriority: 0
module camera: null
distance: 0
index: 0
depth: 1
worldNormal: (0.0, 0.0, 0.0)
worldPosition: (0.0, 0.0, 0.0)
screenPosition: (746.0, 535.0)
module.sortOrderPriority: 0
module.renderOrderPriority: 0
sortingLayer: 0
sortingOrder: 0
<b>Press Rayast:</b>
<B>Pointer:</b> -3
<b>Position</b>: (746.0, 535.0)
<b>delta</b>: (60.0, -44.0)
<b>eligibleForClick</b>: False
<b>pointerEnter</b>: cursorInfoText (UnityEngine.GameObject)
<b>pointerPress</b>:
<b>lastPointerPress</b>:
<b>pointerDrag</b>:
<b>Use Drag Threshold</b>: True
<b>Current Rayast:</b>
Name: cursorInfoText (UnityEngine.GameObject)
module: Name: Canvas (UnityEngine.GameObject)
eventCamera:
sortOrderPriority: 0
renderOrderPriority: 0
module camera: null
distance: 0
index: 0
depth: 1
worldNormal: (0.0, 0.0, 0.0)
worldPosition: (0.0, 0.0, 0.0)
screenPosition: (746.0, 535.0)
module.sortOrderPriority: 0
module.renderOrderPriority: 0
sortingLayer: 0
sortingOrder: 0
<b>Press Rayast:</b>
順便說爲什麼有3個指針(-1,-2,-3),這是什麼意思?我讀過文檔中的某處,pointer -1
是鼠標左鍵,但我沒有點擊,所以這很奇怪。
我可以看到<b>pointerEnter</b>: cursorInfoText (UnityEngine.GameObject)
和Name: cursorInfoText (UnityEngine.GameObject)
這就是我所需要的。 但是,如何提取此信息?我已嘗試print(new PointerEventData(EventSystem.current).pointerEnter);
,但它是Null
無關緊要我進入或懸停。我在文檔中沒有看到其他適當的方法或屬性,儘管我可以看到信息已存儲。我錯過了什麼?我的目標是檢測鼠標是否在一些條件下懸停UI元素(忽略少量的UI元素(可能是標籤),不要忽略其餘元素(大多數元素)),所以我必須得到GameObject
本身。如果鼠標不懸停UI元素(除了那些少數),腳本與UI的東西無關。但不要忽視它是否在同一時間徘徊在其他幾個和其中一個之中。
我忘了交代我已經試過' EventSystem.current.currentSelectedGameObject'也是總是返回'Null'。我究竟做錯了什麼? – Necronomicron
@Necronomicron很好。我提供了2種解決方案的答案。爲什麼不使用第二種方法?這實際上是這樣做的方式。我正在用'IPointerEnterHandler'來談論答案。 – Programmer
我剛剛進入第二個解決方案的核心,但是我想知道爲什麼在第一個地方會出現'Null's? – Necronomicron