Files
test/Assets/PerfectWorld/Scripts/UI/TextOutlet.cs
2026-01-23 17:38:31 +07:00

65 lines
1.8 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using TMPro;
namespace BrewMonster.Scripts.UI
{
/// <summary>
/// Helper class to set text on both legacy Text and TextMeshPro components
/// Supports automatic formatting for Perfect World text codes
/// </summary>
[System.Serializable]
public class TextOutlet
{
[SerializeField] public Text legacy;
[SerializeField] public TMPro.TextMeshProUGUI tmp;
public void Set(string value)
{
if (legacy != null)
{
legacy.text = EC_Utility.FormatForLegacyText(value ?? string.Empty);
}
if (tmp != null)
{
tmp.text = EC_Utility.FormatForTextMeshPro(value ?? string.Empty);
}
}
/// <summary>
/// Set text with explicit formatting preference
/// </summary>
/// <param name="value">Raw text with formatting codes</param>
/// <param name="preferTextMeshPro">Whether to prefer TextMeshPro formatting</param>
public void SetFormatted(string value, bool preferTextMeshPro = true)
{
string formattedText = preferTextMeshPro ?
EC_Utility.FormatForTextMeshPro(value ?? string.Empty) :
EC_Utility.FormatForLegacyText(value ?? string.Empty);
if (legacy != null)
{
legacy.text = formattedText;
}
if (tmp != null)
{
tmp.text = formattedText;
}
}
public void SetColor(Color color)
{
if (legacy != null)
{
legacy.color = color;
}
if (tmp != null)
{
tmp.color = color;
}
}
}
}