如何重构这么大的if条件?武器是一个列表,它是游戏对象
如何重构这么大的if条件?武器是一个列表,它是游戏对象
public class Customanger : MonoBehaviour
{
public List<GameObject> weapon;
public static Customanger singleton;
private void Awake() => singleton = this;
};
Run Code Online (Sandbox Code Playgroud)
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();
}
Run Code Online (Sandbox Code Playgroud)
你可以试试.Any()
,
确定序列的任何元素是否存在或满足条件。
//Here I considered length of Weapon array/list is 17
if (Customanger.singleton.weapon.Any(x => x.activeSelf))
{
dosomething();
}
Run Code Online (Sandbox Code Playgroud)
如果您想检查武器子集的相同条件,那么您可以尝试以下操作,
var subSetOfWeapons = Customanger.singleton.weapon.GetRange(1, 17);
if (subSetOfWeapons.Any(x => x.activeSelf))
{
dosomething();
}
Run Code Online (Sandbox Code Playgroud)
有关更多详细信息:List.GetRange(int index, int count)
归档时间: |
|
查看次数: |
248 次 |
最近记录: |