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

Issues Right Clicking UI Elements.

Discussion in 'UGUI & TextMesh Pro' started by TeamDefiant, May 4, 2021.

  1. TeamDefiant

    TeamDefiant

    Joined:
    Mar 29, 2017
    Posts:
    50
    I know this has been asked before, but none of the solutions I've found have worked for me. (Or my exact problem hasn't been fixed by someone else yet.)

    I have a button object (Using the built in Unity Button component) and it works fine when I left click it.
    I removed the button component and replaced it with a new class I called GUIClickController. It basically does what every single right click post has suggested to do.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Events;
    3. using UnityEngine.EventSystems;
    4.  
    5. public class GUIClickController : MonoBehaviour, IPointerClickHandler
    6. {
    7.     public UnityEvent onLeftClick = new UnityEvent();
    8.     public UnityEvent onMiddleClick = new UnityEvent();
    9.     public UnityEvent onRightClick = new UnityEvent();
    10.  
    11.     public void OnPointerClick(PointerEventData eventData)
    12.     {
    13.         switch(eventData.button)
    14.         {
    15.             case PointerEventData.InputButton.Left:
    16.                 onLeftClick.Invoke();
    17.                 Debug.Log("Left Click");
    18.                 break;
    19.  
    20.             case PointerEventData.InputButton.Middle:
    21.                 onMiddleClick.Invoke();
    22.                 Debug.Log("Middle Click");
    23.                 break;
    24.  
    25.             case PointerEventData.InputButton.Right:
    26.                 onRightClick.Invoke();
    27.                 Debug.Log("Right Click");
    28.                 break;
    29.         }
    30.     }
    31. }
    I'm adding a listener in my code like so: (myButton is defined at the top of the code as this is a prefab being spawned on the fly)
    Code (CSharp):
    1. [SerializeField] private GUIClickController myButton;
    myButton.onLeftClick.AddListener(OnLeftClickedMethod);
    myButton.onRightClick.AddListener(OnRightClickedMethod);

    For left clicking, this works just fine. But it won't trigger for middle or right clicks.
    Again, left clicking using this code works perfectly, as expected.

    I've searched the internet all day and found nothing, which is why I've resorted to asking this question yet again.
     
    Last edited: May 5, 2021
  2. TeamDefiant

    TeamDefiant

    Joined:
    Mar 29, 2017
    Posts:
    50
    Never mind, I've "fixed" it myself.

    I wrote my own button click code using the RectTransformUtility to detect if the mouse if over the UI element, then checking for the relevant mouse click to trigger the click event. It'll work until you have multiple buttons overlapping, (Like say on a popup box) then multiple button clicks will be detected. You can easily code around that issue, but I didn't need to for my problem, plus I'm not going to do all the work am I!

    Here's my code (Just replace the whole OnPointerClick method with this):

    Code (CSharp):
    1.     private RectTransform rectTransform;
    2.  
    3.     private void Awake()
    4.     {
    5.         rectTransform = GetComponent<RectTransform>();
    6.     }
    7.  
    8.  
    9.     private void LateUpdate()
    10.     {
    11.         if (RectTransformUtility.RectangleContainsScreenPoint(rectTransform, Input.mousePosition, null))
    12.         {
    13.             if (Input.GetMouseButtonDown(0)) onLeftClick.Invoke();
    14.             else if (Input.GetMouseButtonDown(2)) onMiddleClick.Invoke();
    15.             else if (Input.GetMouseButtonDown(1)) onRightClick.Invoke();
    16.         }
    17.     }