How to add shortcut to custom action? (ProBuilder)

I made this script to add a button to set the pivot the bottom of the mesh.
It works as it should but I also want to call it with a shortcut.
How can I do so? I tried to add a MenuAction but it just won’t show in Unity’s shortcuts manager.

using UnityEngine;
using UnityEngine.ProBuilder;
using UnityEngine.ProBuilder.MeshOperations;

namespace UnityEditor.ProBuilder.Actions
{
    [ProBuilderMenuAction]
    public class SetPivotLowerCenterAction : MenuAction
    {
        public const string Name = "Set Pivot @LowerCenter";

        public override ToolbarGroup group
        {
            get { return ToolbarGroup.Object; }
        }

        public override Texture2D icon
        {
            get { return null; }
        }

        public override TooltipContent tooltip
        {
            get { return k_Tooltip; }
        }
       
        internal const char CMD_SUPER       = '\u2318';

        // What to show in the hover tooltip window.  TooltipContent is similar to GUIContent, with the exception
        // that it also includes an optional params[] char list in the constructor to define shortcut keys
        // (ex, CMD_CONTROL, K).
        static readonly TooltipContent k_Tooltip = new TooltipContent(
            Name,
            Name,
            CMD_SUPER, 'K'
        );

        // Determines if the action should be enabled or shown as disabled in the menu.
        public override bool enabled
        {
            get { return MeshSelection.selectedObjectCount > 0; }
        }

        [MenuItem("MainMenu/Tools/ProBuilder/Object/" + Name)]
        private void Do()
        {
            // Object[] objects = new Object[MeshSelection.selectedObjectCount * 2];

            foreach (var mesh in MeshSelection.top)
            {
                Undo.RegisterCompleteObjectUndo(mesh, Name);
                Undo.RegisterCompleteObjectUndo(mesh.transform, Name);
            }
           
            foreach (var mesh in MeshSelection.top)
            {
                // TransformUtility.UnparentChildren(mesh.transform);
                var bounds = mesh.GetComponent<MeshRenderer>().bounds;
               
                var pivotPoint = bounds.center;
                pivotPoint.y = bounds.min.y;
               
                mesh.SetPivot(pivotPoint);
                mesh.Optimize();
                // TransformUtility.ReparentChildren(mesh.transform);
            }

            ProBuilderEditor.Refresh();
        }

        public override ActionResult DoAction()
        {
            if (MeshSelection.selectedObjectCount < 1)
                return ActionResult.NoSelection;

            // Object[] objects = new Object[MeshSelection.selectedObjectCount * 2];

            foreach (var mesh in MeshSelection.top)
            {
                Undo.RegisterCompleteObjectUndo(mesh, Name);
                Undo.RegisterCompleteObjectUndo(mesh.transform, Name);
            }
           
            foreach (var mesh in MeshSelection.top)
            {
                // TransformUtility.UnparentChildren(mesh.transform);
                var bounds = mesh.GetComponent<MeshRenderer>().bounds;
               
                var pivotPoint = bounds.center;
                pivotPoint.y = bounds.min.y;
               
                mesh.SetPivot(pivotPoint);
                mesh.Optimize();
                // TransformUtility.ReparentChildren(mesh.transform);
            }

            ProBuilderEditor.Refresh();

            return new ActionResult(ActionResult.Status.Success, Name);
        }
    }
}

Still interested to know about this :slight_smile:

Hi Feast,

MenuItem and the new Shortcut Attribute, which is probably more what you’re looking for, needs to be on a static function to work. I thought we had a public way to get the instance of the action in the toolbar but it’s not the case currently. To make it work, you can do something like this in your custom action:

[Shortcut("MainMenu/Tools/ProBuilder/Object/" + Name", KeyCode.K)]
static void TriggerActionFromShortcut()
{
    new SetPivotLowerCenterAction().DoAction();
}

Hope this helps

1 Like

I just noticed I have this message popping up when I added the shortcut.

But the shortcut works anyway.

6544186--740110--upload_2020-11-20_15-39-51.png

Did you leave the MenuItem attribute on the Do function?

using UnityEditor.ShortcutManagement;
using UnityEngine;
using UnityEngine.ProBuilder;
using UnityEngine.ProBuilder.MeshOperations;

namespace UnityEditor.ProBuilder.Actions
{
    [ProBuilderMenuAction]
    public class SetPivotLowerCenterAction : MenuAction
    {
        public const string Name = "Set Pivot @LowerCenter";
        [Shortcut("MainMenu/Tools/ProBuilder/Object/" + Name, KeyCode.V, ShortcutModifiers.Alt | ShortcutModifiers.Shift)]
        static void TriggerActionFromShortcut()
        {
            new SetPivotLowerCenterAction().DoAction();
        }

        public override ToolbarGroup group
        {
            get { return ToolbarGroup.Object; }
        }

        public override Texture2D icon
        {
            get { return null; }
        }

        public override TooltipContent tooltip
        {
            get { return k_Tooltip; }
        }
        // What to show in the hover tooltip window.  TooltipContent is similar to GUIContent, with the exception
        // that it also includes an optional params[] char list in the constructor to define shortcut keys
        // (ex, CMD_CONTROL, K).
        static readonly TooltipContent k_Tooltip = new TooltipContent(
            Name,
            Name
        );

        // Determines if the action should be enabled or shown as disabled in the menu.
        public override bool enabled
        {
            get { return MeshSelection.selectedObjectCount > 0; }
        }

        [MenuItem("MainMenu/Tools/ProBuilder/Object/" + Name)]
        private void Do()
        {
            // Object[] objects = new Object[MeshSelection.selectedObjectCount * 2];

            foreach (var mesh in MeshSelection.top)
            {
                Undo.RegisterCompleteObjectUndo(mesh, Name);
                Undo.RegisterCompleteObjectUndo(mesh.transform, Name);
            }
            
            foreach (var mesh in MeshSelection.top)
            {
                // TransformUtility.UnparentChildren(mesh.transform);
                var bounds = mesh.GetComponent<MeshRenderer>().bounds;
                
                var pivotPoint = bounds.center;
                pivotPoint.y = bounds.min.y;
                
                mesh.SetPivot(pivotPoint);
                mesh.Optimize();
                // TransformUtility.ReparentChildren(mesh.transform);
            }

            ProBuilderEditor.Refresh();    
        }

        public override ActionResult DoAction()
        {
            if (MeshSelection.selectedObjectCount < 1)
                return ActionResult.NoSelection;

            foreach (var mesh in MeshSelection.top)
            {
                Undo.RegisterCompleteObjectUndo(mesh, Name);
                Undo.RegisterCompleteObjectUndo(mesh.transform, Name);
            }
            
            foreach (var mesh in MeshSelection.top)
            {
                var bounds = mesh.GetComponent<MeshRenderer>().bounds;
                
                var pivotPoint = bounds.center;
                pivotPoint.y = bounds.min.y;
                
                mesh.SetPivot(pivotPoint);
                mesh.Optimize();
            }

            ProBuilderEditor.Refresh();

            return new ActionResult(ActionResult.Status.Success, Name);
        }
    }
}

That’s the code

You should remove [MenuItem(“MainMenu/Tools/ProBuilder/Object/” + Name)] at line 47, it’s causing the warning. The menu item attribute cannot be on a non static function so it’s currently being skipped and warning you that it won’t do anything.

1 Like

Indeed, I had forgotten I wrote this, thanks.