I want to make a few Keyboard shortcuts but i don’t necessary need the menu items as well.
Is that possible?
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!");
}
// ...
}
}
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 :