12
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Unityでドラッグ&ドロップ

Last updated at Posted at 2019-04-02

ドラッグしたいオブジェクトにつける(カードゲームならカード)

DragObj.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class DragObj : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler {
	public Transform parentTransform;
	public void OnBeginDrag(PointerEventData data){
		Debug.Log("OnBeginDrag");
		GetComponent<CanvasGroup>().blocksRaycasts = false;
		parentTransform = transform.parent;
		transform.SetParent(transform.parent.parent);

	}
	public void OnDrag(PointerEventData data){
		transform.position = data.position;
    }
	public void OnEndDrag(PointerEventData data){
		Debug.Log("OnEndDrag");
		transform.SetParent(parentTransform);
		GetComponent<CanvasGroup>().blocksRaycasts = true;
	}	
}

ドラッグしたいオブジェクトにCanvasGroupをつける()
スクリーンショット 2019-04-02 20.10.14.png

ドロップしたいオブジェクトにつける(カードゲームならフィールドなど)

DropArea.cs
using UnityEngine;
using UnityEngine.EventSystems;

public class DropArea : MonoBehaviour, IDropHandler
{
    public void OnDrop(PointerEventData data){
		Debug.Log(gameObject.name);
		
		DragObj dragObj = data.pointerDrag.GetComponent<DragObj>();
		if(dragObj != null){
			dragObj.parentTransform = this.transform;
			Debug.Log(gameObject.name+"に"+data.pointerDrag.name+"をドロップ");
		}		
    }
}

スタジオしまづでゲームの作り方を学びたい人向けのサロン▶︎https://camp-fire.jp/projects/view/149191

12
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?