重构大列表
我在下面的课程中大约有二十个列表
public class Storepart : MonoBehaviour
{
public List<GameObject> A;
public List<GameObject> B;
}
,我需要在每个类别中保存一个特殊的索引,是否有一种重构方法?
public savedata() {
for (int i = 0; i < Storepart.A.Count; i++) {
if (Storepart.A[i].activeSelf) {
SaveAindex = i;
}
}
for (int i = 0; i < Storepart.B.Count; i++) {
if (Storepart.B[i].activeSelf) {
SaveBindex = i;
}
}
}
I HAVE About twenty lists in a class like below
public class Storepart : MonoBehaviour
{
public List<GameObject> A;
public List<GameObject> B;
}
and I need to save a special index in each one, is there a way to refactor it?
public savedata() {
for (int i = 0; i < Storepart.A.Count; i++) {
if (Storepart.A[i].activeSelf) {
SaveAindex = i;
}
}
for (int i = 0; i < Storepart.B.Count; i++) {
if (Storepart.B[i].activeSelf) {
SaveBindex = i;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在此处尝试简单的
linq
ablect。此表达式由2个部分组成。内部调用
lastOrdOdefault
遍历您的列表,并返回ActiveSelf
为true的最后一个元素。外部呼叫获取lastOdordEfault
找到元素的索引。备注:
list&lt; gameObject
whiteactiviveSelf?
true使用firstordordefault
You can try a simple
LINQ
aproach here.This expression consists of 2 parts. The inner call to
LastOrDefault
traverses your List and returns the last element where theactiveSelf
is true. The outer call gets the index of the element found byLastOrDefault
.Remarks:
List<GameObject
whereactiveSelf?
is true useFirstOrDefault