I think we’re almost there!
Yes, when you do File > Save JSON Scene...
this process will create an AssetPack and save it to an .asset
file next to the scene with the same name as the scene. This is an asset representation of the AssetPack ScriptableObject which was used to collect asset references when serializing the scene. If you select this asset, you can use the Inspector to see what asset references were collected when serializing the scene. When you build an AssetBundle, these dependencies get included automatically. You are right that this is what you should have selected when building AssetBundles.
The directory structure created when building a single AssetBundle can be a little confusing, but the file you want from what you pasted is Bundles/assets/scenename.asset
. That is the particular bundle that corresponds to the AssetPack you had selected. The path to each AssetBundle within the Bundles
folder (or whatever you decide to call it) will match its path within the Assets folder. You can ignore the other files that are generated. If you don’t put the output folder in the Assets folder, you also won’t see the .meta
files. Note that you can also make multiple selections and build them all at once.
Unfortunately, the ScriptableObject
for the asset in the Assets folder and the AssetBundle
you build have the same extensinon: .asset
. But they are distinct representations of the AssetPack, and both are needed. There won’t be any files called .assetbundle
or .assetpack
in the process, just .asset. The original
scenename.asset` file, which lives in the Assets folder is simply a list of asset references, with their guids and fileIDs stored as strings. If you use text-based asset serialization, you can easily read its contents in a text editor. The AssetBundle representation is a binary file which includes the AssetPack with those strings and asset references, but also the assets themselves transformed into whatever platform-specific representation is needed in order to be loaded in the Player. The same stuff that happens to your models and textures when you click “Build” for your app is happening when you build AssetBundles.
It is important for the ScriptableObject .asset
to remain in your project after the scene is saved. First of all, you wouldn’t want to build AssetBundles every time you save the JSON scene because, as you have seen, it takes a lot longer to build the AssetBundle than it does to save the scene. Leaving the asset in your project after the scene is saved allows you to save a number of scenes, then select their AssetPack assets and build bundles.
Furthermore, you need to build a different AssetBundle for each build target and save/load them in separate paths, etc. In other words, if you want to build your app for iOS and Android, you need two AssetBundles for each AssetPack: one built for iOS and one for Android. Leaving the ScriptableObject asset in the projects allows you to switch build targets and do your build as many times as you like without having to open the scenes and serialize them again. This is one of the main reasons why our simplistic example here is only going to get you so far. You would need to use a separate path for the iOS vs. the Android bundle and choose which one to use based in the current platform. And, of course, just working with the filesystem on mobile devices can be tricky–just trying to get the AssetBundles onto the local filesystem where the app can access the can be a challenge.
You probably don’t want to put your AssetBundle output directory in the Assets folder. With the code I shared, it should default to the project folder, not Assets folder. That’s what the new DirectoryInfo(Application.dataPath).Parent.FullName
was for. The Editor doesn’t need to “know” about the built AssetBundles; they are only used by the Player. You can avoid getting those extra .meta
files generated, and Unity will spend some time importing them having them in Assets will bog down your project.
You are also right to think that you’ll need to re-build the AssetBundle every time the scene/AssetPack changes. If a new asset reference is added, you will need to rebuild the AssetBundle (remember, one per-platform) in order to reference the asset. If a reference is removed, you probably want to rebuild to make the bundle a little smaller. This is why, in the case of the Companion App workflow, the Resource Manager helps track what scenes have been updated, and we use a cloud service to transfer files around. Doing it all “by hand” isn’t really sustainable, so you will want to take the building blocks you see here and integrate them with some sort of content delivery system. You may also want to consider using Addressable Assets to provide a consistent reference to the AssetPacks in your project. Or you may find that it is more helpful to create custom scripting to combine all the asset references you need into one giant AssetPack. It depends on what you are trying to create.
At this point it may have become clear why we consider this package experimental. As I explained above, the primary purpose of this package is to support the AR Companion App workflow. This allows us to focus on building out a workflow that serves AR developers while incubating this general-purpose scene serialization functionality. It goes beyond the scope of the AR Companion project to support users with their own serialization workflows, but It’s great to hear that some are trying.