Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Radioactive116 · Aug 02, 2011 at 03:01 PM · gui-buttonmouse button

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Meltdown · Aug 02, 2011 at 05:33 PM

You can do something like the follwing...

  1. Go to Edit -> Project Settings -> Input

  2. Add an input type named 'MouseButtonLeft', set its 'Positive Button' to 'mouse 0'

  3. Add an input type named 'MouseButtonRight', set its 'Positive Button' to 'mouse 1'

  4. 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
         }
    
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zachypin · Aug 02, 2011 at 05:41 PM 0
Share

I went to that location but I don't see how you'd add an input type, just edit the current ones...

avatar image AVividLight · Aug 02, 2011 at 05:53 PM 0
Share

Under Axes, the first line should say Size... $$anonymous$$ake that number two bigger. Then, edit the two that apear at the bottom

avatar image Meltdown · Aug 02, 2011 at 06:04 PM 0
Share

Where it says 'Size', make that number bigger. Unity will create another line for you to create a new input type.

avatar image zachypin · Aug 02, 2011 at 06:12 PM 0
Share

thanks guys

avatar image zachypin · Aug 02, 2011 at 07:46 PM 0
Share

I'm having some problems with it. http://answers.unity3d.com/questions/151844/input-mouse-commands-no-reaction.html

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zachypin · Aug 02, 2011 at 05:25 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges