Killing naked strings? What the heck does that even mean? If you have ever worked on a large project with a medium-to-large sized team you will know exactly what I am talking about. How many times have you seen brittle code like this
GameObject.FindWithTag( "SpawnPoint" ) or this
someGameObject.tag == "Enemy"? Those strings are naked and afraid and they will come back to haunt you later.
For the purposes of this discussion we will define a naked string as any string that is not declared as a constant. That means anytime you start typing a quote (“) in your code you are making a naked string and causing problems for future you or the rest of your team. Let’s say, for example, you are comparing GameObject.tag to naked strings as in the first paragraph. What happens if you want to change the tag from “Enemy” to “Tank”? You then have to search your project and hope that you find all the places where you used the “Enemy” string. The worst part is that bugs like this are very hard to track down.
There are various ways to solve this problem. The most common is constant string properties/fields in classes. As long as you are diligent and make sure that you always remember to update the strings when you change your tags, all will be well. We all know that isn’t happening though. Either you or somebody else will change something and forget to update the string. Good luck tracking that bug down.
Enter ConstantsGeneratorKit: the solution to naked strings. As an added benefit, it will also solve your naked layer ints, naked scene names and the worst offender of them all: naked resource paths. You can find ConstantsGeneratorKit on GitHub. It’s a single script. Stick it in a folder named Editor somewhere in your project. That’s it. ConstantsGeneratorKit will wait for changes in scenes, layers, tags and resources then generate classes with all the strings and ints setup as const. Yup. That means you get autocomplete for all of them. Just type
k. and bask in the glory (by default ConstantsGeneratorKit stick everything in the “k” namespace for easy access but you can configure that and other options right in the file.) Below are examples of the 4 classes that ConstantsGeneratorKit generates automatically.
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
1 2 3 4 5 6 7 8 9