如何重构这个很大的条件
如何重构这么大的条件?武器是一个清单,它的游戏对象
如何重新分配这个很大的条件?武器是一个清单,它的gameObject
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();
}
How to refactoring this big if condition? weapon is a list and it;s gameobject
How to refactoring this big if condition? weapon is a list and it;s gameobject
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以尝试
.any()
,
如果您想检查相同的武器子集,则可以在下面尝试,
有关更多详细信息: list.getrange(int index,int count)
You can try
.Any()
,If you would like to check same condition for subset of weapons, then you can try below,
For more details: List.GetRange(int index, int count)
我不知道您正在使用哪种类型。我所做的是检查哪种模式经常出现。我看到您始终访问武器列表/数组。通常可以迭代索引器。
有些假设是由于信息的不完整而进行的。
您可以为此编写一个循环:
I don't know which types you are using. What I did is check which pattern is recurrent. I see that you always access the weapon list/array. Normally indexers can be iterated.
Some assumes are made because of the incompleteness of information.
You could write a loop for it:
无论此检查的含义如何,在OOP世界中,应将其作为实例方法(或属性)在
ustomanger
class中的实例方法(或属性)实现:然后,条件将减少到:
Whatever this check means, in OOP world it should be implemented as instance method (or property) with meaningful name in
Customanger
class:Then condition will be reduced to:
您可以做这样的事情:
也许可以将武器阵列分为类别,您可能会做得更好吗?我无法为您提供更多的代码。
编辑:删除完全不必要的Tolist()
You could do something like that:
You could probably do better than that by splitting your weapon arrays into categories maybe? I can't help you more with the code you provided.
EDIT: Removed the completely unecessary ToList()
您是如何确定
{1、2、3、4、5、8、10、12、13、13、14、15、16、17}
的相关武器指数的?如果它们都是通过履行某种条件来区分自己与其余武器的武器指数,则可以直接识别相关的武器,而不会打扰这些指数。
例如,您可以使用
.where()
来滤除相关武器:假设情况是相关武器都有一个共同的特征:它们都属于已加载的场景。 可以将条件定义为
:
然后 相关武器:
Where()
和.yany()
都在命名空间 system.linq 中找到。另一方面,如果相关武器指数不是基于共同武器特征选择的 ,我基本上是指<a href =“ https://stackoverflow.com/questions/questions/72486634/72486634/ -to-Refactor-this-if-if条件#72487116“>另一个人的答案。
首先,创建相关武器指数的集合(最好使用以某种方式描述为什么这些是相关武器指数的变量名称):
然后,验证条件;首先要明确定义相关武器:
或者通过获取相关武器内联武器:
.select()
和和
.any()()
在命名空间系统中找到.linq 。How did you decide that specifically
{ 1, 2, 3, 4, 5, 8, 10, 12, 13, 14, 15, 16, 17 }
are the relevant weapon indices?If they are all indices of weapons that distinguish themselves from the remaining weapons by fulfilling a certain condition, you could identify the relevant weapons directly and not bother with the indices.
As an example, you could use use
.Where()
to filter out the relevant weapons:Let's say the condition is that the relevant weapons all have a common trait: they all belong to scenes that have been loaded. The condition can then be defined as:
, and you can filter your relevant weapons as follows:
Now, you can use
.Any()
to perform your originalif
check on these relevant weapons:Both
.Where()
and.Any()
are found in the namespace System.Linq.If, on the other hand, the relevant weapon indices are not selected based on common weapon traits, I would basically refer to Another One's answer.
First, create a collection of the relevant weapon indices (preferably using a variable name that somehow describes why these are relevant weapon indices):
Then, verify conditions; either by first explicitly defining the relevant weapons:
or by getting the relevant weapons inline:
Both
.Select()
and.Any()
are found in the namespace System.Linq.有多种方法可以这样做,您可以包括或排除收藏中的项目,而不是检查“ ActiveSelf”标志
前任:
There are multiple ways to do it, you can include or exclude items from your collection and than check for the "activeSelf" flag
ex: