imageをドラッグ&ドロップする
Drag.cs、Drop.cs、UISample.cs作成し、下記のコードを書く
Drag
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;
};
}
}
imageを2つ作成し、CanvasにUISample.csをコンポーネントする
DragimageにDrag.csをコンポーネントして、カラーを変更する
CanvasのUISampleにDragimageとDrop imageをアタッチする
再生し、動作確認する