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 Ralkarin · Oct 10, 2010 at 07:49 PM · guiraycasttooltip

Using GUI.tooltip to avoid executing code over GUI elements

I'm trying to add GUI elements to a scene where I do Raycasting to select objects. But everytime I click a button, I am still raycasting. I would like to use an approach outlined in another question using the GUI.tooltip object.

See this link for the original question: http://answers.unity3d.com/questions/20536/ignoring-raycasts-hitting-gui

This approach doesn't work for me in Unity 3.0.0f5 The following C# Script will always print "got here" whether the cursor is over a GUI element or not. Am I doing something obviously wrong with this approach?

public class TestScript1 : MonoBehaviour { void OnGUI() { Rect boxRect = new Rect (10, 350,150,200); GUI.Button (boxRect, new GUIContent("Box Header", "BoxTooltip")); }

 void FixedUpdate () 
 {
     if (Input.GetMouseButtonDown(0) && GUI.tooltip == "")
     {
         print("got here.");
     }
 }

}

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
0
Best Answer

Answer by Ralkarin · Oct 11, 2010 at 01:41 AM

I hate to provide an answer to my own question, but I wanted to share what I've found.

It seems my error here is that I assumed GUI.tooltip would be persisted outside of the OnGUI function. This does not appear to be the case, so I don't think we can rely on it in an Update() function, much less in another class.

My current solution is to use the following classes: A static GUIManager class to save the state of whether the mouse is currently over a GUI element. In any GUI Script, add some code to check the Tooltip (or hit test a texture or whatever code you want), and set the global static variable on my GUIManager. Using this approach, I can then check to see whether GUIManager.MouseOverGUI == true when determining whether I want to raycast or not. If anyone sees a flaw, feel free to contribute.

EDIT: Fixed the code to work with scenes that have multiple scripts implementing OnGUI(). Be sure to attach GUIManager to your GUI object so LateUpdate() gets executed to reset the MouseOverGUI bit prior to the OnGUI() logic.

public class GUIManager : MonoBehaviour { public static bool MouseOverGUI = false;

 void LateUpdate()
 {
     MouseOverGUI = false;
 }

}

public class TestScript1 : MonoBehaviour {
void OnGUI() { Rect boxRect = new Rect (10, 350,150,200); GUI.Button (boxRect, new GUIContent("Box Header", "BoxTooltip"));

     if (GUI.tooltip != "")
         GUIManager.MouseOverGUI = true;
 }

 void Update() 
 {
     if (Input.GetMouseButtonDown(0) && !GUIManager.MouseOverGUI)
     {
         print("got here.");
     }
 }

}

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 Michard_ · Jan 25, 2014 at 10:17 PM 0
Share

Thanks man! That what's exactly what I was seaching for. I've two days already trying to figure out how to check a mouse click and having a tooltip at the same time. And this give me the result I needed. Thank you very much!

avatar image
0

Answer by Robbskinizer · Jun 13, 2013 at 01:44 PM

Im trying to do some GUI and need exact same functionality. Thou im having problem with the MouseOverGUI to be reseted before I press the button to false, its more like blinking true/false really fast. If im lucky and press at the exact right time i would get the MouseOverGUI = true.

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 Sprite101 · Feb 18, 2014 at 04:37 PM 0
Share

I had the same issue. You need to utilize the Repaint event as shown here:

http://answers.unity3d.com/questions/38611/why-cant-i-get-my-tooltip-to-show-only-when-there.html

avatar image
0

Answer by Michard_ · Jan 25, 2014 at 10:17 PM

Thanks man! That what's exactly what I was seaching for. I've two days already trying to figure out how to check a mouse click and having a tooltip at the same time. And this give me the result I needed. Thank you very much!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

GUI.button and onClick Terrain 2 Answers

Tooltip with GUI.TextField 2 Answers

Talking to an NPC 3 Answers

Raycasts hitting the crosshair 1 Answer

convert GUI.tooltip to int 1 Answer


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