Game Code Architecture

Hello,

I heard that for WebGL there cant be any code contained in a scene asset that is downloaded from the server.

So let say i wrote code that opens a door when the player is near, and that code is attached to a game object that is in the scene asset that is downloaded from the server. It seems everything is downloaded except the code, so now the door wont open when the player is near to it. What is the proper way to organize the code so this will still work?

Another scenario is what if i have many scenes and each scene may have a different game. What if i have a world space object that when touched by the player activates a mini game in the scene asset downloaded from the server. How can this work if i cant have code on the gameObject that the player touches that is supposed to activate the game when the player triggers it?

lets say also that in one scene i have lots of different kinds of power ups all over the place, where power ups have some code on it to do things. How will this work if i cant have code on the power ups because its contained in a scene asset download for the server?

Lets say i have a scene asset for a treasure hunt game, where the scene and all the things to be searched for is inside the scene asset downloaded from a server. The things to be hunted also has code attached that does things when found by the player.But all of this is inside a scene asset downloaded and run by the root game. But now cant have code in there, so how do i detect/trigger on the objects for the treasure hunt etc.

There are lots more scenarios, but im sure there must be a standard way to do all this.

Does the root game have to go around attaching code to all the correct gameObjects after the scene asset is downloaded?

Anybody know what is the proper way to do all these things without having any code in the downloaded scene asset?

Thanks

I believe that the base built webgl player contains all user and engine code so you don’t have to worry about code not being there. Any code is not stored in asset bundles(well, not easily anyways).
Your scene will course have references to scripts, but there’s nothing you have to do manually. It “just works”.

I think he might be confusing “new code/scripts”. I believe on non-il2cpp platforms (desktop) you can have code in asset bundles that wasn’t included/compiled in the original binary. That is obviously not possible on webgl as it’s a il2cpp platform. But yeah we load asset bundles with custom scripts no problem on webgl, just not new scripts!

Thanks for the comments, but the issue is that each scene asset bundle on the server was created by different Unity project that has its own scripts.

I have one main scene/Unity Project that loads all the other scene bundles from the server when needed, and that main scene has a main camera, and some other stuff which stays no matter which scene bundle is loaded.

But the problem i was having was that some scenes needed to modify the main camera changing the focal angle and some other stuff, so i added a script to the scene asset bundle which after it was loaded would locate the main camera and make the modifications, but when the scene bundle was loaded the script to change the main camera parameters wasnt loaded and it showed error message saying script was missing.

This is why i thought the scripts from scene bundle assets are not loaded.

So do you know how i can fix my issue? To keep the scene bundle size low, i had a different unity project for each scene bundle that only contains the minimum stuff needed for that scene, so that i dont have a massive main project with almost everything in there which would take long to download.

Thanks

You can whitelist all the stuff you need for your scenes in the bundles in the link.xml file so it won’t get stripped.
See Unity - Manual: Build your WebGL application

I don’t think the problem is stripping (although he may run into that problem later!); I think the problem is this:

I interpret that to mean Unity project A (the project used to build the binary) does not have all the scripts from Unity project B (the project used to build asset bundles). That won’t work with webgl or any il2cpp platform. Project A must have the scripts (with matching meta files) from project B when building. Otherwise those scripts will get unlinked and will appear missing.