How to detect mouse right or left click to button on new UI System on 4.6
5 Answers
5I needed that myself, so I forked Tim C.'s gist to create my own input handler. It requires ExtraMouseEvents.cs and IExtraMouseEventsHandler.cs to work. To use it, remove the Standalone Input Module component from the EventSystem in your scene, and add Mouse Input Module instead. Bonus feature: You can toggle drag support for the right and middle mouse buttons.
To use it you need to add scripts to your buttons/other controls which implement IRightPointerClickHandler, IRightPointerDownHandler, IRightPointerUpHandler, IMiddlePointerClickHandler, IMiddlePointerDownHandler or IMiddlePointerUpHandler. Example:
using UnityEngine;
using UnityEngine.EventSystems;
public class RightTest : MonoBehaviour, IRightPointerClickHandler
{
public void OnRightPointerClick(PointerEventData data)
{
Debug.Log("Right-click "+gameObject.name);
}
}
I think adding the Event Trigger component to the UI element is easier. Just follow these directions provided in this Answer by HarshadK:
- Add an Event Trigger component to your button game object.
- Click on Add New button and select PointerEnter.
- Now click on ‘+’ button to add a new item to the list of event of type PointerEnter(BaseEventData).
- Select the object containing your script.
- Now select the function to be called from the list of functions.
Now you've on pointer click event, assume that by the time of writtin tehre was not, so combine on pointer click with " Answer by cgriffiths · Aug 04 at 10:14 AM " and you've total right left mouse click control! thanks!!!
– TooManySugarAttach your function to an EventTrigger script with EventType PointerClick. Inside the function:
if (Input.GetMouseButtonUp( 0 ))
{
print( "left click" );
}
else if (Input.GetMouseButtonUp( 1 ))
{
print( "right click" );
}
else if (Input.GetMouseButtonUp( 2 ))
{
print( "middle click" );
}
Write the following code to the script and attach it to the button:
public bool mouseEntered { get; set; }
public void Update(){
if(mouseEntered){
if (Input.GetMouseButtonDown(1)) OnRightClick ();
}
}
Next, add the Event Trigger component to the button and set it up like in the picture:

Just use PointerEventData.pointerId on PC version.
It works nice, thanks!
– alexzzzzorb, this solution is great, thank you. Just wanted to add a quick note: For anyone having problems, changes in beta 19 will cause a few errors pop up when this code is installed. References to currentSelectedObject, lastSelectedObject and firstSelectedObject need to be updated to currentSelectedGameObject, lastSelectedGameObject and firstSelectedGameObject.
– DiscoJusticeThank you, your method helped me a lot
– WoolfYep, the GUI is still a work in process, so they change things around a lot. Hopefully my hacky input mechanism won't be necessary at all by the next beta :) I've updated the input module for beta 19, and will keep an eye out for beta 20 if it still doesn't have right-click functionality.
– orbpersonally I just use Input.GetMouseButtonUp(0 = left 1 = right) for clicks
– Louis_Watson