
팀 과제 시작..
그리고 아이앱 반팔 당첨됐다! 🐧
하지만
작업하던게 맘에 안들어서 여러번 날리고, 다시 태초마을로 돌아왔다...
Unity에서는 드래그를 할 수 있는 기능이 이미 인터페이스로 구현되어있다!
IBeginDragHandler는 드래그 시작, -> OnBeginDrag()
IDragHandler는 드래그 상태, -> OnDrag()
IEndDragHandler는 드래그 끝을 의미한다. -> OnEndDrag()
따라서, 지정해준 변수를 가지고 자유롭게 드래그 상태에서 행동을 정의할 수 있다!
Drop도 마찬가지다. IDopHandler는 드래그 후 놓아지는 상태를 인터페이스로 구현되어있는데,
OnDrop() 메서드를 이용해 매개변수를 활용하여 이벤트를 처리할 수 있다! 매우 편리하다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class Draggable : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
    [SerializeField] public int from;
    private RectTransform rectTransform;
    private CanvasGroup canvasGroup;
    private Vector3 originPositions;
    public void OnBeginDrag(PointerEventData eventData)
    {
        //Debug.Log("OnBeginDrag");
        canvasGroup.blocksRaycasts = false;
        canvasGroup.alpha = 0.7f;
    }
    public void OnDrag(PointerEventData eventData)
    {
        rectTransform.anchoredPosition += eventData.delta;
        //Debug.Log("OnDrag");
    }
    public void OnEndDrag(PointerEventData eventData)
    {
        //Debug.Log("OnEndDrag");
        rectTransform.anchoredPosition = originPositions;
        canvasGroup.blocksRaycasts = true;
        canvasGroup.alpha = 1.0f;
    }
    public void SetOriginalPosition(Vector3 newPositions)
    {
        originPositions = newPositions;
    }
    void Start()
    {
        rectTransform = GetComponent<RectTransform>();
        canvasGroup = GetComponent<CanvasGroup>();
        originPositions = rectTransform.anchoredPosition;
    }
    void Update()
    {
        
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class Droppable : MonoBehaviour, IDropHandler
{
    [SerializeField] private int to;
    public void OnDrop(PointerEventData eventData)
    {
        Draggable dragged = eventData.pointerDrag.GetComponent<Draggable>();
        switch (dragged.from)
        {
            case 1:
                switch (to)
                {
                    case 1:
                        Destroy(dragged);
                        break;
                }
                break;
        }
        Debug.Log(string.Format("dragged {0} to {1}", dragged.from, to));
        dragged.SetOriginalPosition(GetComponent<RectTransform>().anchoredPosition);
    }
    // Start is called before the first frame update
    void Start()
    {
        
    }
    // Update is called once per frame
    void Update()
    {
        
    }
}
타워 객체 생성하기