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'.
1 Answer
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.
OnGui
is deprecated, use the new UI instead. \$\endgroup\$