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
1
Question by Tsanas · Feb 07, 2012 at 12:38 AM · guiclicktoggletooltip

GUI toggle on click, unless on GUI.

Hi there.

Im having a hard time creating a simple gui toggle.

I want the GUI to toggle between on/off when the player clicks anywhere on the screen, UNLESS the click is on the GUI.

Im using tooltips to know whenever the mouse is over a gui-element, but the problem seems to be that during the actual click, the tooltip is null. Even when the mouse is still over the gui.

This is what i have so far:


 private string currentTooltip = "";

 void Update ()
 {
     if (string.IsNullOrEmpty(currentTooltip))
     {
         if (Input.GetMouseButtonDown(0))
         {
             DisplayBunkerGUI = DisplayBunkerGUI ? false : true;
         }
     }
 }

 void OnGUI()
 {
     if (DisplayBunkerGUI)
     {
          GUI.Button(new Rect(50, 50, 100, 100), ToolTipEnter);
     }
 
     currentTooltip = GUI.tooltip;
 }


So when i click outside the button, the GUI toggles fine, but when I click on the button, the GUI still toggles.

Anyone got any suggestions?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by aldonaletto · Feb 07, 2012 at 01:21 AM

A simple way to avoid the button area is to use buttonRect.Contains(Vector2 point) : it returns true when the rect contains the specified point. There's a trick, however: GUI Y is upside down, thus we must convert the mouse position Y to have meaningful results

Rect rButton = new Rect(50, 50, 100, 100); // define button rect

void Update(){ if (Input.GetMouseButtonDown(0)){ bool inButton = false; if (DisplayBunkerGUI){ // if button is being displayed... Vector2 mPos = Input.mousePosition; // check button area: mPos.y = Screen.height - mPos.y; // transform Y to GUI space... inButton = rButton.Contains(mPos); // check if mouse inside button rect } if (!inButton){ // if not over button... DisplayBunkerGUI = !DisplayBunkerGUI; // toggle variable } } }

void OnGUI(){ if (DisplayBunkerGUI){ GUI.Button(rButton, ToolTipEnter); // use rButton to draw the button } }

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 Tsanas · Feb 07, 2012 at 02:59 AM 0
Share

I've ended up using the buttons Rect.contains(Event.current.mousePosition) ins$$anonymous$$d.

$$anonymous$$ore info here

avatar image Berenger · Feb 08, 2012 at 10:07 PM 0
Share

Would it be usefull to split the contains in two if, so you check the x first, and the y only if you pass the x ? If there is a lot of buttons I mean, and if you aim at a device without much power.

avatar image aldonaletto · Feb 09, 2012 at 12:31 PM 1
Share

@Berenger, I suppose the Unity function Rect.Contains does something like this, but probably in C++ compiled to native code, which is much faster than anything we can write in scripts.
Our scripts are compiled to CIL, a kind of interpreted assembler with superpowers. Unfortunately, super-speed isn't among these superpowers: the interpretation process and other house keeping jobs eat precious CPU cycles.

avatar image aldonaletto · Feb 09, 2012 at 12:37 PM 0
Share

@Tsanas, good alternative: Event.current.mousePosition is Input.mousePosition in the GUI "upside-down-Y" fashion, thus there's no need to invert Y.

avatar image Berenger · Feb 09, 2012 at 03:26 PM 0
Share

Oh, good to know, thanks :)

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Pause Button Problems 2 Answers

Close my GUI button by repressing the same Hot-key. 3 Answers

How to assign a different tooltip to each GUI button? 0 Answers

Navigating GUI with gamepad on GUI.Toggle 0 Answers

C# Multiple GUI.Tooltips 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