通过遍历子目录的所有内容打包里面的资源
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);
}
}
AssetBundleBuild之打资源包
于 2024-01-23 10:29:00 首次发布