LabelMaker - jimdroberts/FishMMO GitHub Wiki

Description

The LabelMaker is a MonoBehaviour responsible for managing the creation, pooling, and display of 3D labels in the FishMMO client. It provides a singleton interface for efficient label reuse, reducing instantiation overhead by pooling Cached3DLabel instances. This component is typically used for floating text, damage numbers, and other temporary world-space UI elements.


API Access

Fields

  • private static LabelMaker instance

    Singleton instance of LabelMaker.

  • private Queue pool

    Pool of cached 3D labels for reuse.

Properties

  • public Cached3DLabel LabelPrefab3D

    Prefab used to instantiate new 3D labels.

  • internal static LabelMaker Instance

    Internal accessor for the singleton instance.

Methods

  • void Awake()

    Unity Awake method. Initializes the singleton instance and sets the object name.

  • public bool Dequeue(out Cached3DLabel label)

    Retrieves a label from the pool or instantiates a new one if the pool is empty.

  • public void Enqueue(Cached3DLabel label)

    Returns a label to the pool for reuse, or destroys it if the pool is unavailable.

  • public void ClearCache()

    Clears all cached labels from the pool and destroys their game objects.

  • public static Cached3DLabel Display3D(string text, Vector3 position, Color color, float fontSize, float persistTime, bool manualCache)

    Displays a 3D label at the specified position with the given properties. Returns the displayed label, or null if unavailable.

  • public static void Cache(Cached3DLabel label)

    Caches the given label for reuse.

  • public static void Clear()

    Clears all cached labels from the pool.


Basic Usage

Setup

  1. Attach LabelMaker to a GameObject in your scene and assign the LabelPrefab3D prefab.
  2. Use LabelMaker.Display3D to show a label in the world.
  3. Labels are automatically pooled and reused for efficiency.
  4. Use LabelMaker.Cache to manually cache a label if using manual caching mode.
  5. Use LabelMaker.Clear to clear all cached labels when needed (e.g., on scene change).

Example

// Display a floating label
var label = LabelMaker.Display3D("+100 XP", new Vector3(0, 2, 0), Color.yellow, 24f, 2f, false);

// Manually cache the label when done (if using manualCache = true)
LabelMaker.Cache(label);

// Clear all cached labels
LabelMaker.Clear();

Best Practices

  • Use the pooling system to minimize instantiation and destruction of label GameObjects.
  • Assign the LabelPrefab3D in the inspector for correct label instantiation.
  • Use Display3D for all floating text and temporary world-space UI elements.
  • Clear the label pool on scene changes to avoid memory leaks.
  • Use manual caching for labels that need to persist until a specific event occurs.
⚠️ **GitHub.com Fallback** ⚠️