unity 不抖动 拖拽UI

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

public class DragUI : MonoBehaviour ,IBeginDragHandler,IDragHandler,IEndDragHandler
{
    private RectTransform rectTransform;
    Vector2 mouseDown;
    Vector2 oldPos;

    void Start()
    {
        rectTransform = this.GetComponent();
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        Debug.Log(this.gameObject.name);
        init_Title_Pos();
    }

    public void OnDrag(PointerEventData eventData)
    {
        Vector2 offset = Input.mousePosition;
        offset -= mouseDown;
        offset = oldPos + offset;
        rectTransform.anchoredPosition = offset;
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        oldPos = rectTransform.anchoredPosition;
    }

    void init_Title_Pos()
    {
        mouseDown = Input.mousePosition;
        oldPos = rectTransform.anchoredPosition;
    }
}

你可能感兴趣的:(UNITY)