如何重构这么大的if条件?武器是一张单子,它;s游戏对象

如何重构这么大的if条件?武器是一张单子,它;s游戏对象

public class Customanger : MonoBehaviour
{
    public List<GameObject> weapon;
    public static Customanger singleton;
    private void Awake() => singleton = this;
};
if (Customanger.singleton.weapon[1].activeSelf ||
    Customanger.singleton.weapon[2].activeSelf ||
    Customanger.singleton.weapon[3].activeSelf ||
    Customanger.singleton.weapon[4].activeSelf ||
    Customanger.singleton.weapon[5].activeSelf ||
    Customanger.singleton.weapon[8].activeSelf ||
    Customanger.singleton.weapon[10].activeSelf ||
    Customanger.singleton.weapon[12].activeSelf ||
    Customanger.singleton.weapon[13].activeSelf ||
    Customanger.singleton.weapon[14].activeSelf ||
    Customanger.singleton.weapon[15].activeSelf ||
    Customanger.singleton.weapon[16].activeSelf ||
    Customanger.singleton.weapon[17].activeSelf)
{
    dosomething();
}

推荐答案

你可以试试.Any()

确定序列的任何元素是否存在或满足

//Here I considered length of Weapon array/list is 17
if (Customanger.singleton.weapon.Any(x => x.activeSelf))
{
    dosomething();
}

如果您想判断武器子集的相同条件,那么您可以try 以下操作,

var subSetOfWeapons = Customanger.singleton.weapon.GetRange(1, 17);

if (subSetOfWeapons.Any(x => x.activeSelf))
{
    dosomething();
}

更多详情:List.GetRange(int index, int count)

Csharp相关问答推荐

如何在C#中将对象[*,*]直接转换为字符串[*,*]?

如何删除文件的基础上嵌入的时间戳嵌入文件名

选取器与.NET Maui MVVM的绑定属性

无法通过绑定禁用条目

Blazor在FluentButton onClick事件上设置参数

未找到任何HTTP触发器.成功部署Azure Functions Project后(c#)

Swagger没有显示int?可以为空

DbContext-传递自定义配置选项

Linux Docker上的.NET6在某个时间抛出后,在加密操作期间发生System.Security.Cryptography.CryptographicException:错误

C#动态设置ServerReport报表参数

错误CS1061';AuthenticationBuilder';不包含AddOpenIdConnect的定义

如何将%{v_扩展}转换为%{v_扩展}>>

在C#中,当输入一个方法/局部函数时,我的IEnumerator被重置/重新创建.为什么?

EF Core:如何对关系属性进行建模?

VS代码扩展无法在新版本扩展C#中运行从v2.10.28开始

WPF:如何从DatagridHeader的内容模板绑定到词典项

将两个for循环更改为一条LINQ语句

SharpZipLib在文件名前加上目录名,生成tar.gz

如何在更新数据库实体时忽略特定字段?

Windows 10上埃及标准时间的时区偏移不正确