2016-08-25 3 views
3

스크립트에서 EventSystem.current.IsPointerOverGameObject를 사용 중입니다. UI 아래에 UI/EventSystem 객체가 없다고 맹세하더라도 Unity는 True를 반환합니다.EventSystem.current.IsPointerOverGameObject가 어떤 개체인지 어떻게 알 수 있습니까?

EventSystem이 탐지하는 개체에 대한 정보를 어떻게 찾을 수 있습니까? 당신의 update()에서

+0

나는 경우에도 믿을 수 없어 ...

private static StandaloneInputModuleV2 currentInput; private StandaloneInputModuleV2 CurrentInput { get { if (currentInput == null) { currentInput = EventSystem.current.currentInputModule as StandaloneInputModuleV2; if (currentInput == null) { Debug.LogError("Missing StandaloneInputModuleV2."); // some error handling } } return currentInput; } } 

:

사용법은 간단합니다, 그냥 기본 사람이 같은 코드에 상기의 EventSystem 장면 및 참조에 추가 교체 맹세하니**. 나에게 증거를 보여줘. –

+0

JK! 내 대답을 확인, 그것이 도움이되기를 바랍니다 –

답변

1

나는이 스레드 우연히 제공된 솔루션을 아주 행복하지 않았다, 그래서 여기에 공유하는 다른 구현의 :

public class StandaloneInputModuleV2 : StandaloneInputModule 
{ 
    public GameObject GameObjectUnderPointer(int pointerId) 
    { 
     var lastPointer = GetLastPointerEventData(pointerId); 
     if (lastPointer != null) 
      return lastPointer.pointerCurrentRaycast.gameObject; 
     return null; 
    } 

    public GameObject GameObjectUnderPointer() 
    { 
     return GameObjectUnderPointer(PointerInputModule.kMouseLeftId); 
    } 
} 

GameObject의 이름을 보여주는 EventSystem의 편집기 출력을 살펴보면 일부 기능이 이미 있으며 사용 방법이 있어야한다고 결정했습니다. 앞에서 언급 한 EventSystemIsPointerOverGameObject (PointerInputModule에서 덮어 쓴 것)의 opened source으로 다이빙을 한 후 기본값 StandaloneInputModule을 더 쉽게 확장 할 수있을 것으로 생각했습니다.

var currentGO = CurrentInput.GameObjectUnderPointer(); 
5

:

if(Input.GetMouseButton(0)) 
{ 
    PointerEventData pointer = new PointerEventData(EventSystem.current); 
    pointer.position = Input.mousePosition; 

    List<RaycastResult> raycastResults = new List<RaycastResult>(); 
    EventSystem.current.RaycastAll(pointer, raycastResults); 

    if(raycastResults.Count > 0) 
    { 
     foreach(var go in raycastResults) 
     { 
      Debug.Log(go.gameObject.name,go.gameObject); 
     } 
    } 
} 
+0

완벽, 고마워! UI 요소를 찾은 다음 마우스 클릭을 무시하도록 설정된 옵션이있는 CanvasGroup 구성 요소를 사용하여 GameObject에 부모를 추가 할 수있었습니다. –

0

게임 오브젝트 EventSystemIPointerClickHandler을 구현하고 OnPointerEnter 기능을 재정 의하여 감지되는에 대해 당신은 더 많은 정보를 찾을 수 있습니다.

, 당신은 감지 등의 이미지/RawImage 등으로 UI 구성 요소를 원하는이 경우,이 그것을 수행해야합니다

using UnityEngine.EventSystems; 
public class MouseEnterScript: MonoBehaviour, IPointerEnterHandler 
{ 
    public void OnPointerEnter(PointerEventData eventData) 
    { 
     Debug.Log("Name: " + eventData.pointerCurrentRaycast.gameObject.name); 
    } 
} 

을 이것은 충돌 장치 (3D)와 메쉬 경우 등을 상자 Collider, PhysicsRaycaster을 카메라에 추가 한 다음이 대답의 첫 번째 코드를 사용하여 마우스가 끝난 게임 객체를 찾습니다. 이 후 마우스가

이상
void Start() 
{ 
    Camera.main.gameObject.AddComponent<Physics2DRaycaster>(); 
} 
이다 게임 오브젝트하는 감지이 답변의 첫 번째 코드를 사용하여 카메라에 Physics2DRaycaster 추가, 2 차원 충돌 장치 등 상자 충돌 장치 2DSpriteRenderer 경우

void Start() 
{ 
    Camera.main.gameObject.AddComponent<PhysicsRaycaster>(); 
} 

관련 문제