Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
4
Question by TheMorten · May 17, 2010 at 10:36 AM · guibuttonmouse

Right-click button using GUI class?

Hey, I am trying to use the standard GUI class to implement buttons... As far as just clicking them, everything was pretty easy:

function OnGUI { var myButton = GUI.Button(Rect(), myString)

if(myButton) { //Do something crazy } }

However, if I want something different to happen when the user right-clicks the button, how could that be done? I'm thinking something like:

function OnGUI { var myButton = GUI.Button(Rect(), myString)

if(myButton) { if(mouseButton1) { //Action 1 } else { //Action 2 } } }

Does that make sense? If it does, how do I implement it "correctly"?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
12
Best Answer

Answer by jonas-echterhoff · May 18, 2010 at 01:27 AM

You'll want to look at the current GUI event to see which button was pressed. Try this code:


    if (GUILayout.Button("MyButton")
    {
        if (Event.current.button == 0)
            Debug.Log ("MyButton was clicked with left mouse button.");
        else if (Event.current.button == 1)
            Debug.Log ("MyButton was clicked with right mouse button.");
    }
Comment
Add comment · Show 3 · 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 TheMorten · May 18, 2010 at 06:46 AM 0
Share

Can I also add keyboard modifiers to that? (Like holding ctrl)? Well, I guess I can, but how? :P

avatar image jonas-echterhoff ♦♦ · May 18, 2010 at 05:01 PM 0
Share

if (Event.current.control) Debug.Log ("Ctrl was held");

avatar image TheMorten · May 19, 2010 at 09:37 AM 0
Share

Awesome, thanks!

avatar image
1

Answer by Tobias · May 17, 2010 at 11:48 AM

Maybe sth like that?

private var actives = false; private var actives2= false;

function OnMouseDown () { if(Input.GetMouseButton(1)){ actives = actives ? false : true; } if(Input.GetMouseButton(0)){ actives2= actives2 ? false : true; } }

function OnGUI () { if (actives) { GUI.Label (Rect (50,50,100,50), "test"); } if (actives2) { GUI.Label (Rect (50,50,100,50), "test"); } }

Comment
Add comment · Show 3 · 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 TheMorten · May 17, 2010 at 03:18 PM 0
Share

That seems to require that I create every button as a separate object in the hierarchy, and attach this script to them individually? I'm going to have 20+ buttons in the final application, so that seems a bit excessive... :/

Or am I misunderstanding this completely?

avatar image Tobias · May 17, 2010 at 04:35 PM 0
Share

Why you have buttons in your hierarchy? This script creates the buttons. Just attach this to your main camera and add enough buttons into the javascript code. Or do I misunderstand sth?

avatar image TheMorten · May 18, 2010 at 06:44 AM 0
Share

Ah, I see now... But it is still a kind of "backwards" way of doing it, compared to what I've been scripting so far. Thanks for your answer though. :)

avatar image
0

Answer by ramsey05 · Oct 06, 2021 at 04:54 PM

actives = actives ? false : true;

means

actives = !actives;

Comment
Add comment · 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

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

1 Person is following this question.

avatar image

Related Questions

How to fire gui button on mouse right release 2 Answers

2 Bugs in my game: mouse control takes some time to load and Menu not working. 0 Answers

Detect Mouse Click OnGUI 1 Answer

Menu button needs to be double clicked. 4 Answers

Disable a GUI while left mouse button is Down? 2 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