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>

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值