Imry
Imry

Reputation: 35

How can I use string interpolation in the inspector?

I learned that using $"" and {} allows me to handle strings more freely. And I wanted to use them in the inspector but if I type $"Hi" in the inspector it just recognizes the $ and " as normal characters.

Upvotes: 0

Views: 1350

Answers (2)

Jay Jack
Jay Jack

Reputation: 36

The work around that I came up with is to:

  1. Serialize a text area that you want to type in;
  2. Write a quick method that parses that string by using myString.Replace("{myNumber}", $"{myNumber}");
  3. Make sure the method that you write has a parameter for the number that you want to pass into it, i.e. private string Parse(string myString, int myNumber) OR if you want to make a reusable method you can add parameters with a default argument e.g. private string Parse(string myString, int myNumber = 0, string name = null);
  4. Now store the parsed string from the method in a variable, and feed that variable into your HUD instead.

Upvotes: 2

Daveloper
Daveloper

Reputation: 758

of course it does. $"" and {} you are talking about are c# code syntax. The inspector is a GUI, it doesn't consider your input as such, it doesn't interpret it, it considers it to be plain text.

So the short answer to your actual question "how can i?" is "you can not"... it's not meant for that.

Why don't you elaborate what you're trying to achive as suggested by CodingYoshi, so that maybe we can help you achive your end-goal without using string interpolation in the inspector

Upvotes: 3

Related Questions