![](/img/trans.png)
[英]Find all the children gameObjects from the parent gameObject and assign it to a var gameObject?
[英]Retrieve All Children From Parent[]
給定此類:
public class Parent
{
public Child[] Children {get;set;}
}
和這個數組:
Parent[] parents;
如何使用Linq或其他方法從parent數組中檢索所有子級? 這似乎很糟糕:
IList<Child> children = new List<Child>();
foreach(var parent in parents)
children.AddRange(parent.Children);
還是這樣還不錯? :-)
嘗試這個:
parents.SelectMany(p => p.Children).ToArray()
我認為您在問題中提出的解決方案並不壞。 可讀性強,易於理解正在發生的事情。 除非您出於某種原因需要優化它,否則我不會看到任何錯誤。
如果您只是真的想使用Linq,那就是另一回事了(我認為這是完全正確的-Linq的經驗/做法越多越好)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.