Tooltip System

Dynamic mouse-following tooltip that auto-positions to stay on screen. Supports rich text and custom layouts.

File
TooltipSystem.cs
Unity
2022.3+
Level
intermediate
Size
2.5 KB · 140 lines
Added
2026-03-15
License
MIT
View source

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.

Inspector
Script TooltipSystem
References
Tooltip Rect None (RectTransform)
Header Text None (TextMeshProUGUI)
Body Text None (TextMeshProUGUI)
Canvas Group None (CanvasGroup)
Settings
Offset X16Y-16
Show Delay 0.3
Fade Speed 8
Padding
Screen Padding 10

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

  1. Create a tooltip UI panel with TextMeshPro fields (header + body)
  2. Add CanvasGroup and ContentSizeFitter for auto-sizing
  3. Attach TooltipSystem to the tooltip panel
  4. Show: TooltipSystem.Show("Sword", "+10 Damage")
  5. Hide: TooltipSystem.Hide()
  6. Tooltip auto-flips to stay on screen
  7. 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.

FieldTypeDefaultGroup
Tooltip RecttooltipRectRectTransformNoneReferences
Header TextheaderTextTextMeshProUGUINoneReferences
Body TextbodyTextTextMeshProUGUINoneReferences
Canvas GroupcanvasGroupCanvasGroupNoneReferences
OffsetoffsetVector2(16, -16)Settings
Show DelayshowDelayfloat0.3Settings
Fade SpeedfadeSpeedfloat8Settings
Screen PaddingscreenPaddingfloat10Padding

Public API

What other scripts can call on TooltipSystem.

MemberReturnsDescription
static Show(string header, string body = "")voidShow tooltip with header and body text.
static ShowText(string text)voidShow tooltip with body text only.
static Hide()voidHide the tooltip.
static Instance propertyTooltipSystem

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.

TooltipSystem.cs
C#
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;
    }
}