other
Using variable names as seen in the inspector
A variable by the name of fireResistance becomes Fire Resistance in the inspector.
Is there any way to display the name like that (Fire Resistance) in a text? (without converting it manually)
I could name it Fire_Resistance and then just replace "_" with " ", but I wan't to know if there is a more direct way first.
Answer by Hellium · Jun 20, 2021 at 09:36 AM
If you are creating an Editor script, then you can use ObjectNames.NicifyVariableName
defined in the UnityEditor namespace (hence, you can't use it in a build)
https://docs.unity3d.com/ScriptReference/ObjectNames.NicifyVariableName.html
Otherwise, implementing such function is not complicated
public static string NicifyVariable(string input)
{
System.Text.StringBuilder output = new System.Text.StringBuilder();
char[] inputArray = input.ToCharArray();
int startIndex = 0;
if(inputArray.Length > 1 && inputArray[0] == 'm' && input[1] == '_')
startIndex += 2;
if(inputArray.Length > 1 && inputArray[0] == 'k' && inputArray[1] >= 'A' && inputArray[1] <= 'Z')
startIndex += 1;
if(inputArray.Length > 0 && inputArray[0] >= 'a' && inputArray[0] <= 'z')
inputArray[0] -= (char)('a' - 'A');
for(int i = startIndex ; i < inputArray.Length ; ++i)
{
if(inputArray[i] == '_')
{
output.Append(' ');
continue;
}
if(inputArray[i] >= 'A' && inputArray[i] <= 'Z')
{
output.Append(' ');
}
output.Append(inputArray[i]);
}
return output.ToString().TrimStart(' ');
}
Debug.Log(NicifyVariable(nameof(MyVariable))); // My Variable
Debug.Log(NicifyVariable(nameof(m_TheOtherVariable))); // The Other Variable
Debug.Log(NicifyVariable(nameof(__AnotherVariable))); // Another Variable
Debug.Log(NicifyVariable(nameof(kSomeConstant))); // Some Constant
Debug.Log(NicifyVariable(nameof(someVariable))); // Some Variable
Follow this Question
Related Questions
Variables In Editor Inspector Strings 1 Answer
How to create new line in string from inspector? 1 Answer
Stopwatch will not show time. 1 Answer