I have this Smart String that I want to format:
{Highlighted:{CutLogCount}}
The Highlighted source is a custom formatter that replaces it with a TextMeshPro text style, which in this case firstly evaluates the format to:
<style=Highlighted>{CutLogCount}</style>
I also want to format the CutLogCount variable though, which I add as a local variable of type int.
Right now, I’m formatting the nested source like this, which is obviously hacky, but I haven’t been able to implemented it “properly” using the provided API and the IFormattingInfo parameter.
Any help would be greatly appreciated!
[Serializable]
[DisplayName("Text Style Formatter")]
public sealed class TextStyleFormatter : FormatterBase
{
private const string DefaultFormat = "<style={0}>{1}</style>";
[SerializeField] private string _format = DefaultFormat;
public override string[] DefaultNames => new[] { "" };
public override bool TryEvaluateFormat(IFormattingInfo formattingInfo)
{
if (formattingInfo.CurrentValue is not TMP_Style style)
{
return false;
}
var content = formattingInfo.Format.RawText;
if (content.StartsWith("{") && content.EndsWith("}") && formattingInfo.FormatDetails.FormatCache.LocalVariables.TryGetValue(content[1..^1], out var variable))
{
content = variable.GetSourceValue(null).ToString();
}
var text = string.Format(_format, style.name, content);
formattingInfo.Write(text);
return true;
}
}