- Home /
How would I detect a right click with an event trigger?
I am using event triggers for my games UI menu but there is no option for a right click, only a click. How would I detect a right click on a button?
Answer by JackofTraes · Aug 22, 2017 at 02:24 AM
So there is an even simpler method. If anyone has a similar issue to mine (4.6 buttons that need to be right clicked), then you're in luck! Basically it involved augmenting your class (or creating a new one) with 'IPointerClickHandler'. Example below:
using UnityEngine;
using UnityEngine.EventSystems;
public class MyRightClickClass : MonoBehaviour, IPointerClickHandler {
public void OnPointerClick (PointerEventData eventData) {
if (eventData.button == PointerEventData.InputButton.Right) {
Debug.Log ("Right Mouse Button Clicked on: " + name);
}
}
}
So there it is; a simple alternative to the other examples on this page for those that are seeking a clean (and short) solution.
No problem, I am glad that this helped someone! :)
Close but not quite....
OnPointerClick takes a BaseEventData param.
You have to cast it inside your handler.
there are the 3 solutions :D
the first likes to write, the second get the job quick and dirty done
and there is always the genius that extends short and clean what unity already created.
thanks to all of you, you made my day xD
Answer by jenci1990 · Jan 18, 2015 at 08:15 AM
Creta a new c# script, call RightButtonEvent and paste it:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.Events;
using System.Collections;
[ExecuteInEditMode]
[AddComponentMenu("Event/RightButtonEvent")]
public class RightButtonEvent : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler {
[System.Serializable]public class RightButton : UnityEvent{}
public RightButton onRightDown;
public RightButton onRightUp;
private bool isOver = false;
void Start () {
}
void Update () {
if (Input.GetMouseButtonDown(1)) {
onRightDown.Invoke();
}
if (Input.GetMouseButtonUp(1)) {
onRightUp.Invoke();
}
}
public void OnPointerEnter(PointerEventData eventData) {
isOver = true;
}
public void OnPointerExit(PointerEventData eventData) {
isOver = false;
}
}
Now you have new component in your menu at "Component/Event/RightButtonEvent" add it to any UIObject.
Answer by aphenine · Aug 16, 2016 at 05:04 AM
This is the way (I think) the new EventSystem would like you to do it. (I got stressed figuring this out and there's no easy documentation, so I'm posting it on the stuff I came across to get to this answer):
using UnityEngine;
using UnityEngine.EventSystems;
public void PointerClickHandler( BaseEventData data )
{
PointerEventData pointerEventData = data as PointerEventData;
GameObject pressed = pointerEventData.pointerPress;
//Left click
if (pointerEventData.button == PointerEventData.InputButton.Left ) {
if( pointerEventData.clickCount == 1 )
{
OnLeftSingleClick( pressed );
}
else if( pointerEventData.clickCount == 2 )
{
OnLeftDoubleClick( pressed );
}
}
//Right click
else if (pointerEventData.button == PointerEventData.InputButton.Right ) {
if( pointerEventData.clickCount == 1 )
{
OnRightSingleClick( pressed );
}
else if( pointerEventData.clickCount == 2 )
{
OnRightDoubleClick( pressed );
}
}
//Middle click
else if (pointerEventData.button == PointerEventData.InputButton.Middle ) {
if( pointerEventData.clickCount == 1 )
{
OnMiddleSingleClick( pressed );
}
else if( pointerEventData.clickCount == 2 )
{
OnMiddleDoubleClick( pressed );
}
}
}
Connect that function through an event trigger on the thing you want pressed (in my case, this function is in the camera, because that works for now, and the EventTrigger was on a sphere in the scene)
What's Going On Here
The EventSystem, which is Unity's shiny new method of doing things, sends events out that get captured by EventTrigger, as every tutorial will tell you. So if you put an EventTrigger on your GameObject, it will respond to press and mouse events.
However, what they don't say, is that all the events the EventSystem generates come with event data that the event gets fired with. All the data from EventSystem events subclass BaseEventData.
If you write a function that takes a BaseEventData as a parameter and place it in a script attached to a GameObject, then when you select that GameObject in the EventsTrigger component, it will appear at the top of the available function list in the editor, separate from normal functions talked about here. The EventTrigger will automatically provide that function with stuff that you can then use. That's what I've done in PointerClickHandler(). It handles all Pointer Click events for the EventTrigger I set.
The StandAloneInputModule, which subclasses the PointerInputModule, uses their own pointer events, called the PointerEventData (a subclass of BaseEventData). The contain the good stuff, like what was last pressed, how many clicks there have been and which mouse button was doing the pressing.
The most useful things to know is that pointerPress tells you what GameObject the press was on and clickCount tells you the number of successive clicks within a certain time-out.
Very good solution. Now it's very easy to create a superclass with this method and the definition of the "Onclick" methods can be implemented in the childclass hiding all the behaviour.
I had to modify it a little to fit into my project, adding a structure like this:
public struct GameObjectEvent { public BaseEventData baseEventData; public GameObject gameObject; }
And then define the headers of methods with the new type of object (GameObjectEvent).
public abstract void On$$anonymous$$iddleSingleClick(GameObjectEvent goe);
This is usefull if you are interested in get the exact click point coordinates, for example in a point&click game.
Thanks a lot for such a great code snippet.