ShovelingLife
A Game Programmer
ShovelingLife
전체 방문자
오늘
어제
  • 분류 전체보기 (1072)
    • 그래픽스 (57)
      • 공통 (19)
      • 수학 물리 (22)
      • OpenGL & Vulkan (1)
      • DirectX (14)
    • 게임엔진 (183)
      • Unreal (69)
      • Unity (103)
      • Cocos2D-X (3)
      • 개인 플젝 (8)
    • 코딩테스트 (221)
      • 공통 (7)
      • 프로그래머스 (22)
      • 백준 (162)
      • LeetCode (19)
      • HackerRank (2)
      • 코딩테스트 알고리즘 (8)
    • CS (235)
      • 공통 (21)
      • 네트워크 (44)
      • OS & 하드웨어 (55)
      • 자료구조 & 알고리즘 (98)
      • 디자인패턴 (6)
      • UML (4)
      • 데이터베이스 (7)
    • 프로그래밍 언어 (348)
      • C++ (167)
      • C# (90)
      • Java (9)
      • Python (33)
      • SQL (30)
      • JavaScript (8)
      • React (7)
    • 그 외 (9)
      • Math (5)
      • 일상 (5)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

  • Source Code 좌측 상단에 복사 버튼 추가 완료
  • 언리얼 엔진 C++ 빌드시간 단축 꿀팁
  • 게임 업계 코딩테스트 관련
  • 1인칭 시점으로 써내려가는 글들

인기 글

태그

  • SQL
  • string
  • C++
  • 그래픽스
  • 포인터
  • 티스토리챌린지
  • c#
  • 문자열
  • 알고리즘
  • 언리얼
  • C
  • 프로그래머스
  • 배열
  • 오블완
  • 함수
  • 백준
  • 파이썬
  • Unity
  • 클래스
  • 유니티

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
ShovelingLife

A Game Programmer

게임엔진/Unity

[Unity] 이미지 피벗 기준 클릭 위치 구하기 0.0 ~ 1.0

2024. 12. 17. 20:22
// Take an image and a screenPosition. Return the ptLocationRelativeToImage01 relative to the image (where x,y between 0.0 and 1.0 are on the image, values below 0.0 or above 1.0 are outside the image).
public static bool GetPositionOnImage01 (
	UnityEngine.UI.Image uiImageObject,
	Vector2 screenPosition,
	out Vector2 ptLocationRelativeToImage01 )
{
	ptLocationRelativeToImage01 = new Vector2();
	RectTransform uiImageObjectRect = uiImageObject.GetComponent<RectTransform> ();

	Vector2 localCursor = new Vector2();
	if (RectTransformUtility.ScreenPointToLocalPointInRectangle (
		uiImageObjectRect,
		screenPosition,
		null,
		out localCursor))
	{
		Vector2 ptPivotCancelledLocation = new Vector2( localCursor.x - uiImageObjectRect.rect.x, localCursor.y - uiImageObjectRect.rect.y );
		Vector2 ptLocationRelativeToImageInScreenCoordinates = new Vector2();

		// How do we get the location of the image? Calculate the size of the image, then use the pivot information.
		if( uiImageObject.preserveAspect )
		{
			// If we are preserving the aspect ratio of the image, then we need to do some additional calculations
			// Figure out if the image constrained by height or by width.
			float fImageAspectRatio = uiImageObject.sprite.rect.height / uiImageObject.sprite.rect.width;
			float fRectAspectRatio = uiImageObjectRect.rect.height / uiImageObjectRect.rect.width;

			Rect imageRectInLocalScreenCoordinates = new Rect();
			if( fImageAspectRatio > fRectAspectRatio )
			{
				// The image is constrained by its height.
				float fImageWidth = (fRectAspectRatio/fImageAspectRatio) * uiImageObjectRect.rect.width;
				float fExcessWidth = uiImageObjectRect.rect.width - fImageWidth;
				imageRectInLocalScreenCoordinates.Set( uiImageObjectRect.pivot.x*fExcessWidth,0, uiImageObjectRect.rect.height/fImageAspectRatio, uiImageObjectRect.rect.height);
			}
			else
			{
				// The image is constrained by its width.
				float fImageHeight = (fImageAspectRatio/fRectAspectRatio) * uiImageObjectRect.rect.height;
				float fExcessHeight = uiImageObjectRect.rect.height - fImageHeight;
				imageRectInLocalScreenCoordinates.Set( 0,uiImageObjectRect.pivot.y*fExcessHeight, uiImageObjectRect.rect.width, fImageAspectRatio*uiImageObjectRect.rect.width );
			}
			ptLocationRelativeToImageInScreenCoordinates.Set( ptPivotCancelledLocation.x - imageRectInLocalScreenCoordinates.x, ptPivotCancelledLocation.y - imageRectInLocalScreenCoordinates.y );
			ptLocationRelativeToImage01.Set( ptLocationRelativeToImageInScreenCoordinates.x / imageRectInLocalScreenCoordinates.width, ptLocationRelativeToImageInScreenCoordinates.y / imageRectInLocalScreenCoordinates.height );
		}
		else
		{
			ptLocationRelativeToImageInScreenCoordinates.Set( ptPivotCancelledLocation.x, ptPivotCancelledLocation.y );
			ptLocationRelativeToImage01.Set( ptLocationRelativeToImageInScreenCoordinates.x / uiImageObjectRect.rect.width, ptLocationRelativeToImageInScreenCoordinates.y / uiImageObjectRect.rect.height );
		}
		return true;
	}
	return false;
}

 

https://discussions.unity.com/t/find-x-y-cordinates-of-click-on-ui-image-new-gui-system/129700/3

저작자표시 (새창열림)

'게임엔진 > Unity' 카테고리의 다른 글

[Unity] 6 버전 UI 카메라 설정하는 방법  (0) 2025.05.13
[Unity] 2D Pixel Perfect  (0) 2025.05.13
[Unity] PointEventData 클래스  (0) 2024.12.17
[Unity] 게임 엔진에서 컴포넌트 찾기 FindObjectsByType VS FindObjectsOfType  (0) 2024.12.16
[Unity] 카메라 스테이킹 (Camera Staking)  (0) 2024.12.12
    '게임엔진/Unity' 카테고리의 다른 글
    • [Unity] 6 버전 UI 카메라 설정하는 방법
    • [Unity] 2D Pixel Perfect
    • [Unity] PointEventData 클래스
    • [Unity] 게임 엔진에서 컴포넌트 찾기 FindObjectsByType VS FindObjectsOfType
    ShovelingLife
    ShovelingLife
    Main skill stack => Unity C# / Unreal C++ Studying Front / BackEnd, Java Python

    티스토리툴바