69 lines
2.2 KiB
C#
69 lines
2.2 KiB
C#
using TMPro;
|
|
using UnityEngine;
|
|
|
|
namespace Ashwild.UI
|
|
{
|
|
/// <summary>
|
|
/// Stamps the current build version onto a TextMeshPro label. Reads <see cref="Application.version"/>
|
|
/// (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.
|
|
/// </summary>
|
|
[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
|
|
|
|
/// <summary>
|
|
/// Caches the label reference so the component works even when assigned by RequireComponent.
|
|
/// </summary>
|
|
private void Awake()
|
|
{
|
|
if (label == null)
|
|
label = GetComponent<TextMeshProUGUI>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Writes the version each time the object is enabled, so re-opening a menu always shows it.
|
|
/// </summary>
|
|
private void OnEnable()
|
|
{
|
|
Refresh();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Public API
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
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
|
|
}
|
|
}
|