Search code examples
javascriptunity-game-engineassetbundle

Add script to assetbundle in HTML


I'm working on a project that uses Unity WebGL to display different machines/parts/.. in 3D, these parts or machines are selected by the user and then loaded into a scene (for now there is just a single scene, but we might want to load scenes dynamically also).

Because of the large amount of choices we want to create different asset bundles, containing 1 or more parts each, so we can download these on-demand.

I've done this successfully by passing the URL to LoadFromCacheOrDownload and extracting the gameObject from the www object.

Now we would also like to include scripts with the assets to create animations and user interaction. I followed the explanation given here: docs.unity3d link, and this works perfectly in the WebPlayer. However the end requirement is WebGL, and the same code built for WebGL gives the following error:

NotSupportedException: /Users/builduser/buildslave/unity/build/Tools/il2cpp/il2cpp/libil2cpp/icalls/mscorlib/System/AppDomain.cpp(184) : Unsupported internal call for IL2CPP:AppDomain::LoadAssemblyRaw - "This icall is not supported by il2cpp." System.AppDomain.LoadAssemblyRaw (System.Byte[] rawAssembly, System.Byte[] rawSymbolStore, System.Security.Policy.Evidence securityEvidence, Boolean refonly) System.AppDomain.Load (System.Byte[] rawAssembly, System.Byte[] rawSymbolStore, System.Security.Policy.Evidence securityEvidence, Boolean refonly) System.AppDomain.Load (System.Byte[] rawAssembly, System.Byte[] rawSymbolStore, System.Security.Policy.Evidence securityEvidence) System.AppDomain.Load (System.Byte[] rawAssembly) System.Reflection.Assembly.Load (System.Byte[] rawAssembly) API+c__Iterator0.MoveNext ()

It seems to stem from the call System.Reflection.Assembly.Load(txt.bytes); (which I got from the example), so I suppose the Reflection class is not (yet?) fully supported for WebGL. I can't seem to find any documentation on this.

Is there a way around using Reflection for this? At best I'm hoping for some different code that can fix this, at worst that we will have to create the scripts for WebGL in Javascript and add them as such instead of as a binary? I'm a bit lost here so any leads are appreciated.

(Cross-posted from answers.unity3d)


Solution

  • No, there is no way around this restriction with reflection.

    The key difference between the web player and WebGL build targets in Unity in this case is that WebGL uses AOT (ahead-of-time) compilation, whereas the web player uses JIT (just-in-time) compilation. With AOT compilation, it is not possible to load an assembly at run-time that was not present at compile time.

    Of course, it is possible to load JavaScript code at runtime, so as you suggest, you'll probably need to go this route.