Unity3D资源管理——Unity2017 Assetbundle依赖打包

Unity3D资源管理——Unity2017 Assetbundle依赖打包

http://gad.qq.com/article/detail/289272

一、运行环境

1.1编辑器版本

使用Unity2017.3.1f

1.2 dll

二、目的

预制体打包时会将一份资源进行多份打包,导致数据冗余,所以需要根据依赖,将被多处引用的资源独立达成一个包,建立依赖关心,优化冗余。

三、流程

1. 遍历所有资源文件,设置其包名为空

2. 遍历需要打包的文件夹下的所有文件,通过Unity资源接口获取所有依赖,统计对应的依赖数

3. 将在需要打包文件夹内的包名设置为文件名,将在需要打包文件夹外的依赖数大于2的文件设置包名为文件名

4. 调用Unity的打包接口,会将该工程下的所有被设置包名的资源进行AB打包。

四、代码实现

核心代码:

 
 
public class ABUtils
 
 
    /// <summary>
 
    /// 资源放置的文件夹路径
 
    /// </summary>
 
    private static List<string> resDircectoryPaths = new List<string>(){ "Assets/Resources" };
 
    private static List<string> needDependenyPaths = new List<string>() { "Assets/Resources/Materials" };
 
    /// <summary>
 
    /// 初始化打包环境,将所有的文件的ab属性都置空
 
    /// </summary>
 
    static List<string> InitBuildEnvironment()
 
 
        List<string> paths=new List<string>();
 
        string[] files = AssetDatabase.GetAllAssetPaths();
 
        for (int i = 0; i < files.Length; i++)
 
 
            for (int j = 0; j < resDircectoryPaths.Count; j++)
 
 
                if (files[i].Contains(resDircectoryPaths[j]))
 
 
                    AssetImporter importer = AssetImporter.GetAtPath(files[i]);
 
                    paths.Add(files[i]);
 
                    importer.assetBundleName = String.Empty;
 
 
 
 
        return paths;
 
 
    public static void BuildAllAbAboutDependency(string outPath)
 
 
        ResourceSystem.FileUtils.PrecessDirectoryExist(outPath);
 
        List<string> resPaths=InitBuildEnvironment();
 
        string fileName;
 
        string relationPath;
 
        ResourcesData data;
 
        bool isNeedDependcy;
 
        for (int i = 0; i < resPaths.Count; i++)
 
 
            isNeedDependcy = false;
 
            fileName = PathUtils.GetFileNameWithoutExtension(resPaths[i]);
 
            if (ResourceSystemFacade.Inst.ResDict.ContainsKey(fileName))
 
 
                data = ResourceSystemFacade.Inst.ResDict[fileName];
 
                relationPath = PathUtils.GetRelativePath("Assets", resPaths[i]);
 
                for (int j = 0; j < needDependenyPaths.Count; j++)
 
 
                    if (relationPath.Contains(needDependenyPaths[j]))
 
 
                        isNeedDependcy = true;
 
                        if (data.DependeciedNum > 1)
 
 
                            AssetImporter importer = AssetImporter.GetAtPath(relationPath);
 
                            importer.assetBundleName = data.Name;
 
                            importer.assetBundleVariant = ResourceSystemFacade.FileExtension;
 
 
 
 
                if (!isNeedDependcy)
 
 
                    AssetImporter importer = AssetImporter.GetAtPath(relationPath);
 
                    importer.assetBundleName = data.Name;
 
                    importer.assetBundleVariant = ResourceSystemFacade.FileExtension;
 
 
 
            else
 
 
                DebugUtils.DebugError("未包含文件" + fileName);
 
 
 
        DebugUtils.DebugRedInfo(outPath);
 
        BuildPipeline.BuildAssetBundles(outPath, BuildAssetBundleOptions.UncompressedAssetBundle, BuildTarget.StandaloneWindows64);
 
 
}<span style="white-space: normal;"> </span>
发布了64 篇原创文章 · 获赞 36 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/kuangben2000/article/details/104018201