본문 바로가기

게임 개발 공부 정리

[Unity] UI 기본 - Rect Transform 이동 / UI 마우스 트랙킹

Canvas의 크기는 일반적으로 해상도 전체의 크기를 전제로 하게 된다.

Rect Transform에는 일반 트랜스폼 컴포지션과 다르게 앵커가 있는데, 앵커를 기준으로 UI를 배치하게 된다.

- stetch : 선택한 가로/세로 캔버스와의 비율을 설정한다.

Pivot : UI상에 오브젝트를 배치할때 오브젝트 내부의 기준이다.

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UIController : MonoBehaviour
{
    public RectTransform rectTransform;


    void Start()
    {
        rectTransform = this.GetComponent<RectTransform>();    
    }

    void Update()
    {
        if(Input.GetMouseButton(0))
		{
            rectTransform.anchoredPosition = Input.mousePosition;
		}
    }
}

 

마우스의 포지션UI 앵커드 포지션이 같도록 할려면 앵커드 포지션의 기준이 Botton / Left 여야 한다.