EventSystem.IsPointerOverGameObject
public bool IsPointerOverGameObject ();
public bool IsPointerOverGameObject (int pointerId);
Parameters
pointerId | Pointer (touch / mouse) ID. |
Description
Is the pointer with the given ID over an EventSystem object?
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class MouseExample : MonoBehaviour
{
void Update()
{
// Check if the left mouse button was clicked
if (Input.GetMouseButtonDown(0))
{
// Check if the mouse was clicked over a UI element
if (EventSystem.current.IsPointerOverGameObject())
{
Debug.Log("Clicked on the UI");
}
}
}
}
If you use IsPointerOverGameObject() without a parameter, it points to the "left mouse button" (pointerId = -1);
Default 값이 왼쪽 마우스 클릭 시(-1) 인 경우이다. 매개변수가 필요없다.
when you use IsPointerOverGameObject for touch, you should consider passing a pointerId to it.
터치를 사용할 때는 pointerId 를 사용해야한다.
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class TouchExample : MonoBehaviour
{
void Update()
{
// Check if there is a touch
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
// Check if finger is over a UI element
if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
{
Debug.Log("Touched the UI");
}
}
}
}
Note that for touch, IsPointerOverGameObject should be used with OnMouseDown() or Input.GetMouseButtonDown(0) or Input.GetTouch(0).phase == TouchPhase.Began.
터치에서 쓸수 있는 함수 예제들.
'Unity' 카테고리의 다른 글
[Unity] transform Component 사용 (0) | 2019.10.30 |
---|---|
[Unity] Json을 이용한 저장/불러오기 방법 (0) | 2019.10.30 |
[Unity] Script 내에서 쓰이는 대괄호(속성) 정리 (0) | 2019.10.29 |
[Unity] 유용한 함수들 ( Mathf, Lerp, Slerp, Trigger.. 관련 ) 정리 (0) | 2019.10.25 |
[Unity] TextMeshPro - text (영어+한글지원) 생성하기 (0) | 2019.10.24 |