mob-sakai / ParticleEffectForUGUI

Render particle effect in UnityUI(uGUI). Maskable, sortable, and no extra Camera/RenderTexture/Canvas.
MIT License
4.38k stars 621 forks source link

IL2CPP Code Stripping Bug - "A scripted object (probably Coffee.UIExtensions.UIParticle?) has a different serialization layout when loading." #269

Closed mattluard closed 11 months ago

mattluard commented 1 year ago

Hi, writing this here in case it helps someone else, in IL2CPP builds I got this error at runtime:

A scripted object (probably Coffee.UIExtensions.UIParticle?) has a different serialization layout when loading. (Read 32 bytes but expected 140 bytes)
Did you #ifdef UNITY_EDITOR a section of your serialized properties in any of your scripts?

(The note about #ifdef UNITY_EDITOR does not apply here, firstly because this works fine in Mono builds and not in IL2CPP builds, but secondly because I don't think there's any #ifdef's in the repo that match this description.)

This problem, I believe, is actually due to Unity's code-stripping, if you have ParticleEffectForUGUI as a package installed from the Package Manager, Unity's going to mess up during the build and strip too much from the package. It doesn't matter if you move the Package from /Library/PackageCache/ to /Packages/, it still gets stripped.

From the Unity docs on code stripping:

Minimal - Unity searches only the UnityEngine and the .NET class libraries for unused code.
...
This is the default setting if you use the IL2CPP scripting backend.

The solution, such as it is right now, is to not use ParticleEffectForUGUI as a package at all, but rather copy it right into your /Assets/ folder.

Nothing for @mob-sakai to do here, this is Unity's code stripping problem, except to perhaps add a note to the Installation section of the readme that says something like: "Due to Unity's code-stripping going too far, games targeting IL2CPP should not use the package installation but rather copy the repo into /Assets/, or else there will be serialization errors."

mob-sakai commented 1 year ago

Thank you for your reporting! I would like to know more about this issue.

Could you please attach a minimal project (included Assets, Packages and ProjectSettings directories) that reproduces the issue?

repos

mob-sakai commented 1 year ago

@mattluard

The issue is not reproduced on my environment...
Do you think adding the following code to UIParticle.cs would solve the issue?

[assembly:UnityEngine.Scripting.Preserve]
ZhihaoJun commented 1 year ago

Same problem here. Built standalone on windows platform with il2cpp backend. I got the error log when running: image

My best guess is this macro that causes the bug since this is the only macro I found wrap a SerializeField of UIParticle.cs: image

UIParticle version: 4.1.7 Unity version: 2021.3.28f1

mob-sakai commented 1 year ago

@ZhihaoJun umm... It is not reproduced on my environment.

Could you please attach a minimal project (included Assets, Packages and ProjectSettings directories) that reproduces the issue?

github-actions[bot] commented 1 year ago

:tada: This issue has been resolved in version 4.2.2 :tada:

The release is available on GitHub release

Your semantic-release bot :package::rocket:

ZhihaoJun commented 1 year ago

Sorry for the late reply, but I'm glad the bug has been fixed.

ZhihaoJun commented 1 year ago

Unfortunately, I encountered this issue again using the 4.4.0 version. This time I was building Android version of my game.

However, I managed to solve it by putting entire UIParticle project in the Assets folder rather than in the Packages like mentioned above.

I think this issue is related to AssetBundle building process because my UI prefab is loading from an AssetBundle. Everything is fine in the test project that is not using AssetBundle.

UIParticle version: 4.4.0 Unity version: 2021.3.28f1 IL2CPP backend

Optimization settings as below image

ryushinnn commented 12 months ago

Hi sir @mob-sakai , looks like this issue hasn't been fixed yet. UIParticle ver: 4.2.2 Unity ver: 2022.3.11f1 Please check. Thanks! image

mob-sakai commented 12 months ago

@ZhihaoJun Thank you for your reporting!

@tienassassin As a workaround, try the way that @ZhihaoJun said.

mob-sakai commented 11 months ago

Could you please attach a minimal project (included Assets, Packages and ProjectSettings directories) that reproduces the issue?

repos

ryushinnn commented 11 months ago

Hmm if I create an empty scene (same project as the one having the problem), then use UI Particle here, everything works fine... 😟

mob-sakai commented 11 months ago

I think that the root cause is the different serialization layout, meaning that the data structure differs between build time and load time.

ryushinnn commented 11 months ago

Yeah, I upgraded Unity editor from 2022.3.7f1 to 2022.3.11f1 I just tried removing the UIParticle component, deleting the [generated] object then adding the UIParticle component again, everything works perfectly, it's a miracle Thanks for your dedicated help, sir @mob-sakai

mob-sakai commented 11 months ago

If the same issue arises, please re-open this issue.