Hi there!
I’m trying to highlight a part of the text with a specific color stored in the scirptableObject using smart strings.
The line goes as follows: You need to <color=#{global.fixableOutlineColor}>clean up the yard and the house, and then start a <color=#{global.fixableOutlineColor}>fire at home.
I’m able to access the color variable. It is values are displayed on screen.
If this would be happening in a script I’d use ColorUtility.ToHtmlStringRGB(), but I’m trying to avoid creating an extra script for a single use in the whole game.
I guess I could also make an int field in the scriptableObject that would simply store the hex code of the color, but I’m hoping there is a better solution then creating extra variables.
So, can I turn it somehow into a hex code right inside the smart string without referring to making C# script?
using UnityEngine.Localization;
using UnityEngine.Localization.SmartFormat.Core.Extensions;
[DisplayName("Color Formatter")]
public class ColorFormatter : FormatterBase
{
public override string[] DefaultNames => new string[] { "color" };
public override bool TryEvaluateFormat(IFormattingInfo formattingInfo)
{
if (formattingInfo.CurrentValue is Color color)
{
formattingInfo.Write(ColorUtility.ToHtmlStringRGB(color));
return true;
}
return false;
}
}
Now add this LocalizationSettings/String Database/Smart Format/Formatters, move it to around the midpoint in the list.
if i have localized text with int variables, how can i change color of this variables in runtime, after change their value for example? For example in eng loc text looks like:
Apply {damage} damage
“damage” in brackets is IntVariable
public class DamageValueFormatter : FormatterBase
{
public override string[] DefaultNames => new string[] { "damageInfo" };
public override bool TryEvaluateFormat(IFormattingInfo formattingInfo)
{
if (formattingInfo.CurrentValue is DamageChangeValue damageInfo)
{
if (damageInfo.Sign < 0)
formattingInfo.Write($"<color=red>{damageInfo.Damage}</color");
else if (damageInfo.Sign > 0)
formattingInfo.Write($"<color=green>{damageInfo.Damage}</color");
else
formattingInfo.Write($"<color=white>{damageInfo.Damage}</color");
return true;
}
return false;
}
}
[Serializable]
public class DamageChangeValue : IVariable
{
public int Damage;
public int Sign;
public object GetSourceValue(ISelectorInfo selector)
{
return new { Damage, Sign };
}
}
the line
Apply {0:damageInfo():damage} damage
but something goes wrong)) I add it in localization settings and setup in inspector: