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

1.运行环境

1.1编辑器版本

使用Unity2017.3.1f

1.2 dll

2.目的

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

3.流程

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

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

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

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

4.代码实现

4.1核心代码

public class ABUtils
{

    /// 
    /// 资源放置的文件夹路径
    /// 
    private static List resDircectoryPaths = new List(){ "Assets/Resources" };

    private static List needDependenyPaths = new List() { "Assets/Resources/Materials" };





    /// 
    /// 初始化打包环境,将所有的文件的ab属性都置空
    /// 
    static List InitBuildEnvironment()
    {
        List paths=new List();
        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 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);
    }

}

 

可运行工程可以看:https://blog.csdn.net/qq_28474981/article/details/82749021

 

 

你可能感兴趣的:(Unity3D资源管理)