包含此页的版本:
不含此页的版本:
SearchProvider 类执行特定类型项的搜索,并管理缩略图、说明和子筛选器。
它具有以下基本 API:
public class SearchProvider
{
public SearchProvider(string id, string displayName = null);
// Creates an Item bound to this provider.
public SearchItem CreateItem(string id, string label = null, string description = null, Texture2D thumbnail = null);
// Utility functions to check whether the search text matches a string.
public static bool MatchSearchGroups(string searchContext, string content);
public static bool MatchSearchGroups(string searchContext, string content,
out int startIndex, out int endIndex);
// The provider's unique ID.
public NameId name;
// Text token to "filter" a provider (for example, "me:", "p:", and "s:").
public string filterId;
// This provider is only active when a search explicitly specifies it with
// its filterId.
public bool isExplicitProvider;
// Handler to fetch and format the label of a search item.
public FetchStringHandler fetchLabel;
// Handler to provide an async description for an item. Called just before
// Search displays the item.
// Allows a plug-in provider to fetch long descriptions only when
// Search needs them.
public FetchStringHandler fetchDescription;
// Handler to provider an async thumbnail for an item. Called just before
// Search displays the item.
// Allows a plug-in provider to fetch/generate previews only when
// Search needs them.
public PreviewHandler fetchThumbnail;
// Handler to support drag interactions. It is up to the SearchProvider
// to properly set up the DragAndDrop manager.
public StartDragHandler startDrag;
// Called when the selection changes and Search can track it.
public TrackSelectionHandler trackSelection;
// MANDATORY: Handler to get items for a search context.
public GetItemsHandler fetchItems;
// A Search Provider can return a list of words that help the user complete
// their search query.
public GetKeywordsHandler fetchKeywords;
// List of sub-filters that are visible in the FilterWindow for a
// SearchProvider (see AssetProvider for an example).
public List<NameId> subCategories;
// Called when the Search window opens. Allows the Provider to perform
// some caching.
public Action onEnable;
// Called when the Search window closes. Allows the Provider to release
// cached resources.
public Action onDisable;
// Int to sort the Provider. Affects the order of search results and the
// order in which providers are shown in the FilterWindow.
public int priority;
// Called when Search opens in "contextual mode". If you return true
// it means the provider is enabled for this search context.
public IsEnabledForContextualSearch isEnabledForContextualSearch;
}
当您启动搜索窗口时,它会调用onEnable,您可以使用它来缓存资源。
当您关闭搜索窗口时,它会调用onDisable,您可以使用它来释放资源。
由于搜索项列表使用虚拟滚动算法,因此某些SearchItem字段(例如,label,thumbnail和description)如果尚未提供,则按需获取。
要在创建项后填充这些字段,您需要初始化SearchProvider使用特定的处理程序 (fetchLabel,fetchDescription,fetchThumbnail).
您可以在trackSelection以使“搜索”在你使用鼠标或键盘选择搜索结果中的项目时执行某些作。例如,资产可在游戏或项目中使用的任何媒体或数据。资产可能来自在 Unity 外部创建的文件,例如 3D 模型、音频文件或图像。您还可以在 Unity 中创建一些资产类型,例如动画师控制器、混音器或渲染纹理。更多信息
请参阅术语表和场景场景包含游戏的环境和菜单。将每个唯一的场景文件视为一个独特的关卡。在每个场景中,你放置你的环境、障碍物和装饰品,基本上是将你的游戏设计和构建成碎片。更多信息
请参阅术语表提供程序使用trackSelection回调以 ping 搜索中的选定项目。
某些搜索提供程序返回可以拖放到场景中的项目。如果要创建项支持拖放的自定义提供程序,请实现startDrag.
例如,资产和场景提供程序填充DragAndDrop结构与相关项目 UID 进行交互,以允许适当的拖放交互。
当您使用 Alt Shift + C 快捷键打开“搜索”窗口时,它会启动上下文搜索,这意味着“搜索”会搜索具有焦点的窗口。
当您启动上下文搜索时,覆盖的提供商isEnabledForContextualSearch检查是否应启用它们,如以下示例所示:
// Taken from Scene hierarchy provider:
// Makes the provider part of the contextual search if the Scene view or the
// Hierarchy window has focus.
isEnabledForContextualSearch = () =>
QuickSearchTool.IsFocusedWindowTypeName("SceneView") ||
QuickSearchTool.IsFocusedWindowTypeName("SceneHierarchyWindow");