Tooltip value keep changing in OnGUI method when hovering

Hi ,

I met a problem today with OnGUI method from C# Script. The problem is I want to detect hover behaviour with GUI.tooltip on OnGUI method. I tried this way and it can detect the behaviour but I got weird result as the GUI.tooltip value always change everytime the OnGUI method is casted by the unity. My unity version is 3.4.0.

Some forum also discuss about this problem and somehow the problem can be answered by using the Update method.
This is the source : http://forum.unity3d.com/threads/45860-GUI.tooltip-unexpected-behaviour-on-mouse-hover-click

The example of the changing value is like this :

GUI.tooltip value: “Has mouse focus!”_________________time:1.02

GUI.tooltip value: “”_________________time:1.04

GUI.tooltip value: “Has mouse focus!”_________________time:1.04

GUI.tooltip value: “”_________________time:1.06

GUI.tooltip value: “Has mouse focus!”_________________time:1.06

GUI.tooltip value: “”_________________time:1.08

GUI.tooltip value: “Has mouse focus!”_________________time:1.08

GUI.tooltip value: “”_________________time:1.1

And now, my questions :

  1. how to solve the changing value in GUI.tooltip? Should I use the Update method like the forum or there is other way to get stable value from GUI.tooltip?

  2. Is this the recommended way to detect hover behaviour in Unity? If not, how?

Thank you for your help!

This is not an ideal way to detect hover behaviour, because it relies on comparing strings every frame.

Comparing strings is far less effecient than working with other datatypes like ints, floats, and (therefore) rectangles.

As far as I know, the best way to detect hover state is to pass the mouse position to the Rect.Contains method.

Example:

var boxRect    :  Rect  =  Rect(100, 100, 200, 60);
var labelRect  :  Rect  =  Rect(100, 160, 200, 60);

function OnGUI ()
{
    GUI.Box(boxRect, "I am a box");

    var mousePos : Vector2 = Input.mousePosition;

    /* invert the y-coordinate */

    mousePos.y = Screen.height - mousePos.y;

    var hoverMsg : String = "Hover State: ";

    if ( boxRect.Contains(mousePos) ) {
        hoverMsg += "YES";
    }

    else {
        hoverMsg += "NO";
    }

    GUI.Label(labelRect, hoverMsg);
}

There’s no OnMouseHover (yet?) but, you can check Enter, Exit and the time. Have Enter remember that you are on it, and when. Have exit remember you are off it. You’re hovering if you “remember” you entered it 0.5 seconds ago or more:

// Two vars used to decide if we are hovering:
bool overMe = false;
float lastEnterTime = 0.0f;

// "programmer" way of setting delay. Feel free to lose this and use 0.5 in OnGUI:
static const float HOVER_LAG = 0.5f;

void OnMouseEnter() { overMe=true; lastEnterTime=Time.time; }

void OnMouseExit() { overMe=false; }

// Again, "programmer" way. Feel free to lose this and
//    use the if directly in OnGUI:
public bool isHover() {
  return (overMe==true && Time.time > lastEnterTime + HOVER_LAG);
}

void OnGUI() {
  // are we hovering?
  if( isHover()==true ) {
    // do hover stuff:
  }
  // short version:
  //if(overMe==true && Time.time > lastEnterTime + 0.5f)