Game Development Reference
In-Depth Information
Now that you have the script, in order to use it we need to:
1.
Create a
UI
element called
HoverOverMe
on a
Canvas
and add the
Tooltip
script to it.
2.
Create another
UI
element for the
tooltip
, say an image (add a
Text
component as a child if you wish). Make sure to position it off the UI Canvas
so it does not block the UI on startup.
3.
Select the
HoverOverMe
GameObject and set the
Tooltip Item
property of
the
Tooltip
script to the UI element you created for the tooltip.
Done! Now when you run the project, the tooltip will pop up above the UI element
you attached the script to when the mouse moves over it and when it moves away
it will be hidden. In the example project, I created an
Image
and added the
ToolTip
script
to it. I then added another
Image
called
ToolTip
, this time with a
Text
control
as a child (setting the
Text Anchor
to
Fill
). Finally, I set the
Tooltip Item
property of
the script on the initial
Image
to my new ToolTip Image/Text GameObject. This was
the result:
Note, the Tooltip (shown in green) only appears when the mouse is over the
GameObject (shown in white).
Note: This is a very basic implementation and only works for
Screen
Space - Overlay
canvases. To use it with
Screen Space - Camera
or
World Space
canvases, you will have to translate the mouse position
with the
ScreenPointToRay
function (check out the
UIWindowBase
example later for more details).