Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Standard ToolTip Behavior?

Discussion in 'Immediate Mode GUI (IMGUI)' started by CBot, Mar 5, 2008.

  1. CBot

    CBot

    Joined:
    Jan 25, 2008
    Posts:
    29
    I want to create a tooltip that appears after a short pause hovering over a button, and stays in the same place, and then disappears (standard stuff right? :) ). I've got it working (sort of), but the code is far more elaborate than what I was expecting. Perhaps there is an easier way? Is this functionality built-in but I just missed it?

    Details of my Method:
    Every time the tooltip changes, record the tooltip, current mouse position, and the time it happened. I then display a tooltip if the proper time has elapsed.

    Code (csharp):
    1.  
    2. if (Event.current.type == EventType.Repaint) {
    3.             if (mLastToolTip != GUI.tooltip) {
    4.                 mLastToolTipHoverStart = Time.time;
    5.                 mLastToolTip = GUI.tooltip;
    6.                 mLastToolTipWidth = GetMinGUIWidth(mLastToolTip);
    7.                 mLastToolTipPosition.x = Input.mousePosition.x - mLastToolTipWidth / 2;
    8.                 mLastToolTipPosition.y = buttonTop - 3*screenPadding;
    9.             }
    10.            
    11.             var deltaTime : float = Time.time - mLastToolTipHoverStart;
    12.             if (mLastToolTip != ""  deltaTime > kDelayUntilToolTipOn  deltaTime < kDelayUntilToolTipOff) {
    13.                 GUI.Label(Rect (mLastToolTipPosition.x, mLastToolTipPosition.y, mLastToolTipWidth, buttonHeight), mLastToolTip);
    14.             }
    15.         }
    16.