Unity AssetBundle打包
程序员文章站
2022-04-03 15:38:30
...
using UnityEngine;
using UnityEditor;
using System.IO;
/// <summary>
/// 把其他目录下的资源打包成.unity3d 到StreamingAssets目录下
/// </summary>
public class BuildAssetBundle : Editor
{
public static string m_SourcePath = Application.dataPath + "/BundleRes";
private static string m_AssetbundlesOutPutPath = Application.streamingAssetsPath;
[MenuItem("AssetBundle/BuildAssetBundle")]
public static void BuildAssetBundles()
{
ClearAssetbundlesName();
Pack(m_SourcePath);
string outputPath = Path.Combine(m_AssetbundlesOutPutPath,Platform.GetPlatformFolder(EditorUserBuildSettings.activeBuildTarget));
Debug.Log("outputPath: "+ outputPath);
if(!Directory.Exists(outputPath))
{
Directory.CreateDirectory(outputPath);
}
//根据BuildSetting里面所**的平台进行打包
//BuildAssetBundleOptions
//BuildAssetBundleOptions.None:使用LZMA算法压缩,压缩的包更小,但是加载时间更长。
//使用之前需要整体解压。一旦被解压,这个包会使用LZ4重新压缩。使用资源的时候不需要整体解压。
//在下载的时候可以使用LZMA算法,一旦它被下载了之后,它会使用LZ4算法保存到本地上。
//BuildAssetBundleOptions.UncompressedAssetBundle:不压缩,包大,加载快
//BuildAssetBundleOptions.ChunkBasedCompression:使用LZ4压缩,压缩率没有LZMA高,
//但是我们可以加载指定资源而不用解压全部。
//注意: 使用LZ4压缩,可以获得可以跟不压缩想媲美的加载速度,而且比不压缩文件要小。
BuildPipeline.BuildAssetBundles(outputPath,BuildAssetBundleOptions.None,EditorUserBuildSettings.activeBuildTarget);
AssetDatabase.Refresh();
Debug.Log("打包完成 !!!");
}
/// <summary>
/// 清除之前设置的AssetbundleName 避免不必要的资源也会进行打包
/// 只要设置了AssetbundleName的不论在什么目录都会被打包
/// </summary>
public static void ClearAssetbundlesName()
{
//string[] oldAssetbundleNames = AssetDatabase.GetAllAssetBundleNames();
//获取所有的AssetbunldeName总个数
int length = AssetDatabase.GetAllAssetBundleNames().Length;
Debug.Log("清除之前的BunldeNameLength: " + length);
string[] oldAssetbundleNames = new string[length];
for (int i = 0; i < length; i++)
{
//获取AssetbundleName
oldAssetbundleNames[i] = AssetDatabase.GetAllAssetBundleNames()[i];
}
for (int i = 0; i < oldAssetbundleNames.Length; i++)
{
//移除AssetBunldeName
AssetDatabase.RemoveAssetBundleName(oldAssetbundleNames[i],true);
}
length = AssetDatabase.GetAllAssetBundleNames().Length;
Debug.Log("清除之后的BunldeNameLength: "+length);
}
/// <summary>
/// 获取指定路径下所有文件
/// </summary>
/// <param name="source"></param>
public 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
{
//如果不是meta文件
if (!files[i].Name.EndsWith(".meta"))
{
file(files[i].FullName);
}
}
}
}
/// <summary>
/// 设置AssetbundleName
/// </summary>
/// <param name="source"></param>
public static void file(string source)
{
string _source = Replace(source);
string _assetPath1 = "Assets" + _source.Substring(Application.dataPath.Length);
string _assetPath2 = _source.Substring(Application.dataPath.Length + 1);
AssetImporter assetImporter = AssetImporter.GetAtPath(_assetPath1);
string assetName = _assetPath2.Substring(_assetPath2.IndexOf("/") + 1);
Debug.Log("source: " + source + " _assetPath1: " + _assetPath1 + " _assetPath2: " + _assetPath2+ " assetName: "+ assetName+ " Path.GetExtension(assetName): "+ Path.GetExtension(assetName));
//Path.GetExtension(assetName) 获取扩展名
assetName = assetName.Replace(Path.GetExtension(assetName),".unity3d");
assetImporter.assetBundleName = assetName;
}
public static string Replace(string str)
{
return str.Replace(@"\\","/");
}
}
public class Platform
{
public static string GetPlatformFolder(BuildTarget target)
{
switch (target)
{
case BuildTarget.Android:
return "Android";
case BuildTarget.iOS:
return "IOS";
//case BuildTarget.WebPlayer:
// return "WebPlayer";
case BuildTarget.StandaloneWindows:
case BuildTarget.StandaloneWindows64:
return "Windows";
case BuildTarget.StandaloneOSXIntel:
case BuildTarget.StandaloneOSXIntel64:
//case BuildTarget.StandaloneOSXUniversal:
return "OSX";
default:
return null;
}
}
}
上一篇: JavaScript:document对象的常用属性和方法
下一篇: 泛型单例
推荐阅读
-
Unity Shader 实现流动虚线效果
-
Unity iOS接SDK,定制UnityAppController
-
Unity3d获取系统时间
-
jQuery LigerUI 插件介绍及使用之ligerDrag和ligerResizable示例代码打包_jquery
-
vue-cli打包步骤详解
-
PHP扩展类ZipArchive实现压缩解压Zip文件和文件打包下载
-
使用PHP实现密保卡功能实现代码<打包下载直接运行>_PHP
-
在Linux下用C扩展PHP(打包成so)的方法
-
JavaScript语法着色引擎(demo及打包文件下载)_prototype
-
iOS中使用Fastlane实现自动化打包和发布