클릭한 위치가 특정 오브젝트 안에 속해 있는지 판단하는 방법

2023. 10. 4. 12:31·Unity/Tip
반응형

소개

Unity에서 RectTransform 객체가 화면상의 특정 좌표를 포함하는지 여부를 확인하려면 RectTransformUtility.RectangleContainsScreenPoint 함수를 사용할 수 있습니다. 이 함수는 RectTransform 객체의 좌표, 크기, 회전각도, 그리고 화면상의 좌표를 입력으로 받아 RectTransform 객체가 화면상의 좌표를 포함하는지 여부를 반환합니다.

 

사용법

RectTransformUtility.RectangleContainsScreenPoint 함수는 다음과 같이 사용할 수 있습니다.

bool contains = RectTransformUtility.RectangleContainsScreenPoint(rectTransform, screenPoint);

여기서 rectTransform은 검사할 RectTransform 객체, screenPoint는 검사할 화면상의 좌표입니다.

 

반환값

RectTransformUtility.RectangleContainsScreenPoint 함수는 RectTransform 객체가 화면상의 좌표를 포함하는지 여부를 boolean 값으로 반환합니다.

 

예제

다음은 RectTransformUtility.RectangleContainsScreenPoint 함수를 사용하는 예제입니다.

// RectTransform 객체 생성
RectTransform rectTransform = new RectTransform();
rectTransform.position = new Vector3(0, 0, 0);
rectTransform.sizeDelta = new Vector2(100, 100);

// 화면상의 좌표
Vector2 screenPoint = new Vector2(0, 0);

// RectTransform 객체가 화면상의 좌표를 포함하는지 확인
bool contains = RectTransformUtility.RectangleContainsScreenPoint(rectTransform, screenPoint);

// 결과 출력
Debug.Log("RectTransform 객체가 화면상의 좌표를 포함합니다: " + contains);

이 예제에서는 RectTransform 객체의 위치와 크기를 초기화하고, 화면상의 좌표를 지정합니다. 그런 다음, RectTransformUtility.RectangleContainsScreenPoint 함수를 사용하여 RectTransform 객체가 화면상의 좌표를 포함하는지 확인합니다. 결과는 true로 반환됩니다.

 

추가 기능

RectTransformUtility.RectangleContainsScreenPoint 함수는 다음과 같은 추가 기능을 제공합니다.

  • 레이어: 레이어를 지정하여 RectTransform 객체가 특정 레이어의 화면상의 좌표를 포함하는지 여부를 확인할 수 있습니다.
  • 투명도: 투명도를 지정하여 RectTransform 객체가 투명한 경우에도 화면상의 좌표를 포함하는지 여부를 확인할 수 있습니다.

 

결론

RectTransformUtility.RectangleContainsScreenPoint 함수는 RectTransform 객체가 화면상의 특정 좌표를 포함하는지 여부를 확인하는 데 유용한 함수입니다. 이 함수를 사용하여 게임에서 다양한 인터페이스 요소의 위치를 검사하는 등의 작업을 수행할 수 있습니다.

저작자표시 비영리 변경금지 (새창열림)

'Unity > Tip' 카테고리의 다른 글

Unity에서 게임 오브젝트의 Null 체크 속도 비교  (0) 2023.10.30
IEnumerator 메서드에서 async await 사용  (0) 2023.10.24
Animation Preview Auto Play  (0) 2023.09.11
~~~ is missing the class attribute 'ExtensionOfNativeClass'!  (0) 2023.08.21
오브젝트 자연스럽게 이동하기 : Mathf.PingPong  (0) 2023.08.04
'Unity/Tip' 카테고리의 다른 글
  • Unity에서 게임 오브젝트의 Null 체크 속도 비교
  • IEnumerator 메서드에서 async await 사용
  • Animation Preview Auto Play
  • ~~~ is missing the class attribute 'ExtensionOfNativeClass'!
코샵
코샵
나의 코딩 일기장
    반응형
  • 코샵
    끄적끄적 코딩 공방
    코샵
    • 분류 전체보기 (725)
      • 스마트팜 (0)
      • 상품 추천 (223)
      • MongoDB (4)
      • 하드웨어 (17)
      • 일기장 (4)
      • 파이썬 (130)
        • Basic (41)
        • OpenCV (8)
        • Pandas (15)
        • PyQT (3)
        • SBC(Single Board Computer) (1)
        • 크롤링 (14)
        • Fast API (29)
        • Package (6)
      • Unity (138)
        • Tip (41)
        • Project (1)
        • Design Pattern (8)
        • Firebase (6)
        • Asset (2)
      • Linux (4)
      • C# (97)
        • Algorithm (11)
        • Window (7)
      • TypeScript (51)
        • CSS (10)
      • Git (11)
      • SQL (5)
      • Flutter (10)
        • Tip (1)
      • System (1)
      • BaekJoon (6)
      • Portfolio (2)
      • MacOS (1)
      • 유틸리티 (1)
      • 서비스 (6)
      • 자동화 (3)
      • Hobby (10)
        • 물생활 (10)
        • 식집사 (0)
  • 인기 글

  • 태그

    스크립트 실행
    쇼핑몰리뷰
    스크립트 실행 순서
    리뷰관리
    appdevelopment
    리뷰이관
    리스트
    C#
    믈레코비타멸균우유
    Python
    codingtips
    긴유통기한우유
    라떼우유
    learntocode
    cv2
    devlife
    카페24리뷰이관
    카페24리뷰
    rtsp
    codingcommunity
    ipcamera
    programmerlife
    상품 리뷰 크롤링
    파이썬
    programming101
    유니티
    스마트스토어리뷰
    list
    unity
    셀레니움
  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
코샵
클릭한 위치가 특정 오브젝트 안에 속해 있는지 판단하는 방법
상단으로

티스토리툴바