A-Dyu의 개발 일기장

[유니티] 마우스 포인터가 UI위에 올려져 있는지 본문

유니티/유니티 기능

[유니티] 마우스 포인터가 UI위에 올려져 있는지

ADyu 2024. 5. 21. 21:54

유니티에서 UI를 클릭할 떈 마우스 입력을 받지 않아야 할 때가 있다.

 

그럴 때 EventSystem의 IsPointerOverGameObject메서드를 쓰면 된다.

IsPointerOverGameObject는 EventSystem object위에 있다면 ture를 반환한다.

using UnityEngine;
using UnityEngine.EventSystems;

public class Test : MonoBehaviour
{
    void Update()
    {
        //UI 위에 마우스가 올려져 있다면 ture
        Debug.Log(EventSystem.current.IsPointerOverGameObject());
    }
}

 

IsPointerOverGameObject는 인자로 pointerid를 받는데, 기본값이 -1이다.그러므로 0부터 시작하는 모바일이나 아이패드의 터치는 pointerid를 넘겨주어야 한다.

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class Test : MonoBehaviour {

	void Update () 
	{
		// 화면을 터치했는지
		if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
		{
			// 터치했을 때, 터치 위치가 UI 위라면 ture
            	Debug.Log(EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId));
		}	
	}
}