Cached3DLabel - jimdroberts/FishMMO GitHub Wiki

Description

The Cached3DLabel is a MonoBehaviour used for displaying and managing 3D text labels in the FishMMO client. It supports automatic or manual caching (recycling) of labels, customizable appearance, and integration with the LabelMaker system for efficient label reuse. This component is typically used for floating text, damage numbers, or other temporary world-space UI elements.


API Access

Fields

  • private bool manualCache

    If true, label will only be cached manually; otherwise, cached after time expires.

  • private float remainingTime

    Remaining time before label is automatically cached.

Properties

  • public TextMeshPro TMP

    The TextMeshPro component used to display the label.

Methods

  • void Update()

    Unity Update method. Handles automatic caching of label when time expires.

  • public void Initialize(string text, Vector3 position, Color color, float fontSize, float persistTime, bool manualCache)

    Initializes the label with text, position, color, font size, persistence time, and caching mode.

  • public void SetPosition(Vector3 position)

    Sets the world position of the label.

  • public void SetText(string text)

    Sets the text of the label.


Basic Usage

Setup

  1. Attach Cached3DLabel to a GameObject with a TextMeshPro component.
  2. Use Initialize to set up the label's text, position, color, font size, persistence time, and caching mode.
  3. The label will automatically cache itself after the specified time unless manualCache is true.
  4. Use SetPosition and SetText to update the label as needed.

Example

// Create and initialize a label
cachedLabel.Initialize("+100 XP", new Vector3(0, 2, 0), Color.yellow, 24f, 2f, false);

// Move the label
cachedLabel.SetPosition(new Vector3(0, 3, 0));

// Change the label text
cachedLabel.SetText("Critical Hit!");

Best Practices

  • Use manual caching for labels that need to persist until a specific event occurs.
  • Use automatic caching for temporary labels (e.g., damage numbers) to optimize performance.
  • Always set the TextMeshPro reference in the inspector or via script before using the label.
  • Adjust the label's position based on text height for better visual alignment.
  • Integrate with a pooling system (like LabelMaker) to minimize instantiation overhead.
⚠️ **GitHub.com Fallback** ⚠️