BuildAssetBundles文件打包:
public class BuildAssetBundlesTest : Editor
{
//定义打包文件路径
public static string sourpath = Application.dataPath + "/Res";
//窗口打包按键制作
[MenuItem("XL/AssetBundle/BundleAssetsss")]
//BuildAssetBundle方法(打包主方法)
static void BuildAssetBundle()
{
//调用方法清理原有打包文件名
clearName();
//打包实现 方法
pack(sourpath);
string path = Application.streamingAssetsPath+"/haha"; //打包后文件路径
//判断是否存在,有则进行删除
if (Directory.Exists(path) == false)
{
Directory.CreateDirectory(path);
}
//固定格式
BuildPipeline.BuildAssetBundles(path,BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
}
//清理旧的打包文件
static void clearName()
{
string[] oldName = AssetDatabase.GetAllAssetBundleNames();
for (int i = 0; i < oldName.Length; i++)
{
//删除文件名
AssetDatabase.RemoveAssetBundleName(oldName[i], true);
}
}
//根据路径进行打包
static void pack(string source)
{
//字典存放数据
DirectoryInfo folder = new DirectoryInfo(source);、
//得到数据流
FileSystemInfo[] files = folder.GetFileSystemInfos();
int length = files.Length;
//分文件夹分文件逐步打包
for (int i = 0; i < length; i++)
{
if (files[i] is DirectoryInfo)
{
pack(files[i].FullName);
}
else
{
if (!files[i].Name.EndsWith(".meta"))
{
substring(files[i].FullName);
}
}
}
}
//得到想要的文件名格式
static void substring(string sources)
{
string str01 = sources.Replace("\", "/");
string str02 = str01.Substring(Application.dataPath.Length - 6);
string str03 = str01.Substring(Application.dataPath.Length + 1);
AssetImporter assetImporter = AssetImporter.GetAtPath(str02);
string str04 = str03.Substring(str03.IndexOf("/") + 1, str03.LastIndexOf("/") - str03.IndexOf("/")-2);
assetImporter.assetBundleName = str04;
}
}
注:文件打包需要根据设置的路径等进行设配修改文件打包的文件名