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
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
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.
Shaders dependencies
Whenever shaders are directly referenced as parametersin
C
using UnityEngine;
public class ShadersList : MonoBehaviour {
public Shader[] list;
}
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 {
[MenuItem("Assets/Export all asset bundles")]
static void Export() {
BuildAssetBundleOptions options =
BuildAssetBundleOptions.CollectDependencies |
BuildAssetBundleOptions.CompleteAssets |
BuildAssetBundleOptions.DeterministicAssetBundle ;
BuildPipeline.PushAssetDependencies();
BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath("Assets/ShadersList.prefab"), null, "WebPlayer/ShadersList.unity3d", options);
BuildPipeline.PushAssetDependencies();
BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath("Assets/Scene1.prefab"), null, "WebPlayer/Scene1.unity3d", options);
BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath("Assets/Scene2.prefab"), null, "WebPlayer/Scene2.unity3d", options);
BuildPipeline.PopAssetDependencies();
BuildPipeline.PopAssetDependencies();
}
[MenuItem("Assets/Update shader bundle")]
static void ExportShaders() {
BuildAssetBundleOptions options =
BuildAssetBundleOptions.CollectDependencies |
BuildAssetBundleOptions.CompleteAssets |
BuildAssetBundleOptions.DeterministicAssetBundle ;
BuildPipeline.PushAssetDependencies();
BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath("Assets/ShadersList.prefab"), null, "WebPlayer/ShadersList.unity3d", options);
BuildPipeline.PopAssetDependencies();
}
}
Bear in mind that you must load the shader bundle first. Onedrawback of this method is that the optionBuildAssetBundleOptions.DeterministicAssetBundle