resharper-unity icon indicating copy to clipboard operation
resharper-unity copied to clipboard

Warn if a serialized field is being excluded from build with #if UNITY_EDITOR

Open SugoiDev opened this issue 7 years ago • 9 comments

Unity doesn't like when the serialized data layout changes between the editor and builds. For example, having something like this in a behavior SomeBehaviour

#if UNITY_EDITOR
[SerializeField]
private int _someField;
#endif

or this

#if UNITY_EDITOR
public int SomeField;
#endif

will cause problems in builds (A script behavior (probably SomeBehavior) has a different serialization layout when loading. (Read X bytes but expected Y bytes))

Having a warning (or error) for those situations would be pretty nice.

SugoiDev avatar Mar 09 '18 18:03 SugoiDev

Do you have any links to more details about this? I'd like to be able to have a "Why is Rider suggesting this?" explanation page for this one.

Do I understand it correctly that this is because the builds include the data entered in the editor and serialised by objects running in the editor, and will deserialise that data at run time?

In that case, do builds also include data from editor components?

citizenmatt avatar Jun 07 '18 15:06 citizenmatt

I'm not sure if there's any official docs on this, just user reports.

The actual warning message is like this:

"A script behaviour has a different serialization layout when loading. (Read n bytes but expected m bytes)
Did you #ifdef UNITY_EDITOR a section of your serialized properties in any of your scripts?"

The warnings can be seem on a webGL build, with the built-in console, or by connecting to a running player using the normal Unity console in the editor.

The serialization docs mention that the layout of the serialized object needs to be known ahead of time, and must remain the same.

one of the core foundations of the serialization system is that the layout of the datastream for an object is known ahead of time

The layout of a serialized structure always needs to be the same; independent of the data and only dependent on what is exposed in the script.

SugoiDev avatar Jun 07 '18 23:06 SugoiDev

Any updates on this? Using Rider 2021.1.3 and still no warning about this.

Additional info, sometimes it spams Unity console with a series of warnings like this as well TLS Allocator ALLOC_TEMP_THREAD, underlying allocator ALLOC_TEMP_THREAD.

guneyozsan avatar Jul 02 '21 10:07 guneyozsan

No updates, sorry. Could you provide some more details about the allocator messages? How are those messages related to serialised fields?

citizenmatt avatar Jul 02 '21 11:07 citizenmatt

Sorry @citizenmatt , they randomly appeared and disappeared after I changed the code to include and exclude serialized fields with #if. Maybe they are something else Unity related.

guneyozsan avatar Jul 15 '21 22:07 guneyozsan

@citizenmatt However, I can say that the message noted by @SugoiDev is actually an "error", not a warning. I am attaching a screenshot of how it appears in Unity Cloud Diagnostics logs.

The error is logged only by built apps, but not in Unity editor. So it could be pretty hard to notice when you exclude a serialized field. This is where a warning in Rider could be very handy!

image

guneyozsan avatar Jul 15 '21 22:07 guneyozsan

Unity officially supports editor-only serialized field since 2021.2 : https://docs.unity3d.com/2021.2/Documentation/Manual/script-Serialization.html

fdegryse avatar Apr 21 '22 16:04 fdegryse

Oh that's interesting, thanks @fdegryse. I'm not quite clear what happens in this case, though. Does it just silently ignore runtime data that has extra editor data in there?

citizenmatt avatar Apr 21 '22 19:04 citizenmatt

My guess is that data of serialized fields that do not exist in the compiled code are stripped during the build process. There are a few lines in the 2021.2 changelog that seem to be related to that but I could be completely wrong.

fdegryse avatar Apr 27 '22 22:04 fdegryse