본문 바로가기
프로그래밍

유니티 UI 클릭했을 때, 클릭된 위치 값 가져오기

by 어부 2019. 11. 7.

 

300x300 크기의 이미지에서 정중앙에 클릭하면 150,150의 좌표를 가져옴(피벗이 0,0 기준)

왼쪽 최하단을 클릭하면 0,0의 좌표를 가져옴.

public void OnPointerClick(PointerEventData eventData)
{
	if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(GetComponent<RectTransform>(), 
		eventData.position, eventData.pressEventCamera, out Vector2 localCursor))
		return;

	Debug.Log("LocalCursor:" + localCursor);
}

 

출처: https://answers.unity.com/questions/892333/find-xy-cordinates-of-click-on-uiimage-new-gui-sys.html