- Home /
Use of mouse buttons in a mouse manager script
I'm working on a mouse manager script. I've instantiated a prefab from a list and displayed each item on the screen. I have the color change when the pointer enters and changes back when exiting with the event system.
What I want to do is have it when you click the left button, it does one operation and it does a second operation if you right click. Since this script is on a prefab, I wasn't sure if I should just use Update() or if there was some other recommended way to do it. Thanks!
Answer by Gilead7 · Jun 26, 2017 at 10:33 PM
I finally found the answer. If anyone else is having the same problem. This script will work!
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 ();
}
}
Just attach it to your game object and use it instead of OnPointerClick.
Answer by NorthStar79 · Jun 23, 2017 at 02:40 PM
instead of checking if mouse button clicked in update, just add an Event trigger (pointer Click) and create a method like clickHappened() and assign it to event trigger. in clickHappened() method , you can take base event data and check witch mouse button is clicked, left or right.
this way is more performance friendly as far as i know.
So I'm guessing the method needs a parameter taking in a base event data variable?
Unless I did it wrong, it's only showing me the coordinates of the click, not which button was clicked.
public void OnClick(BaseEventData clicker)
{
Debug.Log (clicker);
}
Results: Position: (189.0, 441.0) delta: (0.0, 0.0) eligibleForClick: True pointerEnter: DBName (UnityEngine.GameObject) pointerPress: DatabasePrefab(Clone) (UnityEngine.GameObject) lastPointerPress: pointerDrag: DatabasePrefab(Clone) (UnityEngine.GameObject) Use Drag Threshold: True Current Rayast: Name: DBName (UnityEngine.GameObject) module: Name: Canvas (UnityEngine.GameObject) eventCamera: sortOrderPriority: 0 renderOrderPriority: 0 distance: 0 index: 0 depth: 2 worldNormal: (0.0, 0.0, 0.0) worldPosition: (0.0, 0.0, 0.0) screenPosition: (189.0, 441.0) module.sortOrderPriority: 0 module.renderOrderPriority: 0 sortingLayer: 0 sortingOrder: 0 Press Rayast: Name: DBName (UnityEngine.GameObject) module: Name: Canvas (UnityEngine.GameObject) eventCamera: sortOrderPriority: 0 renderOrderPriority: 0 distance: 0 index: 0 depth: 2 worldNormal: (0.0, 0.0, 0.0) worldPosition: (0.0, 0.0, 0.0) screenPosition: (189.0, 441.0) module.sortOrderPriority: 0 module.renderOrderPriority: 0 sortingLayer: 0 sortingOrder: 0
Your answer
Follow this Question
Related Questions
How does the event delat and event current work? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Stack Overflow error on delegate call 2 Answers