Custom naming settings for private serialized field

 Working in Unity, I like to use private fields with [SerializeField] attribute.

But I want to distinguish them from standard private fields. So I'm using a _UpperCamelCase with the underscore in front, because in some way they are accessible from the editor as they were public.

Now I have to add a resharper comment around these fields in all my classes. Would be nice to have an additional option in the Editor > Code Style > C# > Naming tab for serialized field.

Comment actions Permalink

I think this is a bad naming practice.

In addition, Unity has other ways to serialize an object, and it's not clear how the Rider should handle them. For example, you can use Odin serialization, or your type may not have the attribute [Serializable], but be a serializable exception (Vector2, Vector3, Matrix4x4, Quaternion, etc. have this principle). Your field can be serialized and remain private (without using [SerializeField]), for example, if you are working inside EditorWindow. Perhaps any class inside EditorWindow will internally follow the same logic. And it is not obvious now whether the private field is serializable, since the context of using the type is not always obvious.

The worst thing is that Rider is bound to standard serialization in this case.

Comment actions Permalink

This is actually a very common naming practice in unity, and I found this thread while looking for the same thing. I think this is a totally valid case to want to cover:

public int someValue {
    get => _someValue;
    set {
        if (_someValue == value) {
        _someValue = value;
        // do some logic when the value changes
private int _someValue;

These things are also totally subjective too, which is why Rider as a tool allows you to configure them and add multiple cases in the first place. Unfortunately the "Unity Serialized Field" configuration only allows you to add one convention for all serialized fields, public or private


Please sign in to leave a comment.