加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

unity 原生UI 拖拽跟随鼠标移动

(2016-11-17 18:55:05)

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

public class MouseDrag : MonoBehaviour, IPointerDownHandler, IDragHandler, IPointerUpHandler, IEndDragHandler, IPointerEnterHandler, IPointerExitHandler
{

    RectTransform canvas;//得到Canvase的ui坐标
    RectTransform TureRect;//得到图片的Ui坐标
    Vector2 offset = new Vector2();//得到鼠标坐标和图片位置的差值
    bool bcontrol_Drag = false;
    private GameObject oldSlot;
    private Transform DragParent;//拖拽时用到的父对象
    Vector3 imgScale = new Vector3(0.8f, 0.8f, 1);//图片缩放
    Vector3 imgNormalScle = new Vector3(1, 1, 1);//正常大小
    RectTransform ImgRect;//得到图片的Ui坐标
    void Awake()
    {
        canvas = GameObject.Find("Canvas").GetComponent();
        DragParent = GameObject.Find("DragParent").transform;
    }
    void Start()
    {
        TureRect = GetComponent();
        ImgRect = DragParent.GetComponent();
    }
 
    ///
    /// 当鼠标按下时
    ///
    ///
    public void OnPointerDown(PointerEventData eventData)
    {
        Vector2 mouseDown = eventData.position;//鼠标按下时屏幕的坐标
        Vector2 GetUiPos = new Vector2();//返回Ui的坐标

        bool isRect = RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas, mouseDown, eventData.enterEventCamera, out GetUiPos);
        oldSlot = transform.parent.gameObject;
        Debug.Log("oldSlot=" + oldSlot.name);
        if (isRect)
        {
            offset = TureRect.anchoredPosition - GetUiPos;
            ImgRect.anchoredPosition = GetUiPos;
            // DragParent.anchoredPosition = offset;
        }
    }
    ///
    /// 当鼠标拖动时调用 对应接口IDragHandler
    ///
    ///
    public void OnDrag(PointerEventData eventData)
    {
        ImgRect.SetAsFirstSibling();
        //  transform.SetParent(DragParent);
        Vector2 mouseDrag = eventData.position;//当鼠标拖动时的屏幕坐标
        Vector2 GetUiPosDrg = new Vector2();//存储转化后的坐标
        bool isRect = RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas, mouseDrag, eventData.enterEventCamera, out GetUiPosDrg);
        if (isRect)
        {
            //   TureRect.anchoredPosition = offset + GetUiPosDrg;
        }
    }
    ///
    /// 当鼠标抬起时调用 对应接口IPointUpHander
    ///
    ///
    public void OnPointerUp(PointerEventData eventData)
    {
        //   Debug.Log(eventData.pointerCurrentRaycast.gameObject.name);
        // offset = Vector2.zero;
    }
    ///
    /// 当鼠标结束拖动时调用 对应接口IEndDragHandler
    ///
    ///
    public void OnEndDrag(PointerEventData eventData)
    {
        if (eventData.button == PointerEventData.InputButton.Left)
        {
            Transform newSlot = null;
            if (eventData.pointerEnter != null)
            {
                newSlot = eventData.pointerEnter.transform;
                Debug.Log("newSlot=" + newSlot);
                GameObject CurrtItemObj = this.gameObject;

                GameObject SecondItem = newSlot.parent.gameObject;
                Debug.Log("SecondItem.name" + SecondItem.name);
            }
            Debug.Log("");
        }
        // offset = Vector2.zero;
    }
    ///
    /// 当鼠标进入图片时调用 对应接口IPointerEnterHandler
    ///
    ///
    public void OnPointerEnter(PointerEventData eventData)
    {
        // TureRect.localScale = imgScale;
    }
    ///
    /// 当鼠标退出图片时调用 对应接口IPointerExitHandlerk
    ///
    ///
    public void OnPointerExit(PointerEventData eventData)
    {
        //  TureRect.localScale =imgNormalScle;
    }
    Vector3 vec3 = new Vector3();
    Vector3 pos = new Vector3();
    //void Update()
    //{
    //    if (Input.GetMouseButtonDown(0))
    //    {
    //        vec3 = Input.mousePosition;
    //        pos = transform.GetComponent().position;
    //    }
    //    if (Input.GetMouseButton(0))
    //    {
    //        Vector3 off = Input.mousePosition - vec3;
    //        vec3 = Input.mousePosition;
    //        pos = pos + off;
    //        transform.GetComponent().position = pos;
    //    }
    //}
}

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有