Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
6
Help us understand the problem. What is going on with this article?
@simanezumi1989

Unityでドラッグ&ドロップ

More than 1 year has passed since last update.

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

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

6
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
simanezumi1989
大学時代に微分方程式の定性的理論に関する研究が国際誌に掲載せれたことが自慢です。

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
6
Help us understand the problem. What is going on with this article?