Ken Gordon
Ken Gordon

Reputation: 305

Unity right click outside of update

I'm working on a mouse manager script. I want to be able to left click on the instantiated text object and have it do one thing, and do something else when right clicked. Trouble is you can't send a parameter to an event trigger OnCLick and to send the base event data doesn't give you which button was clicked. I can't simply use Update because I only want them to right click on the text object not anywhere, especially since I want the right click to delete the object. I've looked and looked, one would think this were a common enough problem to find a solution, but alas.

I already have an OnEnter and OnExit, which changes the colors of the text. Anyone have a solution? Thanks!

Upvotes: 0

Views: 643

Answers (1)

Ken Gordon
Ken Gordon

Reputation: 305

In case anyone else has been pulling out their hair over this kind of issue. I finally found a solution and it works very well. So I will share it.

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Events;

public class RightClick : MonoBehaviour, IPointerClickHandler
{

  public UnityEvent leftClick;
  public UnityEvent middleClick;
  public UnityEvent rightClick;

  public void OnPointerClick(PointerEventData eventData)
{
    if (eventData.button == PointerEventData.InputButton.Left)
        leftClick.Invoke ();
    else if (eventData.button == PointerEventData.InputButton.Middle)
        middleClick.Invoke ();
    else if (eventData.button == PointerEventData.InputButton.Right)
        rightClick.Invoke ();
}

}

Upvotes: 1

Related Questions