版本:Unity 6 (6000.0)
语言英语
  • C#

SearchProvider.trackSelection

建议更改

成功!

感谢您帮助我们提高 Unity 文档的质量。虽然我们无法接受所有提交,但我们确实会阅读用户提出的每项建议更改,并在适用情况下进行更新。

关闭

提交失败

由于某些原因,您的建议更改无法提交。请在几分钟后重试。感谢您抽出时间帮助我们提高 Unity 文档的质量。

关闭

取消

public Action<SearchItem,SearchContext> trackSelection;

说明

在选择项更改并且可以追踪时调用。

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