我正在嘗試收集所有以特定字串或標簽開頭的游戲物件。例如,字串/標簽是“Sub-”。我想將它們全部添加到我的串列中。我如何實作這一目標?
public List<GameObject> subs = new List<GameObject>();
private void Start(){
foreach(GameObject subGroup in GameObject.FindGameObjectsWithTag("Sub-")) {
subs.Add(subGroup);
}
}
uj5u.com熱心網友回復:
使用Linq你可以做類似的事情
using System.Linq;
...
// get all Transform components of the scene
// pass in true to include inactive ones
var all = FindObjectsOfType<Transform>(true);
// Filter and only keep the ones whee the name starts with your string
subs = all.Where(obj => obj.name.StartsWith(nameOfObj)).ToList();
// Or use a certain tag
subs = all.Where(obj => obj.CompareTag(nameOfObj)).ToList();
// or check if the tag starts with your string
subs = all.Where(obj => obj.tag.StartsWith(nameOfObj)).ToList();
看到Object.FindObjectsOfType和LinqWhere
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/424508.html
上一篇:Unity卡片懸停影片
