此处直接代码:
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(