How to catch right mouse button down on GUILayout.Button?

hi guys,
I got a problem. I want detect the event when the right mouse button hit a GUILayout.Button.
here is my code:

void OnGUI()
	{
		for(int i=0; i<4; ++i)
		{
			GUILayout.Button("button"+i) ;
			
			if(Event.current.button == 1  Event.current.type == EventType.MouseDown
				 GUILayoutUtility.GetLastRect().Contains(Event.current.mousePosition))
			{
				Debug.Log("mouse right button hit it!");
			}
		}
	}

It’s not work. Do you know how to do it? please help.
Thank you very much.

Have you tried using the GUILayout.Button call as a condition in the “if” statement?

if (GUILayout.Button("button"+i)) {
    if (Event.current.button == 1) {
        // Right button action.
    } else {
        // Left button action (if there is any).
    }
}
6 Likes