Hello.
I’m a bit stuck over here. Tried to search my way to an answer for hours, now I’m about to lose my mind!
We are making a small program for a client, showing a few houses. The scenes containing each house is rather large, so they wanted it to be downloaded from cloud when loading a spesific house.
To solve this, we made one project where we made multiple scenes, with every scene containing one house. Then we made every scene into an asset bundle. In the “main project” we download the asset bundle of the spesific house they want to open, and load the scene. Since asset bundles cant hold scripts, i figured that i could attach the scripts needed runtime, behind the loading screen.
We load the scenes async, with one other scene active, called “GameMaster”. The GameMaster scene is the first scene that loads, handling some stuff in the background. This scene also contains a ScriptManager GO, with one script for every asset bundle. The plan was to use use these scripts to add the scripts needed in that spesific scene.
The problem is that i cant find the GameObjects im looking for, with tags or names in the new scene that is async loaded from Assetbundles.
To test, i tried with just two local scenes open in the Hierarchy, trying to put a cube in Scene A to an variable in Scene B. I somehow cant get that to work either. Any help/tips would be appreciated.
So, first I want to correct something. Assetbundles can’t contain new scripts, however, they can have scripts that exist in the project attached to them.
For example, if you create a script named HouseProperties and you attach that to a house model and then setup the properties and then create an assetbundle out of that House as long as that HouseProperties script exist in your project, it is able to use that script with your settings and be fine.
What you can’t do is the same situation above but instead, delete the HouseProperties script from the project and then expect the bundle to work. This also applies to adding a new script to the assetbundle from another project.
Hopefully that helps somewhat.
I haven’t done assetbundle scene loading myself yet, I’ve simply loaded bundles of assets.
Thanks for your answer. Got some more questions now
The asset bundle scenes are created in another project, containing only the houses, no scripts in the project. Then downloaded and cached runtime in the main project where we would like to have all the functions. So I’m guessing even though the script is attached to the bundle when it was created, I would need to reapply it in the other project after downloading? Or would it work if the script exists in both projects?
And how would one go about and do this for etc iOS? There must be a way to add scripts runtime scene A to Scene B? Or would the solution be to use the same project, and not two like we do now? Just not include the scenes in the build, but load them from asset bundle?
The reason why we use the whole scene as asset bundle, is because we want to load everything, like light maps and so on, and just reload that scene. Would be a lot of lines to load one object at the time. I figured I could after the scene was loaded, go from menu scene, behind loading screen and just do something like : GameObject SceneScriptManager = GameObject.FindGameObjectWithTag(“SSM”);
SceneScriptManager.AddComponent();
then access the roof script, in my UI script. To hide/show roof. That was the plan at least But since I apparently can’t start the adding of script in menu to the loaded asset scene I got to figure out a new way to do this. Kind of under pressure here, not thinking straight. New meeting with the client on Thursday.
I tried loading the asset bundle scene from the inside the same project where it was created. This worked out nicely, like you said. I still would like to create the bundle without any scripts, then download and open them in a separate project, and add scripts run time. Meaning i would have to locate files in Scene B from a script in Scene A. Then AddComponent to the GO`s located in in that script. Would iOS support asset bundles containing scripts to be loaded runtime?
Another solution might be not to load it as a complete scene, but download and instantiate each object into an existing scene with a SceneScriptMaster, then use AddComponent when everything is done. The problem here, is that i would need to create one script to load every house, to get all components into the scene instead of the scene load soultion where i simply pass URL into a variable.
Oh, and should i swap to addressables in the future instead of Asset bundles?
Addressables are a better system. We’re using it ourselves in several projects. However, they do have a different learning curve.
Every platform, from what we can tell, has support for the scripts being “included” in an assetbundle if it is included in the build also. We’re using that setup on an older project on both Google and iOS stores.
The only reason I might see to use AddComponent is if you plan to add new scripts to the build in the future that you want to add to assets you’re downloading. There is nothing wrong with this, but you’ll still need a way to get references to the assets in the scene.
I’ve done additive loading of scenes before, but usually those scenes have scripts in them to control what is going on. My guess is once you extract the scene from an assetbundle and load it, you’ll need some way to find the stuff in the scene. Are you doing additive loading? If so, you’ll need to make sure the scene is loaded before trying to access stuff in it.
To note, the way we create our assetbundles is within the same project. We just maintain our repo and even if the build itself is being worked on, the assetbundles can be created. Unless your use case has a reason not to, I don’t see a reason not to go this route.
The thing to be cautious of when using two projects and why you might run into issues is if an assetbundle has scripts in it, while the script doesn’t go with a bundle, my guess is some sort of ID does along with data needed to populate the script inspector values I assume.
So what I mean by this is if you have two projects both with a House.cs script for example, create an assetbundle in one project may not be able to reference the House.cs script in another project properly. Thus why you might be trying to use AddComponent while letting the scene layout be done in another project.
Thank you very much for taking the time to elaborate! I really appreciate it.
I started coding with C# back in september, so still much til learn. Coming from a 3D rendering background, and we would like to expand on what we can offer clients, with realtime rendering. My only prior coding experience is from PHP, CSS and HTML.
Tbh, the only reason we wanted to do two projects, its because it felt “cleaner”. All models and so on in one project, while using the other to project to hold/create/attach scripts and display the scenes for our client. I do use additive loading, and like you said, i would need something to find “stuff” in the scene after it was loaded, and then attach scripts to it. My problem was finding that stuff, from the menu scene.
Since your earlier suggestion worked out nicely, we will use this method now. Especially since it also works for iOS.
In the future when not on a deadline to show off a demo, i will try to learn some more about addressables, and when i got that down, i will switch from Asset Bundles.