Tooltip System
Dynamic mouse-following tooltip that auto-positions to stay on screen. Supports rich text and custom layouts.
No APIs that Unity 6.0 to 6.6 made obsolete or removed. How this is checked
What it adds to your GameObject
A MonoBehaviour component: attach it to a GameObject. This is the Inspector you get, read from the source: 8 serialized fields with their defaults.
When to use it
Use in RPGs for item descriptions, in strategy games for unit stats, or in any UI-heavy game that needs hover information. Works well with inventory systems, skill trees, and shop interfaces. Pair with Simple Inventory for item tooltip data.
Setup
- Create a tooltip UI panel with TextMeshPro fields (header + body)
- Add CanvasGroup and ContentSizeFitter for auto-sizing
- Attach TooltipSystem to the tooltip panel
- Show: TooltipSystem.Show("Sword", "+10 Damage")
- Hide: TooltipSystem.Hide()
- Tooltip auto-flips to stay on screen
- Use on hover events: OnPointerEnter/Exit to show/hide
Features
- Singleton pattern with static Show/Hide for easy global access
- Mouse-following position with configurable offset
- Auto-flips tooltip to stay within screen boundaries
- Configurable show delay and smooth fade-in animation
- Separate header and body text fields with auto-hide empty header
- Forces layout rebuild for correct ContentSizeFitter sizing
Inspector fields
Every field Tooltip System exposes in the Inspector, with the default it starts at.
| Field | Type | Default | Group |
|---|---|---|---|
Tooltip RecttooltipRect | RectTransform | None | References |
Header TextheaderText | TextMeshProUGUI | None | References |
Body TextbodyText | TextMeshProUGUI | None | References |
Canvas GroupcanvasGroup | CanvasGroup | None | References |
Offsetoffset | Vector2 | (16, -16) | Settings |
Show DelayshowDelay | float | 0.3 | Settings |
Fade SpeedfadeSpeed | float | 8 | Settings |
Screen PaddingscreenPadding | float | 10 | Padding |
Public API
What other scripts can call on TooltipSystem.
| Member | Returns | Description |
|---|---|---|
static Show(string header, string body = "") | void | Show tooltip with header and body text. |
static ShowText(string text) | void | Show tooltip with body text only. |
static Hide() | void | Hide the tooltip. |
static Instance property | TooltipSystem |
Requirements
- Uses TextMeshPro (the TMPro namespace) for text.
- Reads the legacy Input Manager (UnityEngine.Input). If Project Settings > Player > Active Input Handling is set to Input System Package only, Unity throws an InvalidOperationException; set it to Both.
Common mistakes
The tooltip panel needs a CanvasGroup component for the fade effect — the script adds one automatically if missing, but it's better to set it up yourself. Add a ContentSizeFitter (Preferred/Preferred) so the panel resizes to fit text. The tooltip must be a child of a Canvas set to Screen Space - Overlay for correct mouse tracking.
Source code
TooltipSystem.cs, 140 lines, MIT licensed. Paste it into a new C# file of the same name.
using UnityEngine;
using TMPro;
/// <summary>
/// Mouse-following tooltip that auto-positions to stay on screen.
/// Call TooltipSystem.Show/Hide from any UI element.
/// </summary>
public class TooltipSystem : MonoBehaviour
{
public static TooltipSystem Instance { get; private set; }
[Header("References")]
[SerializeField] private RectTransform tooltipRect;
[SerializeField] private TextMeshProUGUI headerText;
[SerializeField] private TextMeshProUGUI bodyText;
[SerializeField] private CanvasGroup canvasGroup;
[Header("Settings")]
[SerializeField] private Vector2 offset = new Vector2(16f, -16f);
[SerializeField] private float showDelay = 0.3f;
[SerializeField] private float fadeSpeed = 8f;
[Header("Padding")]
[SerializeField] private float screenPadding = 10f;
private Canvas parentCanvas;
private float showTimer;
private bool isShowing;
private float targetAlpha;
private void Awake()
{
if (Instance == null)
Instance = this;
else
{
Destroy(gameObject);
return;
}
parentCanvas = GetComponentInParent<Canvas>();
if (canvasGroup == null)
canvasGroup = tooltipRect.GetComponent<CanvasGroup>();
if (canvasGroup == null)
canvasGroup = tooltipRect.gameObject.AddComponent<CanvasGroup>();
canvasGroup.alpha = 0f;
canvasGroup.blocksRaycasts = false;
}
private void Update()
{
if (!isShowing) return;
showTimer -= Time.unscaledDeltaTime;
if (showTimer <= 0f)
targetAlpha = 1f;
canvasGroup.alpha = Mathf.Lerp(canvasGroup.alpha, targetAlpha, Time.unscaledDeltaTime * fadeSpeed);
UpdatePosition();
}
private void UpdatePosition()
{
Vector2 mousePos = Input.mousePosition;
Vector2 tooltipPos = mousePos + offset;
// Get tooltip size
float width = tooltipRect.rect.width;
float height = tooltipRect.rect.height;
// Clamp to screen edges
if (tooltipPos.x + width > Screen.width - screenPadding)
tooltipPos.x = mousePos.x - width - offset.x;
if (tooltipPos.y - height < screenPadding)
tooltipPos.y = mousePos.y + height + Mathf.Abs(offset.y);
tooltipPos.x = Mathf.Max(screenPadding, tooltipPos.x);
tooltipPos.y = Mathf.Min(Screen.height - screenPadding, tooltipPos.y);
tooltipRect.position = tooltipPos;
}
/// <summary>
/// Show tooltip with header and body text.
/// </summary>
public static void Show(string header, string body = "")
{
if (Instance == null) return;
Instance.ShowInternal(header, body);
}
/// <summary>
/// Show tooltip with body text only.
/// </summary>
public static void ShowText(string text)
{
if (Instance == null) return;
Instance.ShowInternal("", text);
}
/// <summary>
/// Hide the tooltip.
/// </summary>
public static void Hide()
{
if (Instance == null) return;
Instance.HideInternal();
}
private void ShowInternal(string header, string body)
{
isShowing = true;
showTimer = showDelay;
targetAlpha = 0f;
if (headerText != null)
{
headerText.text = header;
headerText.gameObject.SetActive(!string.IsNullOrEmpty(header));
}
if (bodyText != null)
bodyText.text = body;
// Force layout rebuild for correct sizing
UnityEngine.UI.LayoutRebuilder.ForceRebuildLayoutImmediate(tooltipRect);
UpdatePosition();
}
private void HideInternal()
{
isShowing = false;
targetAlpha = 0f;
canvasGroup.alpha = 0f;
}
}