0
\$\begingroup\$

How do I make text appear when I mouse over GameObjects in-game? What is the Unity jargon for this? I'm assuming it's 'annotations'.

\$\endgroup\$
  • \$\begingroup\$ If you're talking about in the inspector in the editor, you would want TooltipAttribute. For ingame functionality or actual gameobjects you will need to build some custom code \$\endgroup\$ – Erik Overflow Jan 9 at 17:02
  • \$\begingroup\$ Note, OnGui is deprecated, use the new UI instead. \$\endgroup\$ – Draco18s no longer trusts SE Jan 9 at 17:36
  • \$\begingroup\$ if this is a gameobject having collider, then can you use raycast at mouse position to get the info about the gameobject and can have a UI as children of this gameobject and disable and enable it on successful hit by the raycast. \$\endgroup\$ – Aryaman Gupta Jan 10 at 5:42
  • \$\begingroup\$ The usual term for that is "tooltip". But AFAIK Unity doesn't have an out-of-the-box solution for that. \$\endgroup\$ – Philipp Jan 12 at 0:37
1
\$\begingroup\$

There's no Unity-specific jargon for this, because what you're describing is not the engine's job. That's part of your game, which means it's on you to implement this.

A rough outline of how this would work:

  • You'd make a data container script representing a "Hoverable" object.

    • You'd place an instance of this component on each object that you want to be able to get an annotation tooltip for, and configure its properties with the message/images or other data that you want to display.

    • You'd also ensure each such object has a collider to represent the shape and extents of its hoverable zone.

  • You'd make an annotation UI object that has the text fields or other visual interface you want for displaying the information about the hovered object.

  • You'd make a Selection / Hover Manager script responsible for keeping track of what the player is doing with their mouse. It would have a member variable to store the currently-hovered Hoverable component.

    • Each frame in Update, this script would convert the mouse position to a ray to fire into the scene. For each hit, you'd check to see if the hit collider has a Hoverable component attached. Then you have a few possibilities:

      • You find a Hoverable component, and it's the same as the currently-hovered Hoverable component. Do nothing and keep showing the current message.

      • You find no Hoverable component, and you didn't have a currently-hovered Hoverable component. Do nothing and keep showing no message.

      • You find no Hoverable component, and you previously had a currently-hovered Hoverable component. Now it's time to un-hover that component, hiding your annotation UI message and any hover highlight state.

        You might want to give a small tolerance of time or distance before un-hovering, to make the selection a bit more forgiving and reduce flickering if the player holds their mouse right near the edge.

      • You find a Hoverable component different from your currently-hovered Hoverable component. Now it's time to show your annotation, populating it with the message/info from the Hoverable component you found and setting it as your currently-hovered Hoverable.

    The selection / hover manager could manipulate the UI object(s) to display the message directly, or just publish events on hover state changes that other objects subscribe to, taking responsibility to update their own states when signaled.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.