- Home /
How do I attach Right Click to an EventSystem listener?
I'm trying to attach both a left click function call and a right click function call to an EventSystem listener. I seem to be successful with the left click but not the right click.
I'm currently trying to run a variable (int) that is stored from Update when you left or right click, but the EventSystem doesn't even bother registering right clicks, so the listener is never run for right clicks, is there some way to force the EventSystem to pick up on right clicks?
I was hoping to avoid running update every frame just to check for a right click as I only want to check for this right click when the UI is open (Right click is used for other things related to game play while the UI is closed).
public int currentMouseInput = -1;
public Button tempButton = new Button();
void Start() {
tempButton.onClick.AddListener(() =>
{
if (currentMouseInput == 0)
{
print("Left Click Working");
}
else if (currentMouseInput == 1)
{
print("Right Click Working");
}
});
}
void Update() {
if (Input.GetMouseButtonDown(0))
{
currentMouseInput = 0;
}
else if (Input.GetMouseButtonDown(1))
{
currentMouseInput = 1;
}
}
Answer by HarshadK · Feb 09, 2017 at 11:59 AM
AFAIK there is no default onClick
event to receive right click on a button. The onClick
is for left click only. But you can create one using OnPointerDown
from IPointerDownHandler
and OnPointerUp
from IPointerUpHandler
interfaces.
Sorry old, but appicable tomy concern I think:HOw do I get rightclik play in unity,like ue4 has ? Not a highly skilled programmer , hence my question--does THIS thread help to that end ?
ty
No, your question is not related to this one.
If you try to achieve a click on a GameObject, look at this question: https://answers.unity.com/questions/373818/how-to-detect-mouse-click-on-a-gameobject.html