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();
    //}
}
上一篇:actionlib


下一篇:Ubentu和ROS一些实用但是很容易忘掉的命令行