首页 > 编程语言 > 详细

Unity3D 通用拖拽

时间:2019-07-05 14:25:01      阅读:126      评论:0      收藏:0      [点我收藏+]
 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class DialogDrag : MonoBehaviour,IDragHandler,IPointerDownHandler
{
    private RectTransform parentRTF;

    private Vector3 offset;

    public void OnPointerDown(PointerEventData eventData)
    {
        //记录从按下点到中心点偏移量(坐标)
        //屏幕坐标-->世界坐标
        RectTransformUtility.ScreenPointToWorldPointInRectangle(parentRTF, eventData.position, eventData.pressEventCamera, out Vector3 worldPoint);
        offset = this.transform.position - worldPoint;
    }

    public void OnDrag(PointerEventData eventData)
    {
        Vector3 worldPoint;
        //屏幕坐标-->世界坐标
        RectTransformUtility.ScreenPointToWorldPointInRectangle(parentRTF, eventData.position, eventData.pressEventCamera, out worldPoint);
        //根据偏移量移动当前UI
        this.transform.position = worldPoint + offset;
    }

   
    void Start()
    {
        parentRTF = this.transform.parent as RectTransform;
    }
}

 

Unity3D 通用拖拽

原文:https://www.cnblogs.com/cct7860/p/11136983.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!