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

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; //回复图片
    }
}

 

转载于:https://www.cnblogs.com/Study088/p/7953780.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值