본문 바로가기
Project/[Unity] 옷입히기 게임

유니티 드래그 앤 드롭 방식 구현해보자

by 27일 낮 2022. 4. 5.
728x90

Drag & Drop .............

어떻게 구현하는거지?

 

그래서 유니티 공홈에 들어가서 검색했다.


EventTrigger.OnDrag (링크)

Called by the EventSystem every time the pointer is moved during dragging.

See EventTrigger for example usage.

 

 

 

예시가 두 개 나와있다.

첫번째 예시를 봐보자.

드래그를 하면 콘솔창에 뜨는 거 같다.

한번 해보자!

//Attach this script to the GameObject you would like to detect dragging on
//Attach an Event Trigger component to the GameObject (Click the Add Component button and go to Event>Event Trigger)
//Make sure the Camera you are using has a Physics Raycaster (Click the Add Component button and go to Event>Physics Raycaster) so it can detect clicks on GameObjects.

using UnityEngine;
using UnityEngine.EventSystems;

public class OnDragExample : MonoBehaviour
{
    void Start()
    {
        EventTrigger trigger = GetComponent<EventTrigger>();
        EventTrigger.Entry entry = new EventTrigger.Entry();
        entry.eventID = EventTriggerType.Drag;
        entry.callback.AddListener((data) => { OnDragDelegate((PointerEventData)data); });
        trigger.triggers.Add(entry);
    }

    public void OnDragDelegate(PointerEventData data)
    {
        Debug.Log("Dragging.");
    }
}

 

 

스크립트를 작성하고 아이템 중 하나에 할당해주었다.

그리고 Add Component 로 Event Trigger 를 추가해준다.

 

 

드래그한 만큼 콘솔창에 Dragging이라는 문구가 찍혔다!

꾹 눌렀더니 618개까지 알림이 떴다!

하지만 드래그만 감지될 뿐, 별다른 변화는 없었다!

 


이제 두번째 예시를 봐보자!!!!!!

 

The next example allows you to move a GameObject by dragging.

using UnityEngine;
using UnityEngine.EventSystems;

public class OnDragExample : MonoBehaviour
{
    void Start()
    {
        //Fetch the Event Trigger component from your GameObject
        EventTrigger trigger = GetComponent<EventTrigger>();
        //Create a new entry for the Event Trigger
        EventTrigger.Entry entry = new EventTrigger.Entry();
        //Add a Drag type event to the Event Trigger
        entry.eventID = EventTriggerType.Drag;
        //call the OnDragDelegate function when the Event System detects dragging
        entry.callback.AddListener((data) => { OnDragDelegate((PointerEventData)data); });
        //Add the trigger entry
        trigger.triggers.Add(entry);
    }

    public void OnDragDelegate(PointerEventData data)
    {
        //Create a ray going from the camera through the mouse position
        Ray ray = Camera.main.ScreenPointToRay(data.position);
        //Calculate the distance between the Camera and the GameObject, and go this distance along the ray
        Vector3 rayPoint = ray.GetPoint(Vector3.Distance(transform.position, Camera.main.transform.position));
        //Move the GameObject when you drag it
        transform.position = rayPoint;
    }
}

 

 

와 움직인다!!! 신난다 >.<!

 

 


이것을 이용하여 옷입히기 하는 방법은 2가지로 생각할 수 있다.

 

1. 사용자가 드래그 앤 드롭 한 곳 그대로, 아이템을 둔다. = 위치 자유로움

2. 아이템의 위치를 미리 정해둔 후, 사용자가 드래그해서 그 지점에 갖다대면 그 아이템이 지정 위치에 딱 맞게 떨어진다!

 

 

1번은 손댈 것없이 이대로 두고,

2번은 Collider를 사용하면 될 것 같다.

 

 

 

우선 기존에 사용한 버튼의 OnClick은 삭제하자!

이건 메뉴 팝업창 같은 게 뜨는 걸로 이용하면 될 것 같다.

 

 

+) 그리고 복원 버튼도 어서 만들어야겠다. 또는 리플레이 버튼?

 

 

 

댓글