Good practice for storing layerInt values

Hi,
As layer numbers are global and can be used in a number of classes/components, how do you store them without having magic numbers scattered around?

It’s only a minor thing, but curious how others in the community go about it.

I’ll be locally declaring them for now, but that’ll still end up duplicated around. A static class would work, as it’s just config, but I’m sure there’s a more intuitive way.

Personally I believe it depends on how frequently each layer is being used. If I know only one type of object in my scene is to be drawn on a given layer, then I would handle the layer in that MonoBehaviour only.

public class MyInvisibleObject : MonoBehaviour
{
     private const int detectiveVisionLayer = 10;
}

However, if I have layers that are used by many objects, I would keep them in a static class somewhere:

public static class GraphicsConstants
{
    public const int fooLayer = 1;
    public const int barLayer = 2;
}

Of course, you could do this in a List or Dictionary instead.

Now, I avoid using strings as much as possible, but with layers I prefer to use strings because it’s easier to see where the result in the scene is coming from. As such, I tend to use this method a lot in my MonoBehaviour subclass:

// Convenience accessor for getting/setting layer by name
public string layerName
{
	get
	{
		return LayerMask.LayerToName(this.gameObject.layer);
	}
	set
	{
		this.gameObject.layer = LayerMask.NameToLayer(value);
	}
}