- Home /
Hot key without a Menu Item
I want to make a few Keyboard shortcuts but i don't necessary need the menu items as well. Is that possible?
Answer by Paul-Sinnett · Apr 23, 2015 at 03:23 PM
This adds a callback function to the Scene view - you can check in there for key code events. The initialize on load attribute makes it install the callback when Unity loads the project.
[InitializeOnLoad]
class MyKeyDetector
{
static MyKeyDetector ()
{
SceneView.onSceneGUIDelegate += CheckKeys;
}
public static void CheckKeys(SceneView view)
{
Event current = Event.current;
if (current.type == EventType.KeyDown && current.modifiers == EventModifiers.Control && current.keyCode == KeyCode.G)
{
Debug.Log("control G pressed!");
}
// ...
}
}
Nice! I was hoping to be able to use something simpler like the $$anonymous$$enuItem since it can detect Combinations easily (alt+shift+1 for example). But this could work as well since i was thinking to also create a window to edit the hotkeys.
I am going leave the question open for a bit more just in case we get any other input on this! if not ill accept you answer! Thanks for you input!
Answer by tribaleur · Jun 05, 2020 at 05:08 PM
Just for those wich are looking for an easier solution use : ShortcutAttribute
/// <summary>
/// Create a new MyCustomComponent in the selected GameObject
/// </summary>
[Shortcut("MyCustomAsset/Add MyCustomComponent", null, KeyCode.P, ShortcutModifiers.Shift)]
public static void AddComponentToSelectedGameObject (ShortcutArguments shortcutArguments) {
GameObject[] selection = Selection.gameObjects;
foreach(GameObject currentGO in selection) {
if (currentGO.AddComponent<MyCustomComponent>() != null) { Debug.Log("Add MyCustomComponent"); }
else{ Debug.Log("Not Add MyCustomComponent"); }
}
}
Then you can manage the shortCut in the Unity ShortucutManager :
Your answer
Follow this Question
Related Questions
Creating a new Editor Window | How (where) do I save field values? 2 Answers
Animation's List browser 0 Answers
Unity editor extension - create drag and drop (similar to Buildbox) 1 Answer
Creating a custom editor window with a grid 1 Answer
Interactive handles inside Handles.BeginGUI section 0 Answers