微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Unity3D Drag and Drop

Unity3D Drag and Drop

Drag_2D

using UnityEngine;

public class Drag_2D : MonoBehavIoUr
{
    #region VERSION 1

    //[Serializefield] private bool isSelected;//MARKER Default Value is False

    //private void onm ouSEOver()
    //{
    //    if (Input.GetMouseButtonDown(0))
    //        isSelected = true;
    //    if (Input.GetMouseButtonUp(0))
    //        isSelected = false;
    //}

    //private void Update()
    //{
    //    //Debug.Log(Input.mousePosition.x + "-" +  Input.mousePosition.y);//MARKER Watch first

    //    if (isSelected)
    //    {
    //        Vector2 cursorWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    //        transform.position = new Vector2(cursorWorldPos.x, cursorWorldPos.y);
    //    }
    //}

    #endregion

    #region VERSION 2

    [Serializefield] private bool isSelected;

    private void onm ouseDrag()
    {
        Vector2 cursorWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        transform.position = new Vector2(cursorWorldPos.x, cursorWorldPos.y);
    }

    #endregion

    private void onm ouseEnter()
    {
        transform.localScale += Vector3.one * 0.1f;
    }

    private void onm ouseExit()
    {
        transform.localScale -= Vector3.one * 0.1f;
    }
}

PuzzleDrag

using UnityEngine;

public class PuzzleDrag : MonoBehavIoUr
{
    private Vector2 startPos;
    [Serializefield] private Transform correctTrans;
    [Serializefield] private bool isFinished; //Default false

    private void Start()
    {
        startPos = transform.position;
        collider2D = GetComponent<Collider2D>();
    }

    private void onm ouseDrag()
    {
        if (!isFinished)
        {
            transform.position = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x,
                Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
        }
    }

    private void onm ouseUp()
    {
        if (Mathf.Abs(transform.position.x - correctTrans.position.x) <= 0.5f &&
            Mathf.Abs(transform.position.y - correctTrans.position.y) <= 0.5f)
        {
            transform.position = new Vector2(correctTrans.position.x, correctTrans.position.y);
            isFinished = true;
        }
        else
        {
            transform.position = new Vector2(startPos.x, startPos.y);
        }
    }

    private void onm ouseDown()
    {
        if (isFinished)
            return;
    }

    //MARKER Mobile Touch Control
    private Collider2D collider2D;
    private float offsetX, offsetY;

    private void Updte()
    {
        if (!isFinished && Input.touchCount > 0)
        {
            switch (Input.GetTouch(0).phase)
            {
                case TouchPhase.Began:
                    //if(collider2D == Physics2D.OverlapPoint(Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position)))
                    //{
                    //    offsetX = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position).x - transform.position.x;
                    //    offsetY = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position).y - transform.position.y;
                    //}
                    break;

                case TouchPhase.Moved:
                    if (collider2D ==
                        Physics2D.OverlapPoint(Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position)))
                    {
                        transform.position = new Vector2(Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position).x,
                            Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position).y);
                    }

                    break;

                case TouchPhase.Ended:
                    if (Mathf.Abs(transform.position.x - correctTrans.position.x) <= 0.5f &&
                        Mathf.Abs(transform.position.y - correctTrans.position.y) <= 0.5f)
                    {
                        transform.position = new Vector2(correctTrans.position.x, correctTrans.position.y);
                        isFinished = true;
                    }
                    else
                    {
                        transform.position = new Vector2(startPos.x, startPos.y);
                    }

                    break;
            }
        }
    }
}

Drag_3D

using UnityEngine;

public class Drag_3D : MonoBehavIoUr
{
    private Vector3 offset;
    private float coordZ;

    private void onm ouseDrag()
    {
        transform.position = GetCursorPos() + offset; //MARKER Object Position = Cursor Pos + offset in 3D
    }

    private void onm ouseDown()
    {
        //MARKER offset = Dragged GameObject Pos - Cursor Pos
        offset = gameObject.transform.position - GetCursorPos();
    }

    private Vector3 GetCursorPos()
    {
        Vector3 CursorPos = Input.mousePosition;
        CursorPos.z = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
        return Camera.main.ScreenToWorldPoint(CursorPos);
    }
}

Drag_3

using UnityEngine;

public class Drag_3 : MonoBehavIoUr
{
    private Vector3 startPos;

    private void Start()
    {
        //Debug.Log(transform.GetComponent<RectTransform>().localPosition);
        startPos = transform.position;
    }

    public void DragMethod()
    {
        transform.position = Input.mousePosition;
    }

    public void DropMethod()
    {
        GameObject fillGo = GameObject.Find("Fill");

        float dist = Vector3.distance(transform.position, fillGo.transform.position);

        if (dist <= 100)
        {
            transform.position = fillGo.transform.position;
        }
        else
        {
            transform.position = startPos;
        }
    }
}

Drag_4

using UnityEngine;
using UnityEngine.EventSystems;

public class Drag_4 : MonoBehavIoUr, IPointerDownHandler, IBeginDragHandler, IDragHandler, IEndDragHandler
{
    private RectTransform rectTrans;
    [Serializefield] private Canvas canvas; //OPTIONAL

    //STEP 02
    private CanvasGroup canvasGroup;

    private void Start()
    {
        rectTrans = GetComponent<RectTransform>();
        canvasGroup = GetComponent<CanvasGroup>();
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        Debug.Log("Point Down");
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        Debug.Log("Begin Drag");
        canvasGroup.alpha = 0.5f;
        canvasGroup.blocksRaycasts = false; //STEP 02
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        Debug.Log("End Drag");
        canvasGroup.alpha = 1f;
        canvasGroup.blocksRaycasts = true; //STEP 02
    }

    public void OnDrag(PointerEventData eventData)
    {
        Debug.Log("Dragging");
        //rectTrans.anchoredPosition += eventData.delta;
        rectTrans.anchoredPosition += eventData.delta / canvas.scaleFactor; //OPTIONAL
    }
}

Slot_4

using UnityEngine;
using UnityEngine.EventSystems;

public class Slot_4 : MonoBehavIoUr, IDropHandler
{
    public void OnDrop(PointerEventData eventData)
    {
        Debug.Log("DROOoooooOOOOp");

        if(eventData.pointerDrag != null)
        {
            eventData.pointerDrag.GetComponent<RectTransform>().anchoredPosition = GetComponent<RectTransform>().anchoredPosition;
        }
    }
}

DragWindow

using UnityEngine;
using UnityEngine.EventSystems;

public class DragWindow : MonoBehavIoUr, IDragHandler, IBeginDragHandler// MAKRER IPointerDowns
{
    [Serializefield] private RectTransform panelRectTrans;

    //OPTIONAL
    //private RectTransform panelRectTrans;

    //private void Awake()
    //{
    //    if (panelRectTrans == null)
    //        panelRectTrans = transform.parent.GetComponent<RectTransform>();
    //}

    public void OnDrag(PointerEventData eventData)
    {
        panelRectTrans.anchoredPosition += eventData.delta;
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        panelRectTrans.SetAsLastSibling();
    }

    //public void OnPointerDown(PointerEventData eventData)
    //{
    //    panelRectTrans.SetAsLastSibling();
    //}
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐