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;
}
Search 창을 실행하면 리소스를 캐시하는 데 사용할 수 있는 onEnable이 호출됩니다.
Search 창을 닫으면 리소스를 해제하는 데 사용할 수 있는 onDisable이 호출됩니다.
검색 항목 목록은 가상 스크롤 알고리즘을 사용하므로 일부 SearchItem 필드(예: label, thumbnail, description)는 아직 제공되지 않은 경우 필요에 따라 가져옵니다.
항목이 생성된 후 해당 필드를 채우려면 특정 핸들러(fetchLabel, fetchDescription, fetchThumbnail)를 사용하여 SearchProvider를 초기화해야 합니다.
마우스나 키보드를 사용하여 검색 결과에서 항목을 선택할 때마다 검색 작업을 수행하도록 trackSelection에 콜백을 등록할 수 있습니다. 예를 들어 에셋 및 씬 제공자는 trackSelection 콜백을 사용하여 검색에서 선택한 항목을 핑합니다.
일부 검색 제공자는 씬에 드래그 앤 드롭할 수 있는 항목을 반환합니다. 항목이 드래그 앤 드롭을 지원하는 커스텀 제공자를 생성하는 경우 startDrag를 구현합니다.
예를 들어, 에셋과 씬 공급자는 적절한 드래그 앤 드롭 상호 작용이 가능하도록 관련 항목 UID로 DragAndDrop 구조를 채웁니다.
Alt Shift+C 단축키로 Search 창을 열면 컨텍스트 검색이 시작됩니다. 즉, 포커스가 있는 창을 검색합니다.
컨텍스트 검색을 시작하면 다음 예시와 같이 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");