2016-07-23 28 views
0

我可以通過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的東西無關。但不要忽視它是否在同一時間徘徊在其他幾個和其中一個之中。

回答

1

但是我怎麼知道它究竟是哪個GameObjest?

您正在尋找EventSystem.current.currentSelectedGameObject

像這樣:

if (EventSystem.current.IsPointerOverGameObject()) 
{ 
    Debug.Log("Mouse Over: " + EventSystem.current.currentSelectedGameObject.name); 
} 

檢查由標籤

if (EventSystem.current.IsPointerOverGameObject() && EventSystem.current.currentSelectedGameObject.CompareTag("yourTagName")) 
{ 

} 

或用IPointerEnterHandlereventData.pointerCurrentRaycast

using UnityEngine.EventSystems; 
public class Test : MonoBehaviour, IPointerEnterHandler 
{ 

    public void OnPointerEnter(PointerEventData eventData) 
    { 
     if (eventData.pointerCurrentRaycast.gameObject != null) 
     { 
      Debug.Log("Mouse Over: " + eventData.pointerCurrentRaycast.gameObject.name); 
     } 
    } 
} 
+0

我忘了交代我已經試過' EventSystem.current.currentSelectedGameObject'也是總是返回'Null'。我究竟做錯了什麼? – Necronomicron

+0

@Necronomicron很好。我提供了2種解決方案的答案。爲什麼不使用第二種方法?這實際上是這樣做的方式。我正在用'IPointerEnterHandler'來談論答案。 – Programmer

+0

我剛剛進入第二個解決方案的核心,但是我想知道爲什麼在第一個地方會出現'Null's? – Necronomicron

相關問題