Unity5.x AssetBundle打包详解

Unity5.x AssetBundle打包详解

在网上查看了很多资料,想详细搞清楚AssetBundle的原理。以实现符合项目需求的打包工具和加载逻辑

1. AssetBundle是什么?

AssetBundle是Unity用于动更的一种资源打包格式,如果某个资源需要动更的话,它必须被打包成AssetBundle

2. AssetBundle打包常见要面临的问题?

  • 如何组织打包
  • 如何避免资源重复打包
  • 打包的资源如何加载

3. 我的打包方案

Unity5.x已经大幅度简化了AssetBundl的打包过程,理论上只需要调用一个API即可以完成打包

BuildPipeline.BuildAssetBundles(RES_OUTPUT_PATH, BuildAssetBundleOptions.DeterministicAssetBundle, BuildTarget.StandaloneOSXIntel); 

但资源之间的依赖关系,避免资源重复打包的问题还是要自己解决。Unity5.x提供了一个很好的东西,就是可以给资源设置AssetBundleName


Unity5.x AssetBundle打包详解_第1张图片

Unity5.x中会将设置AssetBundleName相同的资源打包到一起,所以我们的打包过程其实就是把需要打包到一起的资源设置成相同的AssetBundleName,为每个资源设置AssetBundleName。最后调用打包API即可完成打包

我目前使用的打包规则是:

  • prefab、scene等文件单独打包
  • 图片资源按文件夹打包

关键代码如下:

    /// 
    /// 设置AssetBundleName
    /// 
    /// Fullpath.
    public static void setAssetBundleName(string fullPath) 
    {
        string[] files = System.IO.Directory.GetFiles (fullPath);
        if (files == null || files.Length == 0) {
            return;
        }
        Debug.Log ("Set AssetBundleName Start......");
        string dirBundleName = fullPath.Substring (RES_SRC_PATH.Length);
        dirBundleName = dirBundleName.Replace ("/", "@") + ASSET_BUNDLE_SUFFIX;
        foreach (string file in files) {
            if (file.EndsWith (".meta")) {
                continue;
            }
            AssetImporter importer = AssetImporter.GetAtPath (file);
            if (importer != null) {
                string ext = System.IO.Path.GetExtension (file);
                string bundleName = dirBundleName;
                if (null != ext && (ext.Equals (".prefab")||ext.Equals(".unity"))) {
                    // prefab单个文件打包
                    bundleName = file.Substring (RES_SRC_PATH.Length);
                    bundleName = bundleName.Replace ("/", "@");
                    if (null != ext) {
                        bundleName = bundleName.Replace (ext, ASSET_BUNDLE_SUFFIX);
                    } else {
                        bundleName += ASSET_BUNDLE_SUFFIX;
                    }
                }
                bundleName = bundleName.ToLower ();
                Debug.LogFormat ("Set AssetName Succ, File:{0}, AssetName:{1}", file, bundleName);
                importer.assetBundleName = bundleName;
                EditorUtility.UnloadUnusedAssetsImmediate();
                // 存储bundleInfo
                AssetBuildBundleInfo info = new AssetBuildBundleInfo();
                info.assetName = file;
                info.fileName = file;
                info.bundleName = bundleName;
                if (null != ext) {
                    info.fileName = file.Substring (0, file.IndexOf (ext));
                }
                fileMap.Add (file, info);
                List infoList = null;
                bundleMap.TryGetValue(info.bundleName, out infoList);
                if (null == infoList) {
                    infoList = new List ();
                    bundleMap.Add (info.bundleName, infoList);
                }
                infoList.Add (info);
            } else {
                Debug.LogFormat ("Set AssetName Fail, File:{0}, Msg:Importer is null", file);
            }
        }
        Debug.Log ("Set AssetBundleName End......");
    }

打包好之后生成依赖关系的配置文件,配置文件格式如下:


    
        [email protected]
        Assets/Resources/Prefabs/Login
        Assets/Resources/Prefabs/Login.prefab
        
            [email protected]
            [email protected]
        
    
    
        [email protected]
        Assets/Resources/Scenes/Main
        Assets/Resources/Scenes/Main.unity
    
    
        [email protected]
        Assets/Resources/Textures/Common/btn_blue
        Assets/Resources/Textures/Common/btn_blue.png
    
    
        [email protected]
        Assets/Resources/Textures/Common/btn_red
        Assets/Resources/Textures/Common/btn_red.png
    
    
        [email protected]
        Assets/Resources/Textures/Login/bg
        Assets/Resources/Textures/Login/bg.png
    
    
        [email protected]
        Assets/Resources/Textures/Login/text_input
        Assets/Resources/Textures/Login/text_input.png
    

  • bundleName 打包的文件名
  • fileName 包里包含的文件名
  • assetName 包里的AssetName
  • deps 依赖的其他bundleName

上述过程就完成了打包,具体可以参考的github: https://github.com/aodota/TestUnity

你可能感兴趣的:(Unity5.x AssetBundle打包详解)