Resharper-unity: Unity serialized field naming rule makes it impossible to vary by visibility.

Created on 23 Oct 2018  Â·  12Comments  Â·  Source: JetBrains/resharper-unity

As noted by englerj, this change has created a ton of incorrect warnings in my project that can't be turned off without turning off all inconsistent naming warnings.

A fix for this would either be to an option that allowed no special naming and to fall back to normal rules, or different naming schemes for public and private unity serialized fields.

Most helpful comment

Thanks for the explanation. Interesting that it's a split on [Serializable]/MonoBehaviour.

Adding those two lines should work. I've just tested it and it seems to work ok. I've made some tweaks, so try adding this to your .sln.DotSettings file. You should be able to edit the file, save it and go back to Rider. Rider will read the file, update settings and should show it immediately - this is what I'm seeing when testing with Rider 2019.2.2.

The file format is horrible, in the way that only a computer can love, but it needs to be valid XML, so make sure these lines go before the closing </wpf:ResourceDictionary> (this is quite often added to the end of a line, rather than having its own line, so it's easy to miss. And the parser is silently forgiving):

<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=e66e0435_002Dde09_002D4fdf_002Da4ba_002D16a598a0c336/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Instance" AccessRightKinds="Public" Description="Unity serialized field (Public)"&gt;&lt;ElementKinds&gt;&lt;Kind Name="UNITY_SERIALISED_FIELD" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /&gt;&lt;/Policy&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=110218a1_002Dc4d4_002D4137_002Db798_002D981bc635cddc/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Instance" AccessRightKinds="Private, Protected, ProtectedInternal" Description="Unity serialized field (Non-public)"&gt;&lt;ElementKinds&gt;&lt;Kind Name="UNITY_SERIALISED_FIELD" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /&gt;&lt;/Policy&gt;</s:String>

This adds two new custom rules - "Unity serialised field (Public)" and "Unity serialised field (Non-public)", set up to be AaBb and _aaBb respectively. You won't be able to edit either of these rules (that's what RIDER-8339 is all about), not even in the Unity settings page - that rule has a different GUID.

All 12 comments

This really requires RIDER-8339 to be implemented

Sorry if this is the wrong place to put this. Is there no way to make public/private serialized fields use different naming conventions now? If I disable the unity serialized field naming, it just ignores all public/private fields naming, instead of falling back to normal naming conventions.

There is an example in this comment that provides a manual override to allow different naming conventions for public/private. The linked RIDER-8339 issue is the correct fix for this to be part of the UI.

Out of interest, would you mind explaining your use case, please? Why do you want to have a mix of public/private serialised fields?

I would only really use a public field for simple data container types. It’s more wanting to differentiate between private and public regardless of being a serialized field. It might just be me misunderstanding or using bad coding practices though.

Thanks for your help!

IMO, using the same convention for both private AND public fields - IS a bad practice, and Rider decided to encourage this for some weird reason. I never saw any Unity code that would use some kind of special style for serialized fields, while having no difference between public & private - this just doesn't make sense.

This still hasn't been fixed? I'm on the latest Rider and it's still doing this and there's no option to disable.

Unfortunately, this isn't fixed. It requires RIDER-8339 to be implemented, as a core part of Rider, rather than something that can be fixed from the plugin.

Just to clarify, though, it's currently possible to set up different naming standards for:

  • Non-private fields (public, protected, etc.) - default is UpperCamelCase
  • Private instance fields - default is _lowerCamelCase
  • Private static fields - default is _lowerCamelCase
  • Non-private constant fields - default is UpperCamelCase
  • Private constant fields - default is UpperCamelCase
  • Non-private static readonly fields - default is UpperCamelCase
  • Private static readonly fields - default is UpperCamelCase

On top of that, there is a naming standard for "Unity serialised fields", with a default of lowerCamelCase. This will apply to public fields inside a Unity specific type, or to private fields marked with [SerializeField]. This is a more specific rule than "public" or "private" fields, and will take precedence.

Which means:

public class MyBehaviour : MonoBehaviour
{
  public string serialisedString1;   // Unity serialised field (default lowerCamelCase)
  [SerializedField] private string serialisedString2; // Unity serialised field (default lowerCamelCase)
  private string _nonSerialisedString;  // Private instance field (default _lowerCamelCase)
}

public class DataClass
{
  public string NonSerialisedString1;  // Non-private field (default UpperCamelCase)
  private string _nonSerialisedString2;  // Private instance field (default _lowerCamelCase)
}

So public and private non-serialised fields have different naming standards. There is another naming standard for serialised fields, but this only applies to types that are implicitly (public) or explicitly ([SerializeField] private …) serialised fields.

The inspection for "Unity serialised fields" can be disabled in _Preferences | Languages & Frameworks | Unity Engine_, but this prevents showing a warning, rather than treating these fields as non-serialised fields.

There is a manual workaround that adds a new rule for _private_ "Unity serialised fields" to the .sln.dotSettings file that is checked into source control. This is what RIDER-8339 will do - update the UI in _Preferences | Editor | Code Style | C#_ to allow building custom rules based on entity ("Unity serialised field") and visibility (public, private or any combination).

As an aside, I'm still unsure why you would have some serialised fields public and some private?

If a class is a small [Serializable] container class then I usually make its fields public (UpperCamelCase). I think that fits the intention of that sort of class better than hiding them behind properties. In my current project I have quite a few of those. Otherwise MonoBehaviours being more complex rarely have public fields and anything that needs to be adjusted in the inspector is a private field ( _lowerCamelCase) marked [SerializeField].

I tried adding those 2 lines from the thread to the .sln.DotSettings file in the root of my Unity project directory and it did not stop the naming suggestions on public fields in a serialized class. Is there something else I'm supposed to do? I just started using Rider so I'm not that familiar with it.

Thanks for the explanation. Interesting that it's a split on [Serializable]/MonoBehaviour.

Adding those two lines should work. I've just tested it and it seems to work ok. I've made some tweaks, so try adding this to your .sln.DotSettings file. You should be able to edit the file, save it and go back to Rider. Rider will read the file, update settings and should show it immediately - this is what I'm seeing when testing with Rider 2019.2.2.

The file format is horrible, in the way that only a computer can love, but it needs to be valid XML, so make sure these lines go before the closing </wpf:ResourceDictionary> (this is quite often added to the end of a line, rather than having its own line, so it's easy to miss. And the parser is silently forgiving):

<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=e66e0435_002Dde09_002D4fdf_002Da4ba_002D16a598a0c336/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Instance" AccessRightKinds="Public" Description="Unity serialized field (Public)"&gt;&lt;ElementKinds&gt;&lt;Kind Name="UNITY_SERIALISED_FIELD" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /&gt;&lt;/Policy&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=110218a1_002Dc4d4_002D4137_002Db798_002D981bc635cddc/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Instance" AccessRightKinds="Private, Protected, ProtectedInternal" Description="Unity serialized field (Non-public)"&gt;&lt;ElementKinds&gt;&lt;Kind Name="UNITY_SERIALISED_FIELD" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /&gt;&lt;/Policy&gt;</s:String>

This adds two new custom rules - "Unity serialised field (Public)" and "Unity serialised field (Non-public)", set up to be AaBb and _aaBb respectively. You won't be able to edit either of these rules (that's what RIDER-8339 is all about), not even in the Unity settings page - that rule has a different GUID.

I know this thread is quite old but I search a bit for this and I just saw that you can just configure this in the unity plugin configuration:
Languages & frameworks > Unity Engine > Serialized field naming rules >
Prefix = _
Suffix =
Style = lowerCamelCase

I know this thread is quite old but I search a bit for this and I just saw that you can just configure this in the unity plugin configuration:
Languages & frameworks > Unity Engine > Serialized field naming rules >
Prefix = _
Suffix =
Style = lowerCamelCase

This does not actually solves the problem, as it will try to force you to use the same naming convention for any serialized field (public or private with [SerializeField]). For now the only solution is to edit .sln.DotSettings like shown here

This does not actually solves the problem, as it will try to force you to use the same naming convention for any serialized field (public or private with [SerializeField]). For now the only solution is to edit .sln.DotSettings like shown here
You're absolutely right. I discovered this later but to be honest I forgot about this thread. Thanks for correcting.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

justinlueders picture justinlueders  Â·  3Comments

RealMSHB picture RealMSHB  Â·  3Comments

SugoiDev picture SugoiDev  Â·  6Comments

JashanChittesh picture JashanChittesh  Â·  3Comments

FWCorey picture FWCorey  Â·  4Comments