码迷,mamicode.com
首页 > 编程语言 > 详细

unity中 UGUI的按下、拖动接口事件的实现

时间:2017-12-02 12:56:59      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:deb   out   oca   res   raycast   npoi   pos   设置图   dos   

using UnityEngine;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class testUI : MonoBehaviour, IPointerClickHandler, IPointerDownHandler, IPointerUpHandler, IDragHandler, IEndDragHandler,
    IPointerEnterHandler, IPointerExitHandler
{

    //private GraphicRaycaster graphicRaycaster;
    //private EventSystem eventSystem;
    //private RectTransform Image;

    public RectTransform canvas; //得到canvas的ugui坐标
    private RectTransform imgRect; //得到图片的ugui坐标
    Vector2 offset = new Vector3(); //用来得到鼠标和图片的差值
    Vector3 imgReduceScale = new Vector3(0.7f, 0.7f, 1); //设置图片缩放
    Vector3 imgNormalScale = new Vector3(1, 1, 1); //正常大小
    void Start()
    {
        //graphicRaycaster = GameObject.Find("Canvas").GetComponent<GraphicRaycaster>();
        //eventSystem = GameObject.Find("EventSystem").GetComponent<EventSystem>();
        //Image = GameObject.Find("Canvas/Image").GetComponent<RectTransform>();
        //SetRectTransformSize(Image, new Vector2(2, 2));

        imgRect = GetComponent<RectTransform>();
    }

    // Update is called once per frame
    void Update()
    {
        //if (CheckGuiRaycastObjects()) return;
        //Debug.Log(EventSystem.current.gameObject.name);
        //if (Input.GetMouseButtonDown(0))
        //{
        //    if (EventSystem.current.IsPointerOverGameObject())
        //    {
        //        Debug.Log("当前触摸在UI上");
        //        Debug.LogError(EventSystem.current.IsPointerOverGameObject());
        //    }

        //   else
        //       Debug.Log("当前没有触摸在UI上");  
        //}
    }

    bool CheckGuiRaycastObjects()
    {
        PointerEventData eventData = new PointerEventData(eventSystem);
        eventData.pressPosition = Input.mousePosition;
        eventData.position = Input.mousePosition;

        List<RaycastResult> list = new List<RaycastResult>();
        graphicRaycaster.GetComponent<GraphicRaycaster>().Raycast(eventData, list);
        Debug.Log(list.Count);
        return list.Count > 0;
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        //if (eventData.clickCount == 2)
        //{
        //    this.transform.DOScale(new Vector3(1.5f, 1.5f, 1.5f), 0.5f);
        //}
        //if (eventData.clickCount == 1)
        //{
        //    this.transform.DOScale(Vector3.one, 0.5f);
        //}
    }

    private void SetInsetAndSizeFromParentEdge(GameObject obj)
    {
        var rt = obj.gameObject.GetComponent<RectTransform>();
        rt.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Right, 0, 100);
        rt.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Bottom, 0, 30);
    }

    public static void SetRectTransformSize(RectTransform trans, Vector2 newSize)
    {
        Vector2 oldSize = trans.rect.size;
        Vector2 deltaSize = newSize - oldSize;
        trans.offsetMin = trans.offsetMin - new Vector2(deltaSize.x * trans.pivot.x, deltaSize.y * trans.pivot.y);
        trans.offsetMax = trans.offsetMax + new Vector2(deltaSize.x * (1f - trans.pivot.x), deltaSize.y * (1f - trans.pivot.y));
    }



    //当鼠标按下时调用 接口对应 IPointerDownHandler
    public void OnPointerDown(PointerEventData eventData)
    {
        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(canvas, mouseDown, eventData.enterEventCamera, out mouseUguiPos);
        if (isRect) //如果在
        {
            //计算图片中心和鼠标点的差值
            offset = imgRect.anchoredPosition - mouseUguiPos;
        }
    }



    //当鼠标抬起时调用 对应接口 IPointerUpHandler
    public void OnPointerUp(PointerEventData eventData)
    {
        //offset = Vector2.zero;
    }
    //当鼠标拖动时调用 对应接口 IDragHandler
    public void OnDrag(PointerEventData eventData)
    {
        Vector2 mouseDrag = eventData.position; //当鼠标拖动时的屏幕坐标
        Vector2 uguiPos = new Vector2(); //用来接收转换后的拖动坐标
        //和上面类似
        bool isRect = RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas, mouseDrag, eventData.enterEventCamera, out uguiPos);

        if (isRect)
        {
            //设置图片的ugui坐标与鼠标的ugui坐标保持不变
            imgRect.anchoredPosition = offset + uguiPos;
        }
    }
    //当鼠标结束拖动时调用 对应接口 IEndDragHandler
    public void OnEndDrag(PointerEventData eventData)
    {
        //offset = Vector2.zero;
    }
    //当鼠标进入图片时调用 对应接口 IPointerEnterHandler
    public void OnPointerEnter(PointerEventData eventData)
    {
        //imgRect.localScale = imgReduceScale; //缩小图片
    }
    //当鼠标退出图片时调用 对应接口 IPointerExitHandler
    public void OnPointerExit(PointerEventData eventData)
    {
        //imgRect.localScale = imgNormalScale; //回复图片
    }
}

 

unity中 UGUI的按下、拖动接口事件的实现

标签:deb   out   oca   res   raycast   npoi   pos   设置图   dos   

原文地址:http://www.cnblogs.com/Study088/p/7953780.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!