Unity image 드래그 앤 드롭

15366 단어 Unity

이미지 드래그 앤 드롭



Drag.cs, Drop.cs, UISample.cs 만들고 아래 코드 작성

드래그
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.EventSystems;

[RequireComponent(typeof(CanvasGroup))]
public class Drag : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{

    //ドラッグ開始時処理
    public Action OnBeginDragAction;
    //ドラッグ中処理
    public Action OnDragAction;
    //ドラッグ終了処理
    public Action OnEndDragAction;

    private CanvasGroup _canvasGroup;
    private Vector3 _startPosition;
    private Camera _uiCamera;

    private void Awake()
    {
        _canvasGroup = gameObject.GetComponent<CanvasGroup>();
    }

    private void OnDestroy()
    {
        OnBeginDragAction = null;
        OnDragAction = null;
        OnEndDragAction = null;
        _uiCamera = null;
    }

    public void SetScreenSpaceCamera(Camera uiCamera)
    {
        _uiCamera = uiCamera;
    }

    public void OnBeginDrag(PointerEventData pointerEventData)
    {
        if (enabled == false)
        {
            return;
        }

        //ドロップ判定が取れるように
        _canvasGroup.blocksRaycasts = false;

        _startPosition = transform.position;

        if (OnBeginDragAction != null)
        {
            OnBeginDragAction();
        }
    }

    public void OnDrag(PointerEventData pointerEventData)
    {
        if (enabled == false)
        {
            return;
        }

        if (_uiCamera != null)
        {
            var position = _uiCamera.ScreenToWorldPoint(pointerEventData.position);
            position.z = transform.position.z;
            transform.position = position;
        }
        else
        {
            transform.position = pointerEventData.position;
        }

        if (OnDragAction != null)
        {
            OnDragAction();
        }
    }

    public void OnEndDrag(PointerEventData pointerEventData)
    {
        _canvasGroup.blocksRaycasts = true;

        transform.position = _startPosition;

        if (enabled == false)
        {
            return;
        }

        if (OnEndDragAction != null)
        {
            OnEndDragAction();
        }
    }

}

Drop
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.EventSystems;

public class Drop : MonoBehaviour, IDropHandler, IPointerEnterHandler, IPointerExitHandler
{
    //ドロップ範囲に入ったときの処理
    public Action OnPointerEnterAction;
    //ドロップ範囲内から出たときの処理
    public Action OnPointerExitAction;
    //ドロップ時処理
    public Action OnDropAction;

    private void OnDestroy()
    {
        OnPointerEnterAction = null;
        OnPointerExitAction = null;
        OnDropAction = null;
    }

    public void OnPointerEnter(PointerEventData pointerEventData)
    {
        if (OnPointerEnterAction != null)
        {
            OnPointerEnterAction();
        }
    }

    public void OnPointerExit(PointerEventData pointerEventData)
    {
        if (OnPointerExitAction != null)
        {
            OnPointerExitAction();
        }
    }

    public void OnDrop(PointerEventData pointerEventData)
    {
        if (OnDropAction != null)
        {
            OnDropAction();
        }
    }
}

UISample
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIsample : MonoBehaviour
{
    [SerializeField]
    private Drag _drag;
    [SerializeField]
    private Drop _drop;

    //Use this for intialization
    void Start()
    {
        _drop.OnDropAction = () =>
        {
            var dropImage = _drop.GetComponent<Image>();
            var dragImage = _drag.GetComponent<Image>();
            dropImage.color = dragImage.color;
        };
    }

}

두 개의 이미지를 만들고 Canvas에 UISample.cs를 구성합니다.


Drop image에 Drop.cs 구성 요소


Dragimage에서 Drag.cs를 구성하여 색상 변경


Canvas의 UISample에 Dragimage와 Drop image를 연결


재생하고 동작 확인

좋은 웹페이지 즐겨찾기