2016-07-23 3 views
0

마우스가 UI 요소를 가리키고 있는지 감지 할 수 있습니다. EventSystem.current.IsPointerOverGameObject().포인터가 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); 사용하여 필요한 정보를 볼 수 있습니다 왜 3 포인터가 그런데

<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> 

를 (-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입니다. 이처럼

: 나는`해봤 말할 것도 잊어 버린

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 가지 해결책을 제시했다. 두 번째 방법을 사용하지 않는 이유는 무엇입니까? 그것은 실제로 이것을하는 방법입니다. 나는 IPointerEnterHandler로 대답을 이야기하고있다. – Programmer

+0

두 번째 솔루션의 핵심에 다가 가고 있습니다.하지만 왜 1 번에 '널 (null)'이 있는지 궁금합니다. – Necronomicron