Version: 2022.2
LanguageEnglish
  • C#

SearchProvider.fetchThumbnail

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

public Func<SearchItem,SearchContext,Texture2D> fetchThumbnail;

Description

Handler to provide an asynchronous thumbnail for an item. Is called when the item is about to be displayed. Compared to preview a thumbnail should be small and returned as fast as possible. Use fetchPreview if you want to generate a preview that is bigger and slower to return. Allows a plugin provider to only fetch/generate previews when they are needed.

[SearchItemProvider]
internal static SearchProvider CreateProvider()
{
    return new SearchProvider("example_tree", "Trees")
    {
        filterId = "tree:",
        priority = 99999, // Put example provider at a low priority
        showDetailsOptions = ShowDetailsOptions.Inspector | ShowDetailsOptions.Actions,
        fetchItems = (context, items, provider) => FetchItems(context, provider),
        fetchThumbnail = (item, context) => AssetDatabase.GetCachedIcon(item.id) as Texture2D,
        fetchPreview = (item, context, size, options) => AssetDatabase.GetCachedIcon(item.id) as Texture2D,
        fetchLabel = (item, context) => AssetDatabase.LoadMainAssetAtPath(item.id).name,
        fetchDescription = (item, context) => AssetDatabase.LoadMainAssetAtPath(item.id).name,
        toObject = (item, type) => AssetDatabase.LoadMainAssetAtPath(item.id),
    };
}

private static IEnumerable<SearchItem> FetchItems(SearchContext context, SearchProvider provider)
{
    if (context.empty)
        yield break;

    // Yield items asynchronously which is the recommended way.
    foreach (var guid in AssetDatabase.FindAssets("t:Prefab tree " + context.searchQuery))
        yield return provider.CreateItem(context, AssetDatabase.GUIDToAssetPath(guid), null, null, null, null);
}