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.

3
4 comments

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.

0

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) {
            return;
        }
        _someValue = value;
        // do some logic when the value changes
    }
}
[SerializeField]
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

0

I believe this is a valid request, alternatively, it would be great if the unity validation for SerializedField could be disabled and instead follow the default c# coding style. Right now, if you disable the validation for SerializedField it just completely stops any validation for naming whatsoever. 

0

Hello everyone,

Unfortunately the "Unity Serialized Field" configuration only allows you to add one convention for all serialized fields, public or private

There is a way to set naming separately for private, separately for public fields, but there is no UI in Rider to do this (there is a task about this: Add-UI-for-custom-naming-rules). There is a manual workaround in this GitHub comment.

alternatively, it would be great if the unity validation for SerializedField could be disabled and instead follow the default c# coding style. Right now, if you disable the validation for SerializedField it just completely stops any validation for naming whatsoever. 

By default, when "File | Settings | Languages & Frameworks | Unity Engine | Serialized Field Naming Rules | Enable inspection" option is disabled, validation shouldn't stop. If it stops for you, then let me know for what code this happens (for Unity serialized fields only?). Could you please provide your Rider settings: File | Settings | "Manage Layers" button at the bottom => "Export to File" in the context menu for each layer? Then please upload settings files to JB server via https://uploads.jetbrains.com/ form. Thank you!

0

Please sign in to leave a comment.