Unity-Technologies / guid-based-reference

A component for giving Game Objects a GUID and a class to create references to objects in any Scene by GUID
Other
625 stars 71 forks source link

Editor-only fields in GuidReference aren't allowed #2

Closed SilentSin closed 6 years ago

SilentSin commented 6 years ago

There's a problem in GuidReference.cs:

#if UNITY_EDITOR
// decorate with some extra info in Editor so we can inform a user of what that GUID means
[SerializeField]
private string cachedName;
[SerializeField]
private SceneAsset cachedScene;
#endif

This will still include serialized data for those fields in a build and will throw warnings in the log about the serialized data not matching the class.

Being able to make editor-only fields like that would be super useful but would require low level engine modifications. Even if we have to write [SerializeField(EditorOnly = true)] or something since reflection can't detect the #if on its own. Otherwise the build process would need to re-serialize everything based on the runtime class structure (possible, but would extend build times).

The only way to currently serialize editor-only data on a component is to have a whole separate component and set its hideFlags to DontSaveInBuild. Also note that this approach doesn't work if the component is saved in a prefab because the hideFlags get ignored.

william-w-armstrong commented 6 years ago

I do not see anything in the log in a development build in Standalone in 2017.4 This should, at the very least, remove the memory cost at runtime, unless I am very much mistaken.

SilentSin commented 6 years ago

No, you're right, it seems they did implement re-serialization based on the runtime class structure since I last checked (which was like, back in Unity 4 or something) because I'm not getting the same old error and I can't detect any difference in build size or runtime resource load time when creating a float[1024 * 1024] in a #if UNITY_EDITOR region (which does clearly affect the size of the prefab asset in the editor).

And perfect timing too. I was just about to implement a rather hacky workaround for serializing editor-only fields.

SugoiDev commented 6 years ago

This is great news! I also had my fair share of workarounds for that issue.

I remember only ever actually seeing the warnings on WebGL builds. It would spam the console hard when the serialized layout didn't match between the editor and a build.