Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Unity Search API: Getting path from SearchItem?

Discussion in 'Editor & General Support' started by Alex-Chouls, Nov 9, 2023.

  1. Alex-Chouls

    Alex-Chouls

    Joined:
    Mar 24, 2009
    Posts:
    2,666
    In the Global Search window we can do a search like this: p: t=light .unity
    and get the path to the scenes in the Path column.

    How do we do the same using the search API?

    I can execute the search and get the SearchItems, BUT:

    item.ToObject is null if the scene is not loaded.

    item.value contains a GlobalObjectId string, but using GlobalObjectId.GlobalObjectIdentifierToObjectSlow with the parsed id returns null.

    item.data seems to contain AssetMetaInfo with a path property, but its internal.

    I'm assuming anything we can do in the Global Search window can be achieved with the search API, but I can't figure out how. An example would be very helpful!
     
  2. Alex-Chouls

    Alex-Chouls

    Joined:
    Mar 24, 2009
    Posts:
    2,666
    Figured it out, was missing the assetGUID in the GlobalObjectId.

    So you can parse the SearchItem value to get a GlobalObjectId and then get the path from the assetGUID:

    Code (CSharp):
    1. if (GlobalObjectId.TryParse(item.value as string, out var id))
    2. {
    3.     Debug.Log(AssetDatabase.GUIDToAssetPath(id.assetGUID));
    4. }
    EDIT:

    Another thing I was missing, if SearchItem label is null you can call GetLabel(context) to get a label similar to the one shown in the search window.
     
    Last edited: Nov 9, 2023