Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Jun 20, 2021 at 11:18 PM by maxgiao for the following reason:

other

avatar image
0
Question by maxgiao · Jun 20, 2021 at 01:02 AM · textinspectorstringnamevarable

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
1
Best Answer

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
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

173 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Variables In Editor Inspector Strings 1 Answer

How to create new line in string from inspector? 1 Answer

Is it possible to create multiple Gameobject Texts in script and simply click and drag the texts in the Hierarchy into the Inspector? 0 Answers

Stopwatch will not show time. 1 Answer

Why is my code not working? [please help] 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges