【UGUI】屏幕区域点击检测

本文介绍了如何在Unity中使用UGUI进行屏幕点击检测,特别是在需要关闭界面或者实现特定区域拖动事件时。作者提供了一个自定义的InputManager工具,解决了全透明图片托底方案的问题,避免遮挡其他UI事件。该工具支持矩形区域的硬检测,以适应不同场景的屏幕区域交互需求。
摘要由CSDN通过智能技术生成

此处直接代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


namespace Asstes.UI
{
    /// <summary>
    /// Introduction: 屏幕区域点击判断
    /// Author: 
    /// Time: 
    /// </summary>
    public class AreaClick
    {
        private static AreaClick m_instance;

        public static AreaClick Instance
        {
            get
            {
                if (m_instance == null)
                    m_instance = new AreaClick();
                return m_instance;
            }
        }

        private int m_index;
        private List<AreaRect> m_rects;
        private List<AreaRect> m_removes;
        private List<int> m_deletes;

        public AreaClick()
        {
            m_index = 0;
            m_rects = new List<AreaRect>();
            m_removes = new List<AreaRect>();
            m_deletes = new List<int>();
            InputManager.Instance.AddListener(InputType.OnClick, OnClickScreen);
            InputManager.Instance.AddListener(InputType.OnEndLongPress, OnClickScreen);
        }

        private void OnClickScreen(Vector2 param)
        {
            m_removes.Clear();
            for (int i = 0; i < m_rects.Count; i++)//移除已删除的点
            {
                if(m_deletes.Contains(m_rects[i].id))
                    m_removes.Add(m_rects[i]);
            }
            for (int i = 0; i < m_removes.Count; i++)
            {
                m_rects.Remove(m_removes[i]);
            }
            m_deletes.Clear();
            for (int i = 0; i < m_rects.Count; i++)
            {
                m_rects[i].OnClick(param);
            }
        }

        /// <summary>
        /// 添加检测
        /// </summary>
        /// <param name="area"></param>
        /// <returns></returns>
        public int AddCheck(AreaRect area)
        {
            area.id = m_index++;
            m_rects.Add(area);
            return area.id;
        }

        /// <summary>
        /// 移除检测
        /// </summary>
        /// <param name="id"></param>
        public void CancelCheck(int id)
        {
            if(!m_deletes.Contains(id))
                m_deletes.Add(id);
        }

    }

    public class AreaRect
    {
        private Rect m_rect;
        private bool m_allNotice;
        private bool m_onlyOnce;
        private Callback_1<bool> m_callback;
        internal int id;
        /// <summary>
        /// 是否只使用一次
        /// </summary>
        public bool UseOnce { get { return m_onlyOnce; } set { m_onlyOnce = value; } }

        /// <summary>
        /// 普通构造函数
        /// </summary>
        /// <param name="rect">在屏幕上的区域</param>
        /// <param name="allNotice">通知点中和未点中</param>
        /// <param name="callback">回调</param>
        public AreaRect(Rect rect, bool allNotice, Callback_1<bool> callback)
        {
            m_rect = rect;
            m_allNotice = allNotice;
            m_callback = callback;
        }

        /// <summary>
        /// 可缩放区域
        /// </summary>
        /// <param name="rect">设计时所选区域,如UI设计时的rect</param>
        /// <param name="designSize">UI设计时屏幕分辨率</param>
        /// <param name="allNotice">通知点中和未点中</param>
        /// <param name="callback">回调</param>
        public AreaRect(
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值