Using ScriptableObject with Asset Bundles

I have a ScriptableObject that needs to be referenced by several objects that just happen to be stored in different asset bundles. Currently the ScriptableObject is not explicitly bundled itself, although the objects that need it have a public reference to it. Unfortunately, this seems to result in copies of the ScriptableObject being added to each bundle, and then when the game runs, the objects referencing it have different copies of the data, which somewhat defeats the purpose of the ScriptableObject, and in my game actually results in some bugs :confused:

I could put the ScriptableObject in another bundle, and reference it from there, but then everything that uses it has to know its bundle name and asset name, which is rather clumsy.

Does anyone know of a way around this bundling issue? Is there a correct and incorrect way to use ScriptableObject with asset bundles?

The official docs explain the cause and solution to my problem here: https://docs.unity3d.com/Manual/AssetBundles-Dependencies.html


Basically, if an asset that is NOT contained in an asset bundle is referenced by an object that IS contained in an asset bundle, the referenced asset will be copied to the bundle of each asset that references it. This means that if you have several assets in different bundles referencing the same asset, they will each have their own copy of the asset built into their respective bundle and not be referencing the same data.

To get around this problem, you have to add shared assets to a separate asset bundle so that the asset will be referenced instead of copied. The shared bundle must also be loaded on startup (or at least before the referencing assets are loaded). In my case, I was using ScriptableObject assets for data, so I created a bundle named “data” and loaded that at startup. Now everything seems to be working correctly.