ドキュメント

Unity Engine

Unity Engine

アクションハンドラーの登録

Create action handlers for the search provider.
読み終わるまでの所要時間 3 分最終更新 1ヶ月前

検索プロバイダーにアクションを登録することができます。ユーザーは、検索結果の More Options (⋮) アイコンから、登録したアクションにアクセスできます。
注意アクションハンドラーの登録と検索プロバイダーの登録は異なる処理です。既存の検索プロバイダーに新しいアクションハンドラーを登録できます。
アクションを登録するには、
SearchActionsProvider
属性でタグ付けされた関数を作成します。この関数は
IEnumerable<SearchAction>
を返す必要があります。
以下の例は、Asset 検索プロバイダーにアクションを登録する方法を示しています。
[SearchActionsProvider]internal static IEnumerable<SearchAction> ActionHandlers(){ return new[] { new SearchAction("asset", "select", Icons.@goto, "Select asset...") { handler = (item, context) => { var asset = AssetDatabase.LoadAssetAtPath<Object>(item.id); if (asset != null) { Selection.activeObject = asset; EditorGUIUtility.PingObject(asset); EditorWindow.FocusWindowIfItsOpen( Utils.GetProjectBrowserWindowType()); } } }, new SearchAction("asset", "open", SearchIcon.open, "Open asset... (Alt+Enter)") { handler = (item, context) => { var asset = AssetDatabase.LoadAssetAtPath<Object>(item.id); if (asset != null) AssetDatabase.OpenAsset(asset); } }, new SearchAction("asset", "reveal", SearchIcon.folder, "Show in Explorer") { handler = (item, context) => { EditorUtility.RevealInFinder(item.id); } } };}

検索アクション

SearchAction
クラスはアクションを記述し、特定の
SearchItem
に対してアクションを実行するハンドラーを提供します。
public class SearchAction{ public SearchAction(string providerType, string name, Texture2D icon = null, string tooltip = null); public ActionHandler handler; public EnabledHandler isEnabled;}
providerType
は、アクションを登録するプロバイダーのユニークな ID です。
ActionHandler
は、以下のようなシグネチャを持っています。
// item: item that needs the action to be executed.// context: search context of the SearchTool when the item is executed.public delegate void ActionHandler(SearchItem item, SearchContext context);
isEnabled
述部でアクションを設定することができます。この述部は、特定のアイテムに対してアクションが使用可能かどうかを判断します。

コンテクスト検索アクション

検索結果の特定の種類のアイテムに対してコンテキスト依存 (右クリック) アクションを提供するには、検索プロバイダーに
context
という名前のアクションを登録します。
以下の例は、Asset 検索プロバイダーのものです。
new SearchAction(type, "context", null, "Context"){ handler = (item, context) => { var asset = AssetDatabase.LoadAssetAtPath<Object>(item.id); if (asset != null) { Selection.activeObject = asset; EditorUtility.DisplayPopupMenu( QuickSearchTool.ContextualActionPosition, "Assets/", null); } }}