본문 바로가기

카테고리 없음

[Unity] UI 1 - Rect 변수형

ㅇRect

Vector3, 2와 같은 위치 데이터 타입이다.

출저: 유니티 도큐멘트

 

씬의 모든 오브젝트는 트랜스폼을 가진다.

설령 캔버스의 유아이라 할지라도 가지고 있다.

화면 상의 위치뿐만 아니라 하이라키의 계층 구조에서의 위치도 담고 있다.

Transform child. 에서 gameObject를 해서 오브젝트를 구할수도, position을 적어서 벡터 위치 값을 구할수도 있는것이다.

 

Slot이라는 오브젝트에 해당 스크립트가 들어가 있고, Slot 오브젝트의 자식으로 이미지 오브젝트가 들어가있다.

이런 방식으로 액세스 하여 지우는 것이다.

 

Image 컴포넌트를 불러오기 위해선, Using UI를 해줘야만 한다.

 

드래그앤드롭

더보기
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
//클릭 관련 이벤트를 상속받기 위함이다.

//이렇게 각 드래그 별로 클래스를 상속 받는다.
public class DND : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IDropHandler
{
    //이하에 나오는 함수들은 각 클래스별의 함수이다.

    //드래그를 시작할때 한번 호출된다.
    public void OnBeginDrag(PointerEventData eventData)
    {
        //오류가 났을때 시스템 디버깅 창에 메시지를 throw하는 코드이다. 
        //딱히 필요없어서 나중에 나오는 코드도 주석 처리한다.
        //throw new System.NotImplementedException();
        Debug.Log("Begin Drag");
    }

    //드래그를 시작한 후 마우스가 움직일때마다 실행된다.
    public void OnDrag(PointerEventData eventData)
    {
        //throw new System.NotImplementedException();
        Debug.Log("Drag");
        transform.position = eventData.position;

    }
    
    //오직 이미지 영역 내에서만 마우스가 up되었을때 호출된다.
    public void OnDrop(PointerEventData eventData)
    {
        //throw new System.NotImplementedException();
        Debug.Log("Drop");
    }

    //이미지 영역과 상관없이 게임 창 외부를 포함하여 마우스를 놓았을때 호출되는 함수이다.
    public void OnEndDrag(PointerEventData eventData)
    {
        //throw new System.NotImplementedException();
        Debug.Log("End Drag");
    }