Version: 2023.2
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("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),
        trackSelection = TrackSelection,
        startDrag = StartDrag
    };
}

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);
}

private static void TrackSelection(SearchItem searchItem, SearchContext searchContext)
{
    EditorGUIUtility.PingObject(AssetDatabase.LoadMainAssetAtPath(searchItem.id));
}

private static void StartDrag(SearchItem item, SearchContext context)
{
    if (context.selection.Count > 1)
    {
        var selectedObjects = context.selection.Select(i => AssetDatabase.LoadMainAssetAtPath(i.id));
        var paths = context.selection.Select(i => i.id).ToArray();
        StartDrag(selectedObjects.ToArray(), paths, item.GetLabel(context, true));
    }
    else
        StartDrag(new[] { AssetDatabase.LoadMainAssetAtPath(item.id) }, new[] { item.id }, item.GetLabel(context, true));
}

public static void StartDrag(UnityEngine.Object[] objects, string[] paths, string label = null)
{
    if (paths == null || paths.Length == 0)
        return;
    DragAndDrop.PrepareStartDrag();
    DragAndDrop.objectReferences = objects;
    DragAndDrop.paths = paths;
    DragAndDrop.StartDrag(label);
}
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961