Unity UGUI拖拽移动

//第一种拖拽挂在UI上即可
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; public class Clonephoto : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler { public bool isPrecision; //存储图片中心点与鼠标点击点的偏移量 private Vector3 offect; //存储当前拖拽图片的RectTransform组件 private RectTransform rect; void Start() { rect = this.transform.GetComponent<RectTransform>(); } public void OnBeginDrag(PointerEventData eventData) { //如果是精确拖拽则进行计算偏移量操作 if (isPrecision) { // 存储点击时的鼠标坐标 Vector3 tWorldPos; //UI屏幕坐标转换为世界坐标 RectTransformUtility.ScreenPointToWorldPointInRectangle(m_rt, eventData.position, eventData.pressEventCamera, out tWorldPos); //计算偏移量 m_offset = transform.position - tWorldPos; } //否则,默认偏移量为0 else { m_offset = Vector3.zero; } m_rt.position = Input.mousePosition + m_offset; } //拖拽过程中触发 public void OnDrag(PointerEventData eventData) { m_rt.position = Input.mousePosition + m_offset; } //结束拖拽触发 public void OnEndDrag(PointerEventData eventData) { m_rt.position = Input.mousePosition + m_offset; } }

第二种 克隆拖拽

/******************************************
 * 项目名称:UGUI通用
 * 脚本功能:UI图片拖拽功能(将脚本挂载在需要拖放的图片上)
*******************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class Clonephoto : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
    private GameObject objj;
    private RectTransform m_rt;//canvas下面的一个ui
    private Vector3 offect;
    private RectTransform rect;
    void Start()
    {
        //查找这个ui(前提是必须得有这个UI)
        m_rt = GameObject.Find("Canvas/CloneBan").GetComponent<RectTransform>();
        rect = this.transform.GetComponent<RectTransform>();
    }
    //开始拖拽
    public void OnBeginDrag(PointerEventData eventData)
    {
        //ui屏幕坐标转换为世界坐标
        RectTransformUtility.ScreenPointToWorldPointInRectangle(rect, eventData.position, eventData.
            pressEventCamera, out offect);
        //克隆自己在m_rt上面显示
        objj = Instantiate(transform.gameObject,m_rt);
        //objj.transform.GetComponent<RectTransform>().sizeDelta = new Vector2(80, 80); //设置ui大小
        //objj.transform.localScale = new Vector3(0.7f, 0.7f, 0.7f);                  //设置一下拖的时候大小,显得好看一点点,可要可不要
        objj.transform.GetComponent<RectTransform>().anchoredPosition = Input.mousePosition -offect; //计算偏移量
    }
    //拖拽中
    public void OnDrag(PointerEventData eventData)
    {
        if (objj !=null )
        {
            objj.transform.GetComponent <RectTransform>().anchoredPosition = Input.mousePosition-offect;
        }
    }
    //结束拖拽
    public void OnEndDrag(PointerEventData eventData)
    {
        if (objj!=null)
        {
            Accuracy_testing.name = objj.transform.name;
             objj.SetActive(false);
             Invoke("ShanChu", 0.1f);            
        }
    }
    void ShanChu()
    {
        Destroy(objj.gameObject);
    }

}

 

上一篇:Unity EventSystem 详解(Unity Version 5.5.1)不是原创


下一篇:【图像识别】基于模板匹配之手写数字识别系统GUI界面【Matlab 094期】【图像处理26】