Managing asset dependencies

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.

Shaders dependencies

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 {
    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 canproduce conflicts due to colliding hashes when the amount ofobjects is too large. In this case the build will fail, and itwon’t be possible to update the shader bundle alone. In this caseyou will have to remove that option and rebuild all the assetbundles.

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可 6私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值