using TMPro; using UnityEngine; namespace Ashwild.UI { /// /// Stamps the current build version onto a TextMeshPro label. Reads /// (authored in Project Settings ▸ Player ▸ Version) so the displayed string always matches the /// real build without anyone editing the scene. Drop this on any UGUI element that carries a /// TextMeshProUGUI — typically a small corner label on the main menu, pause or loading canvas. /// [DisallowMultipleComponent] [RequireComponent(typeof(TextMeshProUGUI))] public class VersionLabel : MonoBehaviour { #region Serialized Fields [Header("Target")] [Tooltip("The label to write the version into. Auto-filled from this GameObject if left empty.")] [SerializeField] private TextMeshProUGUI label; [Header("Format")] [Tooltip("Wraps the version. Use {0} as the placeholder for Application.version, e.g. \"v{0}\".")] [SerializeField] private string format = "v{0}"; #endregion #region Unity Lifecycle /// /// Caches the label reference so the component works even when assigned by RequireComponent. /// private void Awake() { if (label == null) label = GetComponent(); } /// /// Writes the version each time the object is enabled, so re-opening a menu always shows it. /// private void OnEnable() { Refresh(); } #endregion #region Public API /// /// Composes the version string from the format and pushes it onto the label. Guards against a /// missing label and logs the culprit so the broken object is selectable from the console. /// public void Refresh() { if (label == null) { Debug.LogError($"[VersionLabel] '{name}' has no TextMeshProUGUI assigned — cannot show version.", this); return; } label.text = string.Format(format, Application.version); } #endregion } }