Unity中在判断某个Button是否被点击的时候,就需要在这个Button的点击事件上加监听,这样下来一通操作很麻烦,为此博主封装了一个针对于Button的拓展方法,可直接判定Button是否被点击,目的是提高效率,但不保证兼容各种复杂项目哦。首先介绍如何使用,末尾简单概述一下实现思路。
实现代码:
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UGUIButtonChecker : MonoBehaviour
{
void Update()
{
List<int> btns = ButtonExpand.btns;
if (ButtonExpand.btns.Count != 0)
{
foreach (var item in btns)
{
ButtonExpand.btnDc[item] = false;
}
btns.Clear();
}
}
}
// Button 拓展类
public static class ButtonExpand
{
public static Dictionary<int, bool> btnDc = new Dictionary<int, bool>();//字典(键:按钮的ID,值:按钮是否被点击)
public static List<int> btns = new List<int>();
static GameObject obj;//常驻
///