Version: 2023.1
LanguageEnglish
  • C#

SearchUtils.FetchGameObjects

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Declaration

public static GameObject[] FetchGameObjects(SceneManagement.Scene scene);

Declaration

public static IEnumerable<GameObject> FetchGameObjects();

Parameters

scene Scene to get objects from.

Returns

GameObject[] The array of game objects in the scene.

Description

Utility function to fetch all the game objects in a particular scene.

Use SearchUtils.FetchGameObjects to create a custom SearchProvider that is able to access current scene objects.

static void OnEnable()
{
    s_GameObjects = SearchUtils.FetchGameObjects().ToArray();
    s_QueryEngine = new QueryEngine<GameObject>();

    // Id supports all operators
    s_QueryEngine.AddFilter("id", go => go.GetInstanceID());
    // Name supports only :, = and !=
    s_QueryEngine.AddFilter("n", go => go.name, new[] {":", "=", "!="});

    // Add distance filtering. Does not support :.
    s_QueryEngine.AddFilter("dist", DistanceHandler, DistanceParamHandler, new[] {"=", "!=", "<", ">", "<=", ">="});
}
static IEnumerator SearchItems(SearchContext context, SearchProvider provider)
{
    var query = s_QueryEngine.ParseQuery(context.searchQuery);
    if (!query.valid)
        yield break;

    var filteredObjects = query.Apply(s_GameObjects);
    foreach (var filteredObject in filteredObjects)
    {
        yield return provider.CreateItem(filteredObject.GetInstanceID().ToString(), null, null, null, filteredObject.GetInstanceID());
    }
}
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961