Unity Search API: Getting path from SearchItem?

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!

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:

if (GlobalObjectId.TryParse(item.value as string, out var id))
{
    Debug.Log(AssetDatabase.GUIDToAssetPath(id.assetGUID));
}

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.

1 Like