AssetBundleBuild之打资源包

通过遍历子目录的所有内容打包里面的资源

   void StartBuild()
        {

            ABs = new List<AssetBundleBuild>();
            string TargePath="xxxxx/xxx/xxx/x";
            Directory.CreateDirectory(TargePath);//创建打包存放的位置
            string FileRoot="xxxxx/xxx/xxx/x";  
            List<string> Files = GetChilds(FileRoot);//获取要打包文件的根目录
            foreach (var file in Files)
            { 
                //打包的内容
                BuikdResource(file); 
            } 
            BuildPipeline.BuildAssetBundles(TargePath, ABs.ToArray(), BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
            AssetDatabase.Refresh();
        }


获取某个文件节点下的所有子目录
List<string> GetChilds(string path)
        {
            DirectoryInfo dirInfo = new DirectoryInfo(path);
            FileSystemInfo[] files = dirInfo.GetFileSystemInfos();
            List<string> list = new List<string>();
            foreach (FileSystemInfo file in files)
            {  
                if (Directory.Exists(file.FullName))
                {
                    list.Add(file.FullName);
                }
            }
            return list;
        }


  void BuikdResource(string Root)
        {
       
            DirectoryInfo RootInfo = new DirectoryInfo(Root); //获取当前目录节点的具体信息 
            string[] RootFile= Directory.GetFiles(Root);//获取当前节点内部的子目录
            for (int i = 0; i < RootFile.Length; i++)
            {
                FileInfo fileInfo = new FileInfo(RootFile[i]); //获取每一个文件
                if (fileInfo.Extension==".meta")
                {
                    continue;
                }
                string curAbagPath = RootFile[i].Replace("\\", "/").Substring(RootFile[i].IndexOf("Assets/"));//定义AB包的相对路径
                string curName= fileInfo.Name.Replace(fileInfo.Extension, "");//定义AB包的名字

                string curFileFullPath= fileInfo.Directory.FullName.Replace("\\","/");
                string curRootProPath = RootInfo.Parent.FullName.Replace("\\","/");
                AssetBundleBuild curABag = new AssetBundleBuild
                {
                    assetBundleName = curFileFullPath.Replace(curRootProPath + "/", "") + "/" + curName, //只保留根节点到本文件之间的地址
                    assetNames=new string[] { curAbagPath }, 
                };
                Debug.Log("abName:" + curABag.assetBundleName + "  path:" + curAbagPath);
                ABs.Add(curABag);            
            } 
        }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值