Unity/참고자료

IsPointerOverGameObject / RayCast가 UI를 통과하지 않게하기

치명적흑형 2021. 10. 20. 10:06

EventSystem.IsPointerOverGameObject

 

IsPointerOverGameObject() 라는 함수는 UI가 클릭되면 true를 아니면 false를 반환한다.

 

UI에 있는 오브젝트를 클릭했을 때 다른 클릭 이벤트가 발생하지 않도록 해야할 때가 있다. 예를 들어, 스타크래프트에서 UI를 클릭했는데 유닛이 움직이거나 공격한다면 곤란할 것이다.

 


Unity에서는 Raycast를 통해 클릭이벤트를 처리한다. 그럼 UI에서도 Raycast가 가능하게 설정한 후에 UI가 클릭되었을 때는 다른 클릭이 처리되지 않게하면 된다.

 
IsPointerOverGameObject() 라는 함수는 UI가 클릭되면 true를 아니면 false를 반환한다.
이를 이용하여 다른 부분들을 처리하면 된다.

출처
https://ladun.tistory.com/32

 

사용법

using UnityEngine.EventSystems;
if(Input.GetMouseButtonDown(0)) 
{
	if(!EventSystem.current.IsPointerOverGameObject())
	{  
	         //클릭 처리
	}	
}

 


https://docs.unity3d.com/kr/530/ScriptReference/EventSystems.EventSystem.IsPointerOverGameObject.html

 

Unity - 스크립팅 API: EventSystems.EventSystem.IsPointerOverGameObject

If you use IsPointerOverGameObject() without a parameter, it points to the "left mouse button" (pointerId = -1); therefore when you use IsPointerOverGameObject for touch, you should consider passing a pointerId to it.

docs.unity3d.com

 

 


 

 

참고 블로그

 

https://sangh518.github.io/record/block-event-when-ui-click/

 

https://m.blog.naver.com/yoohee2018/220839396232