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

> Create action handlers for the search provider.

検索プロバイダーにアクションを登録することができます。ユーザーは、検索結果の **More Options** (**⋮**) アイコンから、登録したアクションにアクセスできます。

> **注意**アクションハンドラーの登録と検索プロバイダーの登録は異なる処理です。既存の検索プロバイダーに新しいアクションハンドラーを登録できます。

アクションを登録するには、[`SearchActionsProvider`](/engine/6000.0/script-reference/unity-editor-search/search-actions-provider-attribute.md) 属性でタグ付けされた関数を作成します。この関数は `IEnumerable<SearchAction>` を返す必要があります。

以下の例は、Asset 検索プロバイダーにアクションを登録する方法を示しています。

```CSharp
[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`](/engine/6000.0/script-reference/unity-editor-search/search-action.md) クラスはアクションを記述し、特定の `SearchItem` に対してアクションを実行するハンドラーを提供します。

```CSharp
public class SearchAction
{
    public SearchAction(string providerType, string name,
                        Texture2D icon = null,
                        string tooltip = null);
    public ActionHandler handler;
    public EnabledHandler isEnabled;
}
```

`providerType` は、アクションを登録するプロバイダーのユニークな ID です。

`ActionHandler` は、以下のようなシグネチャを持っています。

```CSharp
// 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 検索プロバイダーのものです。

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