# Managing asset dependencies

Any given asset in a bundle may depend on other assets. Forexample, a model may incorporate materials which in turn make useof textures and shaders. It is possible to include all an asset’sdependencies along with it in its bundle. However, several assetsfrom different bundles may all depend on a common set of otherassets (eg, several different models of buildings may use the samebrick texture). If a separate copy of a shared dependency isincluded in each bundle that has objects using it, then redundantinstances of the assets will be created when the bundles areloaded. This will result in wasted memory.

To avoid such wastage, it is possible to separate shareddependencies out into a separate bundle and simply reference themfrom any bundles with assets that need them. First, the referencingfeature needs to be enabled with a callto BuildPipeline.PushAssetDependencies.Then, the bundle containing the referenced dependencies needs to bebuilt. Next, another call to PushAssetDependencies should be madebefore building the bundles that reference the assets from thefirst bundle. Additional levels of dependency can be introducedusing further calls to PushAssetDependencies. The levels ofreference are stored on a stack, so it is possible to go back alevel using the correspondingBuildPipeline.PopAssetDependencies function.The push and pop calls need to be balanced including the initialpush that happens before building.

At runtime, you need to load a bundle containing dependenciesbefore any other bundle that references them. For example, youwould need to load a bundle of shared textures before loading aseparate bundle of materials that reference those textures.

## Asset IDs

If you anticipate needing to rebuild asset bundles that are part ofa dependency chain then you should build them with theBuildAssetBundleOptions.DeterministicAssetBundle optionenabled. This guarantees that the internal ID values used toidentify assets will be the same each time the bundle isrebuilt.

When building the asset bundle with this method, the objects in itare assigned a 32 bit hash code that is calculated using the nameof the asset bundle file, the GUID of the asset and the local id ofthe object in the asset. For that reason make sure to use the samefile name when rebuilding. Also note that having a lot of objectsmight cause hash collisions preventing Unity from building theasset bundle.

Whenever shaders are directly referenced as parametersin BuildPipeline.BuildAssetBundle,or indirectly with the optionBuildAssetBundleOptions.CollectDependencies theshader’s code is included with the asset bundle. This could cause aproblem if you use BuildAssetBundle alone to create several assetbundles, since referenced shaders will be included in everygenerated bundle. There could be conflicts, i.e. when you mixdifferent versions of a shader, so you will have to rebuild allyour bundles after modifying the shaders. The shader’s code willalso increase the size of bundles. To avoid these problems you canuse BuildPipeline.PushAssetDependencies toseparate shaders in a single bundle, and that will allow you toupdate the shader bundle only. As an example of how to achieve thisworkflow, you can create a prefab that includes references to therequired shaders:

### C

using UnityEngine;

public class ShadersList : MonoBehaviour {
}



Create an empty object, assign the script, add the shaders to thelist and create the prefab, i.e. “ShadersList”. Then you can createan exporter that generates all the bundles and updates the bundleof shaders:

### C

using UnityEngine;
using UnityEditor;

public class Exporter : MonoBehaviour {

static void Export() {
BuildAssetBundleOptions options =
BuildAssetBundleOptions.CollectDependencies |
BuildAssetBundleOptions.CompleteAssets |
BuildAssetBundleOptions.DeterministicAssetBundle;

BuildPipeline.PushAssetDependencies();

BuildPipeline.PushAssetDependencies();

BuildPipeline.PopAssetDependencies();
BuildPipeline.PopAssetDependencies();
}

BuildAssetBundleOptions options =
BuildAssetBundleOptions.CollectDependencies |
BuildAssetBundleOptions.CompleteAssets |
BuildAssetBundleOptions.DeterministicAssetBundle;

BuildPipeline.PushAssetDependencies();
}