Return 项目列表并阅读

Return a list of items and read it

我有一个包含外部调用列表的函数。我想通过对象名称查找包含在列表中的对象。我想获取对象的 属性 类它包含的组件。我该怎么做?

public string nameOfObj;
void Start () {
      //Call function here
    GetObjects(nameOfObj);

   }

    public List<GameObject> GetObjects(String obj){
        return new List<GameObject>();
    }

您可以使用 Where 函数执行此操作。

using System.Linq;

List<GameObject> objectsToSearch = new List<GameObject>();

public List<GameObject> GetObjects(string obj){
    return objectsToSearch.Where(a => a.nameOfObj == obj).ToList();
}

作为警告,这不是获取数据的最佳方式。根据调用频率以及搜索列表中的数据量,此方法可能会导致问题。