Unity内自建可以拖拽的UGUI

Unity内拖拽的UGUI

介绍

这是我项目中因为需求,而根据Unity自带的接口(interface)创建的一个UI。
我有个文章专门记录了一些UGUI的接口及其用法和触发条件。该文算是那篇文章的一个实例。附上链接:https://blog.csdn.net/f_957995490/article/details/103928846

功能

该UI同时具有点击功能和拖拽功能。项目要求点击在UI的范围内,在该范围投影内的3D物体便会触发响应事件;或者在UI的范围内进行拖拽,可以旋转相应的3D物体。关于旋转的逻辑并贴出来,很多方法很简单,对本文也并不重要。

代码

  • 使用委托事件
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class UITouch : Selectable, IPointerClickHandler, IDragHandler, IBeginDragHandler, IEndDragHandler
{
    private bool isDrag = false;

    public ParameterVecter2 onClick;

    public virtual void OnPointerClick(PointerEventData eventData)
    {
        //Debug.Log(eventData.position);
        if (!isDrag)
        {//如果经过拖拽,则不出发事件
            if (null != onClick) onClick.Invoke(eventData.position);
            //Debug.Log("PointerClick");
        }
    }

    public ParameterVecter2 onBeginDrag;

    public virtual void OnBeginDrag(PointerEventData eventData)
    {
        isDrag = true;
        if (null != onBeginDrag) onBeginDrag.Invoke(eventData.position);
        //Debug.Log("BeginDrag");
    }

    public ParameterVecter2 onDrag;

    public virtual void OnDrag(PointerEventData eventData)
    {
        if (null != onDrag) onDrag.Invoke(eventData.position);
        //Debug.Log("Drag");
    }

    public ParameterVecter2 onEndDrag;

    public virtual void OnEndDrag(PointerEventData eventData)
    {
        if (null != onEndDrag) onEndDrag.Invoke(eventData.position);
        //Debug.Log("EndDrag");
        isDrag = false;
    }
}

public delegate void ParameterVecter2(Vector2 vec);
public delegate void ParameterVecter3(Vector3 vec);
  • 使用UnityEvent
using System;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace ZM_Code
{
    /// <summary>
    /// UI触碰
    /// </summary>
    public class UITouch : Selectable, IPointerClickHandler, IDragHandler, IBeginDragHandler, IEndDragHandler
    {
        private bool isDrag = false;

        public ParameterVecter2Event onClick;

        public virtual void OnPointerClick(PointerEventData eventData)
        {
            //Debug.Log(eventData.position);
            if (!isDrag)
            {//如果经过拖拽,则不出发事件
                if (null != onClick) onClick.Invoke(eventData.position);
                //Debug.Log("PointerClick");
            }
        }

        public ParameterVecter2Event onBeginDrag;

        public virtual void OnBeginDrag(PointerEventData eventData)
        {
            isDrag = true;
            if (null != onBeginDrag) onBeginDrag.Invoke(eventData.position);
            //Debug.Log("BeginDrag");
        }

        public ParameterVecter2Event onDrag;

        public virtual void OnDrag(PointerEventData eventData)
        {
            if (null != onDrag) onDrag.Invoke(eventData.position);
            //Debug.Log("Drag");
        }

        public ParameterVecter2Event onEndDrag;

        public virtual void OnEndDrag(PointerEventData eventData)
        {
            if (null != onEndDrag) onEndDrag.Invoke(eventData.position);
            //Debug.Log("EndDrag");
            isDrag = false;
        }

        public ParameterVecter2Event onScroll;

        /// <summary>
        /// 鼠标滚落事件
        /// </summary>
        public virtual void OnScroll(PointerEventData eventData)
        {
            if (null != onScroll) onScroll.Invoke(eventData);
            //Debug.Log(eventData.scrollDelta);
            //Debug.Log("Scroll");
        }
        
        [Serializable]
        public class ParameterVecter2Event : UnityEvent<Vector2>
        {
            public ParameterVecter2Event() { }
        }

        [Serializable]
        public class ParameterVecter3Event : UnityEvent<Vector3>
        {
            public ParameterVecter3Event() { }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

天富儿

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

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

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

打赏作者

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

抵扣说明:

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

余额充值