C# 在多个元素上使用相同的函数
假设我想在多个数组元素上使用相同的函数: 公共游戏对象[]锁定按钮; LockPIButton[0]。SetActivefalse; LockPIButton[1]。SetActivetrue; LockPIButton[2].SetActivetrue; LockPIButton[3].SetActivetrue; LockPIButton[4].SetActivetrue; LockPIButton[5].SetActivetrue; LockPIButton[6].SetActivetrue; LockPIButton[7].SetActivetrue; LockPIButton[8].SetActivetrue; LockPIButton[9].SetActivetrue; LockPIButton[10].SetActivetrue; LockPIButton[11].SetActivetrue;C# 在多个元素上使用相同的函数,c#,arrays,unity3d,C#,Arrays,Unity3d,假设我想在多个数组元素上使用相同的函数: 公共游戏对象[]锁定按钮; LockPIButton[0]。SetActivefalse; LockPIButton[1]。SetActivetrue; LockPIButton[2].SetActivetrue; LockPIButton[3].SetActivetrue; LockPIButton[4].SetActivetrue; LockPIButton[5].SetActivetrue; LockPIButton[6].SetActivetru
有没有一种方法可以将同一函数应用于多个元素而不复制粘贴它?通常,您可以使用任何类型的循环,例如foreach 为了清楚起见,我在数组字段的名称中添加了复数形式的s
public GameObject[] LockPIButtons;
// ....
foreach (var lockPIButton in LockPIButtons)
{
lockPIButton.SetActive(true);
}
或使用等效的ForEach方法:
但是,由于您依赖索引将其中一个设置为false,因此您可以:
使用经典for循环,请参见
只需将您想在事后设置为false的设置往后退,尽管这感觉像是在浪费处理时间:
使用linq的Select可以将索引作为附加的lambda参数:
通常,您可以使用任何类型的循环,例如foreach 为了清楚起见,我在数组字段的名称中添加了复数形式的s
public GameObject[] LockPIButtons;
// ....
foreach (var lockPIButton in LockPIButtons)
{
lockPIButton.SetActive(true);
}
或使用等效的ForEach方法:
但是,由于您依赖索引将其中一个设置为false,因此您可以:
使用经典for循环,请参见
只需将您想在事后设置为false的设置往后退,尽管这感觉像是在浪费处理时间:
使用linq的Select可以将索引作为附加的lambda参数:
一般来说是的:这就是or循环的用途。Linq只是这些的简写 在您的代码片段中,您似乎希望有一个值为false的索引,以便可以使用例如
public GameObject[] LockPIButtons;
public void SetInactive(int index)
{
for(var i = 0; i < LockPIButtons.Length; i++)
{
lockPIButton.SetActive(i != index);
}
}
将导致您向我们展示的内容。通常是的:这就是or循环的用途。Linq只是这些的简写 在您的代码片段中,您似乎希望有一个值为false的索引,以便可以使用例如
public GameObject[] LockPIButtons;
public void SetInactive(int index)
{
for(var i = 0; i < LockPIButtons.Length; i++)
{
lockPIButton.SetActive(i != index);
}
}
将导致您向我们展示的内容。我忘了澄清我使用的是UnityEngine。在阅读循环建议后,我提出了以下解决方案
public GameObject[] LockPIButton;
for (var T = 0; T < LockPIButton.Length; T++) { LockPIButton[T].SetActive(true); }
//This one is to make sure all elements is (true)
LockPIButton[0].SetActive(false);
//This one is added because i want to make an exception on 1 single element, so i change it back to (false).
我忘了澄清我使用的是UnityEngine。在阅读循环建议后,我提出了以下解决方案
public GameObject[] LockPIButton;
for (var T = 0; T < LockPIButton.Length; T++) { LockPIButton[T].SetActive(true); }
//This one is to make sure all elements is (true)
LockPIButton[0].SetActive(false);
//This one is added because i want to make an exception on 1 single element, so i change it back to (false).
其中一个应该是假的吗?@derHugo是的,我需要对其中的一个或几个破例一个或几个。。。你需要更具体一点;如果有几个,那么在什么条件下?其中一个是假的吗?@derHugo是的,我需要对其中一个或几个破例一个或几个。。。你需要更具体一点;如果是少数,那么在什么条件下?
public GameObject[] LockPIButton;
for (var T = 0; T < LockPIButton.Length; T++) { LockPIButton[T].SetActive(true); }
//This one is to make sure all elements is (true)
LockPIButton[0].SetActive(false);
//This one is added because i want to make an exception on 1 single element, so i change it back to (false).