Assign Undo and Redo shortcuts to the lateral buttons of the mouse while working in the Editor?

Hi,

Title says it all. Here is my poor attempt:

using UnityEngine;
using UnityEditor;

public class UndoRedoToMouse : Editor
{
    public void OnGUI()
    {
        Event e = Event.current;

        if (e.button == 0)
            Debug.Log ("Left click");

        if (e.keyCode == (KeyCode.Mouse3))
        {
            Debug.Log ("Button 3");
            Undo.PerformUndo ();
        }
        if (e.keyCode == (KeyCode.Mouse4))
        {
            Debug.Log ("Button 4");
            Undo.PerformRedo ();
        }
    }
}

What am I doing wrong?

Thanks.

Take a look here: Unity - Scripting API: Editor

The Editor is intended to form the basis of your own custom editors for a specific type of object (see sample code in the above page).

Is that what you are doing, or are you hoping to override the entire editor’s global notion of Undo/Redo?

I don’t think an editor script is a good way to go about accomplishing this goal. I don’t even 100% know that it’s possible to write an editor script that will respond to every input event the mouse sends from anywhere in the Editor.

I would say you should use whatever software your mouse came with to map those buttons to the menu items themselves (or the appropriate key combos).

1 Like

I should precise that I can’t even see the Debug.Log in the console, which indicates that I can’t even get the event to recognize I’m clicking the mouse buttons while working on my scene. Is the method “OnGUI()” even the correct “space” where I should put it?
This script is placed in an Editor folder but is not attached as a component to any gameobject.

@Kurt-Dekker
I’m not sure to see what applies to my case in the sample code of the page you linked…
Also I’m not trying to override any notion of Undo/Redo. I just want to have the same effect as when I click Ctrl + Z / Ctrl + Y (or Edit > Undo / Edit > Redo) but with my mouse.

@StarManta
I have a logitech mouse M500 and the program that controls its functionalities is Logitech SetPoint. But I don’t see anywhere allowing me to map these keys specifically for Unity. It works by default as Next / Previous on the internet, so I’m reluctant to change that for the whole mouse. I need it only for Unity.

Update:
So I found the solution to my problem.
Contrary to what I stated previously, it turns out I was able to map the Ctrl + Z and Ctrl + Y with Logitech Setpoint exclusively for Unity. Worked perfectly. No code needed indeed!
Thanks to all !!

1 Like