x


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.");
        }
    }
}
more ▼

asked Oct 10 '10 at 07:49 PM

Ralkarin gravatar image

Ralkarin
70 3 3 10

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.");
        }
    }
}
more ▼

answered Oct 11 '10 at 01:41 AM

Ralkarin gravatar image

Ralkarin
70 3 3 10

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3678
x1528
x53

asked: Oct 10 '10 at 07:49 PM

Seen: 1664 times

Last Updated: Mar 10 '12 at 12:34 PM