Blog

Tags & Layers Constants Generator in Unity

August 3, 2012 by Devin Reimer

I hate using manually typed strings for comparing, it’s error prone, lacks auto-complete and is sometimes difficult to change.

This is why, in all my Unity projects I have two classes, Tags.cs and Layers.cs. These contain constants for each tag and layer entry. This way I get auto-complete, it’s not possible to make a typo and if I need to make change, it’s all in one place.

The downside is that every time I make a change to Tags or Layers, I need to also update the corresponding class. So I finally wrote an editor script that does this for you.

After you make a change to tags or layers, simply go ‘Edit->Rebuild Tags and Layers Classes’. It then auto generates updated versions of the classes and putting them in the Scripts/AutoGenerated folder. To use, simply type the constants, ex: Tags.RESPAWN or Layers.IGNORE_RAYCAST.

Download TagsLayersBuilder.cs (right-click save as).

Remember to put it in a folder called ‘Editor’ in your project.

No Responses to "Tags & Layers Constants Generator in Unity"

Leave a response