拖拽ui

拖拽ui
鼠标选中ui,并进行拖拽。
话不多说,直接上代码。

using System.Collections;

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.UI;

using UnityEngine.EventSystems;

using DG.Tweening;

using GlobALSC;

///

/// UI 拖动位置

///

public class UI_Drag:MonoBehaviour,IBeginDragHandler, IDragHandler, IEndDragHandler{

RectTransform canvasRect;          //得到canvas的ugui坐标



RectTransform imgRect;        //得到图片的ugui坐标

Vector2 offset = new Vector3();    //用来得到鼠标和图片的差值



GameObject functionObj;



void OnEnable(){

    imgRect = this.GetComponent<RectTransform>();

    canvasRect = transform.parent as RectTransform;

    functionObj = GameObject.Find ("function");

}





/// <summary>

/// 按下按钮,对应接口 IPointerDownHandler

/// </summary>

/// <param name="eventData">Event data.</param>

public void OnBeginDrag(PointerEventData eventData)

{

    if (GlobalSc._instance.IsIn3D==true)

    {

        GameObject.Find("CameraRoot").GetComponent<CmeraOrbit>().enabled = false;

    }

    functionObj.GetComponent<Freeview> ().userControl = false;

    Vector2 mouseDown = eventData.position;    //记录鼠标按下时的屏幕坐标

    Vector2 mouseUguiPos = new Vector2();   //定义一个接收返回的ugui坐标

    //RectTransformUtility.ScreenPointToLocalPointInRectangle():把屏幕坐标转化成ugui坐标

    //canvas:坐标要转换到哪一个物体上,这里img父类是Canvas,我们就用Canvas

    //eventData.enterEventCamera:这个事件是由哪个摄像机执行的

    //out mouseUguiPos:返回转换后的ugui坐标

    //isRect:方法返回一个bool值,判断鼠标按下的点是否在要转换的物体上

    bool isRect = RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRect, mouseDown, eventData.enterEventCamera, out mouseUguiPos);

    if (isRect)   //如果在

    {

        //计算图片中心和鼠标点的差值

        offset = imgRect.anchoredPosition - mouseUguiPos;

    }







}





/// <summary>

/// 当鼠标拖动时调用   对应接口 IDragHandler

/// </summary>

/// <param name="eventData">Event data.</param>

public void OnDrag(PointerEventData eventData)

{

    if (eventData.button != PointerEventData.InputButton.Left)

        return;

    

    Vector2 mouseDrag = eventData.position;   //当鼠标拖动时的屏幕坐标

    Vector2 uguiPos = new Vector2();   //用来接收转换后的拖动坐标

    //和上面类似

    bool isRect = RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRect, mouseDrag, eventData.enterEventCamera, out uguiPos);



    if (isRect)

    {            //设置图片的ugui坐标与鼠标的ugui坐标保持不变

        imgRect.anchoredPosition = offset + uguiPos;

    }

}



/// <summary>

/// 当鼠标抬起时调用  对应接口  IPointerUpHandler

/// </summary>

/// <param name="eventData">Event data.</param>

public void OnEndDrag(PointerEventData eventData)

{

    if (GlobalSc._instance.IsIn3D == true)

    {

        GameObject.Find("CameraRoot").GetComponent<CmeraOrbit>().enabled = true;

    }

    offset = Vector2.zero;

    functionObj.GetComponent<Freeview> ().userControl = true;

}

}
将此脚本挂在需要拖动的ui上就可以了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

�A Lemon

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值