Using layers, tags and other Unity definitions from code

Unity allows manipulations with names of layers, tags, input axes and other things through visual editing in editor. What about using same things inside code in proper way?

Unity allows manipulations with names of layers, tags, input axes and other things through visual editing in editor. What about using same things inside code in proper way?

Unfortunately, there are no any access to this data as to typed data - we should use strings with constant values. Using string constants directly in code - bad practice, all of them should be extracted to field constants or even to separate class. It will allow us to use named values in code with intellisense with easy refactoring it later.

Alright, constants as fields in classes. But manual creation, checking for obsoleted / invalid values each time - too boring as for me. I wrote special generator (editor script) that saves me a lot of time and published it as part of my library. This tool is accessible through main menu of Unity editor:

After run you will see window with editable settings which can be changed for each project. You can specify target file for constants from “Assets” folder, namespace for target class and declare folders which should be ignored from scanning (Folder paths should be splitted with “;”).

When all settings are valid and you pressed “Generate”, actual settings will be saved automatically as project asset - very useful feature for next re-generation with same settings. In fact, for regeneration, you will need to press only one button.

Settings “Options” declares what you want to get in code:

  • Layers - Camera / Physics layers.
  • Tags - Tags.
  • Scenes - Scenes. Only scenes from BuildSettings will be included to scan.
  • Animators - Animator-graph parameters.
  • Axes - Input axes.
  • Shaders - Shader parameters. Parameters from shaders with names started from “Hidden” will be ignored. Parameters with attribute HideFromInspector will be ignored too.
  • SortingLayers - 2d-mode sorting layers.

As result of scan we will get new file with new class inside (contents of this file will be changed on each re-generation with new data):

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Auto generated code, dont change it manually!

using UnityEngine;

namespace Client.Common {
public static partial class UnityIdents {
public const int SortingLayerDefault = 0;

public const string AxisCancel = "Cancel";

public const string AxisFire1 = "Fire1";

public const string AxisFire2 = "Fire2";

public const string AxisFire3 = "Fire3";

public const string AxisHorizontal = "Horizontal";

public const string AxisJump = "Jump";

public const string AxisMouseScrollWheel = "Mouse ScrollWheel";

public const string AxisMouseX = "Mouse X";

public const string AxisMouseY = "Mouse Y";

public const string AxisSubmit = "Submit";

public const string AxisVertical = "Vertical";

public const string TagEditorOnly = "EditorOnly";

public const string TagFinish = "Finish";

public const string TagGameController = "GameController";

public const string TagMainCamera = "MainCamera";

public const string TagPlayer = "Player";

public const string TagRespawn = "Respawn";

public const string TagUntagged = "Untagged";

public static readonly int LayerDefault = LayerMask.NameToLayer ("Default");

public static readonly int LayerIgnoreRaycast = LayerMask.NameToLayer ("Ignore Raycast");

public static readonly int LayerTransparentFX = LayerMask.NameToLayer ("TransparentFX");

public static readonly int LayerUI = LayerMask.NameToLayer ("UI");

public static readonly int LayerWater = LayerMask.NameToLayer ("Water");

public static readonly int MaskLayerDefault = 1 << LayerDefault;

public static readonly int MaskLayerIgnoreRaycast = 1 << LayerIgnoreRaycast;

public static readonly int MaskLayerTransparentFX = 1 << LayerTransparentFX;

public static readonly int MaskLayerUI = 1 << LayerUI;

public static readonly int MaskLayerWater = 1 << LayerWater;
}
}

Now we can use layers, tags, input axes with intellisense and be sure that we wrote them correctly without mistypes.