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를 연결
재생하고 동작 확인
Reference
이 문제에 관하여(Unity image 드래그 앤 드롭), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/bu-ta/items/c7e5ad130eb491d525bf텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)