2016-07-23 9 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つと残りの1つのいずれかを乗せているなら、無視しないでください。

答えて

1

しかし、正確にどのGameObjestがわかりますか?

あなたはEventSystem.current.currentSelectedGameObjectを探しています。このよう

:私は `試した言及するのを忘れてしまった

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

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

それともIPointerEnterHandlereventData.pointerCurrentRaycastで:タグことで

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

チェックEventSystem.current.currentSelectedGameObject'もそうです常に 'Null'も返します。私は間違って何をしていますか? – Necronomicron

+0

@Necronomicronいいですね。私は2つのソリューションで答えを提供しました。なぜ2番目の方法を使用しないのですか?それが実際にこれを行う方法です。私は 'IPointerEnterHandler'で答えを話しています。 – Programmer

+0

私は2番目のソリューションの中核に着きましたが、なぜ1番目の場所にヌルがあるのだろうと思いますか? – Necronomicron

関連する問題