# 注册操作处理程序

> Create action handlers for the search provider.

您可以为搜索提供程序注册操作。用户可以通过搜索结果中的**更多选项** (**⋮**) 图标访问已注册的操作。

> \*\*注意：\*\*注册操作处理程序和注册搜索提供程序是不同的过程。您可以为现有搜索提供程序注册新的操作处理程序。

要注册操作，必须创建一个带有 [`SearchActionsProvider`](/engine/6000.0/script-reference/unity-editor-search/search-actions-provider-attribute.md) 属性标签的函数。此函数必须返回 `IEnumerable<SearchAction>`。

以下示例展示了如何为资源搜索处理程序注册操作。

```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` 的操作。

以下示例来自资源搜索提供程序：

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