Untitled
unknown
csharp
a year ago
5.8 kB
16
Indexable
using System.Diagnostics;
using System.Text.RegularExpressions;
using UnityEditor;
[InitializeOnLoad]
//Just holds a reference to the colors to be used on the texts
public static class TextFormating
{
//IMPORTANT!!! DO NOT MODIFY THE VARIABLES' NAMES.
//Texts formated from inspector would NOT update to those changes and they would be unable to format properly!
public static string healthColor = "<color=#13A638>";
public static string healthIcon = "<sprite name=\"Health\">";
public static string resourceColor = "<color=#35A2C5>"; //This one is variable, use this one for mana mainly
public static string resourceIcon = "<sprite name=\"Resource\">";
public static string armorColor = "<color=#FFFF00>";
public static string armorIcon = "<sprite name=\"Armor\">";
public static string magicResistColor = "<color=#240D84>";
public static string magicResistIcon = "<sprite name=\"MagicResist\">";
public static string attackDamageColor = "<color=#E06A0F>";
public static string attackDamageIcon = "<sprite name=\"AttackDamage\">";
public static string attackSpeedColor = "<color=#D3CC79>";
public static string attackSpeedIcon = "<sprite name=\"AttackSpeed\">";
public static string healingPowerColor = "<color=#65B765>";
public static string healingPowerIcon = "<sprite name=\"HealingPower\">";
public static string moveSpeedColor = "<color=#7ECAD4>";
public static string moveSpeedIcon = "<sprite name=\"MovementSpeed\">";
public static string spellPowerColor = "<color=#3E94D1>";
public static string spellPowerIcon = "<sprite name=\"SpellPower\">";
public static string castSpeedColor = "<color=#A8DCF8>";
public static string castSpeedIcon = "<sprite name=\"CastSpeed\">";
public static string critChanceColor = "<color=#CE1417>";
public static string critChanceIcon = "<sprite name=\"CritChance\">";
public static string critMultColor = "<color=#9B0008>";
public static string critMultIcon = "<sprite name=\"CritMult\">";
public static string healthRegenColor = "color=#42C65E>";
public static string healthRegenIcon = "<sprite name=\"HealthReg\">";
public static string resourceRegenColor = "<color=#6189AA>"; //Same as with resource
public static string resourceRegenIcon = "<sprite name=\"ResourceRegen\">";
public static string incomingDamageMultColor = "<color=#757575>";
public static string incomingDamageMultIcon = "<sprite name=\"IncomingDamageMult\">";
public static string damageDealtMultColor = "<color=#654998>";
public static string damageDealtMultIcon = "<sprite name=\"DamageDealtMult\">";
public static string physicalDamageColor = "<color=#BF4936>";
public static string magicDamageColor = "<color=#6B64ED>";
public static string healingColor = "<color=#69D680>";
public static string shieldingColor = "<color=#878C99>";
public static string highlightColor = "<color=#FF9F00>";
public static string FormatText(string inputString)
{
if (string.IsNullOrEmpty(inputString))
{
return "";
}
//Searchs for coinciedences in the text of "{string}" type, then gets the value between the "{}" and, if it matches a parameter on TextFormating, it's replaced by the value of that parameter
return Regex.Replace(inputString, "\\{(.*?)\\}", match =>
{
//From the coincidence of "{string}", gets only the "string" part
string key = match.Groups[1].Value;
try
{
//Searchs for a field on TextFormating that matches the given string
var field = typeof(TextFormating).GetField(key);
if (field != null)
{
//Returns the field as a string that will replace the match found "{string}"
return field.GetValue(null).ToString();
}
else
{
Debug.Print("Field not found in textFormating: {" + key + "} !!");
return match.Value;
}
}
catch
{
return match.Value;
}
}
);
}
//Follows the same logic as the above one, but instead of applying the formating, checks if the text has a valid format
public static bool ValidateFormating(string inputString)
{
if (string.IsNullOrEmpty(inputString))
{
return true;
}
foreach (Match match in Regex.Matches(inputString, "\\{(.*?)\\}"))
{
string key = match.Groups[1].Value;
var field = typeof(TextFormating).GetField(key);
if (field == null)
{
Debug.Print("Field not found in textFormating: {" + key + "} !!");
return false;
}
}
return true;
}
//Returns the invalid inputs, mainly for debugging
public static string GetInvalidFormats(string inputString)
{
var invalids = "No invalid fields!";
if (string.IsNullOrEmpty(inputString))
{
return invalids;
}
foreach (Match match in Regex.Matches(inputString, "\\{(.*?)\\}"))
{
string key = match.Groups[1].Value;
var field = typeof(TextFormating).GetField(key);
if (field == null)
{
return "{" + key + "} is not a valid formating input!";
}
}
return invalids;
}
}Editor is loading...
Leave a Comment