Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Can the UI buttons detect a right mouse-click?

Discussion in 'UGUI & TextMesh Pro' started by Zerodev, Nov 9, 2014.

  1. Zerodev

    Zerodev

    Joined:
    Oct 20, 2014
    Posts:
    2
    I'm instantiating buttons and adding listeners to detect when they're clicked. But I've noticed that this method only seems to detect a button-click if it came from the left-mouse button. The button doesn't register a right-mouse click.

    Is there a way to make the button react to a right-click? Perhaps even run a different script than it does with a left-click?

    Code (csharp):
    1.  
    2. public GameObject selector_ButtonPrefab;
    3.  
    4. void CreatePrefabButton()
    5. {
    6. GameObject newButton = Instantiate(selector_ButtonPrefab) as GameObject;
    7. newButton.GetComponent<Button>().onClick.AddListener(delegate {TestScript();});
    8. }
    9.  
    10. void TestScript()
    11. {
    12. Debug.Log("See me if you left-click, but not if you right-click.");
    13. }
    14.  
     
    PolygonPros and tigerleapgorge like this.
  2. jfarias

    jfarias

    Joined:
    Apr 8, 2014
    Posts:
    53
    Instead of using the AddListener, create a script and add it to your prefab button with something like:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.EventSystems;
    4.  
    5. public class ClickableObject : MonoBehaviour, IPointerClickHandler {
    6.  
    7.     public void OnPointerClick(PointerEventData eventData)
    8.     {
    9.         if (eventData.button == PointerEventData.InputButton.Left)
    10.             Debug.Log("Left click");
    11.         else if (eventData.button == PointerEventData.InputButton.Middle)
    12.             Debug.Log("Middle click");
    13.         else if (eventData.button == PointerEventData.InputButton.Right)
    14.             Debug.Log("Right click");
    15.     }
    16. }
    This works on any object, not just buttons.
    Hope it helps.
     
  3. Zerodev

    Zerodev

    Joined:
    Oct 20, 2014
    Posts:
    2
    This works beautifully, thanks!
     
    fullcorder likes this.
  4. redthrawn

    redthrawn

    Joined:
    May 8, 2013
    Posts:
    27
    Or, just go:
    Code (CSharp):
    1.  
    2. if (eventData.pointerId == -1)
    3.     Debug.Log("Left click");
    4. else if (eventData.pointerId == -3)
    5.     Debug.Log("Middle click");
    6. else if (eventData.pointerId == -2)
    7.     Debug.Log("Right click");
    8.  
     
    ChillyRolande, hms0589 and grimmgames like this.
  5. braur

    braur

    Joined:
    Jan 7, 2018
    Posts:
    7
    Hi, if you want to extend this example of some handy events that could be used to easy assign delegates in the inspector, you could go as follows :)

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.EventSystems;
    4. using UnityEngine.Events;
    5. public class GUIClickController : MonoBehaviour, IPointerClickHandler
    6.     {
    7.         public UnityEvent onLeft;
    8.         public UnityEvent onRight;
    9.         public UnityEvent onMiddle;
    10.  
    11.         public void OnPointerClick(PointerEventData eventData)
    12.         {
    13.             if (eventData.button == PointerEventData.InputButton.Left)
    14.             {
    15.                 onLeft.Invoke();
    16.             }
    17.             else if (eventData.button == PointerEventData.InputButton.Right)
    18.             {
    19.                 onRight.Invoke();
    20.             }
    21.             else if (eventData.button == PointerEventData.InputButton.Middle)
    22.             {
    23.                 onMiddle.Invoke();
    24.             }
    25.         }
    26.     }
    27.  
     
    zelderus, ega881124, Snake-M3 and 5 others like this.
  6. zoskeb

    zoskeb

    Joined:
    Feb 28, 2018
    Posts:
    1
    Thank you so much!!!!!
     
  7. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    387
  8. unity_sTKXHclFXh5sEA

    unity_sTKXHclFXh5sEA

    Joined:
    Nov 23, 2017
    Posts:
    3
    What if we bind the action using the AddListener() function ?

    Code (CSharp):
    1. Button btn = GetComponent<Button>();
    2. btn.onClick.AddListener(() => {
    3. if (Input.GetMouseButtonDown(1)) {
    4. // this is never reached when right mouse button is clicked !!
    5. }
    6. }
     
    Green11001 likes this.
  9. invisage

    invisage

    Joined:
    Jun 27, 2019
    Posts:
    23
    Another similar way with eventData, but a little easier to read, for those looking into it:

    Code (CSharp):
    1. public void OnPointerClick(PointerEventData eventData)
    2. {
    3.     if (eventData.button == PointerEventData.InputButton.Left)
    4.           DoSomething();
    5. }
     
  10. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    This has now helped me TWICE...the first time, and this time, when I totally forgot about how to do it all. I had forgotten to implement IPointerClickHandler. :)
     
    Last edited: Dec 7, 2021
    Westland likes this.
  11. trullilulli

    trullilulli

    Joined:
    Sep 4, 2013
    Posts:
    7
    This is a bit of a necropost, however i've figured a solution which is better and easier for me. My game already utilises the built in unity Button object but I just needed to add a right click to it.
    Code (CSharp):
    1. using UnityEngine.EventSystems;
    2. using UnityEngine.UI;
    3.  
    4. /// <summary>
    5. /// This is a custom button class that inherits from the default button. This allows us to have seperate events for left and right click, utilising all of the handy features of Button.
    6. /// </summary>
    7. public class UIButton : Button
    8. {
    9.     public UnityEngine.Events.UnityEvent onRightClick = new UnityEngine.Events.UnityEvent();
    10.     public override void OnPointerClick(PointerEventData eventData)
    11.     {
    12.         if (eventData.button == PointerEventData.InputButton.Left)
    13.         {
    14.             // Invoke the left click event
    15.             base.OnPointerClick(eventData);
    16.         }
    17.         else if (eventData.button == PointerEventData.InputButton.Right)
    18.         {
    19.             // Invoke the right click event
    20.             onRightClick.Invoke();
    21.         }
    22.     }
    23. }
    24.  
    now wherever you have "Button" just replace it with "UIButton" and you can add a button.OnRightClick.AddEventListener
     
    Last edited: Jul 28, 2023
    spilat12 likes this.