不抖动 拖拽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<RectTransform>();
    }

    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;
    }
}

猜你喜欢

转载自blog.csdn.net/degyss/article/details/91412804