Version: 2022.3
Language : English
Performing a search
Glossary

Registering an Action Handler

You can register actions for a Search Provider. Users can access registered actions via the More Options () icon in the search results.

Note: Registering an action handler and registering a Search Provider are different processes. You can register new action handlers for existing Search Providers.

To register an action, you create a function tagged with the SearchActionsProvider attribute. This function must return an IEnumerable<SearchAction>.

The following example shows how to register actions for the Asset Search Provider.

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

Search actions

The SearchAction class describes an action and provides a handler to execute the action on a specific SearchItem.

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

The providerType is the unique ID of the provider that you register the action for.

The ActionHandler has the following signature:

// 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);

You can set up an action with the isEnabled predicate, which determines whether an action is available for a specific item.

Contextual search actions

To provide contextual (right-click) actions for specific types of items in search results, register an action named context for the Search Provider.

The following example is from the Asset Search Provider:

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);
        }
    }
}
Performing a search
Glossary
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961