Version: 2021.1
LanguageEnglish
  • C#

SearchProvider.toObject

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,Type,Object> toObject;

Description

Returns any valid Unity object held by the search item.

[SearchItemProvider]
internal static SearchProvider CreateProvider()
{
    return new SearchProvider(id, name)
    {
        active = false,
        filterId = "tree:",
        priority = 99999, // put example provider at a low priority
        fetchItems = (context, items, provider) =>
        {
            // That provider searches for tree prefabs in the project
            var results = AssetDatabase.FindAssets("t:Prefab tree" + context.searchQuery);
            foreach (var guid in results)
            {
                items.Add(provider.CreateItem(context, AssetDatabase.GUIDToAssetPath(guid), null, null, null, null));
            }
            return null;
        },
        #pragma warning disable UNT0008 // Null propagation on Unity objects
        // Use fetch to load the asset asynchronously on display
        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),
        #pragma warning restore UNT0008 // Null propagation on Unity objects
        // Shows handled actions in the preview inspector
        // Shows inspector view in the preview inspector (uses toObject)
        showDetailsOptions = ShowDetailsOptions.Inspector | ShowDetailsOptions.Actions,
        trackSelection = (item, context) =>
        {
            var obj = AssetDatabase.LoadMainAssetAtPath(item.id);
            if (obj != null)
                EditorGUIUtility.PingObject(obj.GetInstanceID());
        },
        startDrag = (item, context) =>
        {
            var obj = AssetDatabase.LoadMainAssetAtPath(item.id);
            if (obj != null)
            {
                DragAndDrop.PrepareStartDrag();
                DragAndDrop.objectReferences = new Object[] { obj };
                DragAndDrop.StartDrag(item.label);
            }
        }
    };
}