any alternative for [Header] attribute on properties?

I have a bunch of properties (with getters and setters) that I want to group together in the inspector. (This is from Photon Fusion.)

    [Header("Networked Properties")]      // Drat; does not work with properties
    [Networked] public string displayName { get; set; }
    [Networked] public Vector3 headLocalPos { get; set; }
    [Networked] public Quaternion headLocalRot { get; set; }

But this doesn’t work; I get a “not valid on this declaration type” error. It appears that PropertyAttributes like this actually work only on fields, not properties.

I tried adding a hidden (by making it private or adding [HideInInspector]) field at the top of the block, but of course the Header attribute is ignored in that case.

Anybody know a clever trick, workaround, or alternative that would let me add a header in the inspector for my get/set properties?

Does [field: Header("Networked Properties")] work?

3 Likes

Yes! Thank you very much. But I’ve never seen this syntax before… Can I trouble you to explain it or point me to a reference?

It’s basically just a way to apply an attribute to the backing field of an auto-implemented property, instead of to the property itself.

Here’s the feature spec from C# 7.3:
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-7.3/auto-prop-field-attrs

1 Like