Cached3DLabel - jimdroberts/FishMMO GitHub Wiki
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.
-
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.
-
public TextMeshPro TMP
The TextMeshPro component used to display the label.
-
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.
- Attach
Cached3DLabel
to a GameObject with aTextMeshPro
component. - Use
Initialize
to set up the label's text, position, color, font size, persistence time, and caching mode. - The label will automatically cache itself after the specified time unless
manualCache
is true. - Use
SetPosition
andSetText
to update the label as needed.
// 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!");
- 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.