- Home /
How do I tell which mouse button was used to click a gui button?
I have a GUI button that is supposed to do one thing when left clicked, and another thing when right clicked. Since
if (GUI.Button(Rect(0, 0, 20, 20), "hello world"))
returns true for any mouse button, I need a way of detecting which mouse button was pressed. I tried just using Input.GetMouseButton(), but
Input.GetMouseButton(0)
is always false on a GUI button. Is this even possible?
Answer by Meltdown · Aug 02, 2011 at 05:33 PM
You can do something like the follwing...
Go to Edit -> Project Settings -> Input
Add an input type named 'MouseButtonLeft', set its 'Positive Button' to 'mouse 0'
Add an input type named 'MouseButtonRight', set its 'Positive Button' to 'mouse 1'
On your scene manager script.. write something like the following...
string lastMouseButtonPressed = "";
void Update () { if (Input.GetButtonDown("MouseButtonLeft")) lastMouseButtonPressed = "Left button"; if (Input.GetButtonDown("MouseButtonRight")) lastMouseButtonPressed = "Right button"; } void OnGUI() { GUI.Label(new Rect(0, 0, 100, 100), lastMouseButtonPressed); if (GUI.Button(new Rect(20, 20, 100, 100), "My Button") && lastMouseButtonPressed == "Left Button") // Do something with button when left button pressed on it if (GUI.Button(new Rect(20, 20, 100, 100), "My Button") && lastMouseButtonPressed == "Right Button") // Do something with button when right button pressed on it }
I went to that location but I don't see how you'd add an input type, just edit the current ones...
Under Axes, the first line should say Size... $$anonymous$$ake that number two bigger. Then, edit the two that apear at the bottom
Where it says 'Size', make that number bigger. Unity will create another line for you to create a new input type.
I'm having some problems with it. http://answers.unity3d.com/questions/151844/input-mouse-commands-no-reaction.html
Answer by DayyanSisson · Aug 02, 2011 at 05:22 PM
What are you asking? Are you asking the script to identify what button is pressed so the outcome is different (left click on button means level 1, right click means level 2). Or do you just want the user to click a specified button. For that, you need:
(Input.GetButtonDown("Fire 1"))
If so, change the "fire 1" to whatever specified button. If not, clear up what you're trying to say.
If you right click on a button one thing would happen, if you left click on the button something else would happen. How do you check which button was clicked on the mouse when the button was pushed is the question.
Your answer
Follow this Question
Related Questions
How to make gui image button revolve 2 Answers
Look like a Website Menue! 1 Answer
Obj appear and disapear on touch gui buttom 1 Answer
How to create a gui button inside an if statement? 1 Answer
Checking for idle hover using tooltips 0 Answers