写在前面:毕业很多年了,工作也很多年了,但还是第一次写博客,希望现在开始记录下自己的点点滴滴吧
Unity 5.X以后的 AssetBundle,极大的优化了打热更新包的策略,更简单也更直接了。但单纯的打包还会有一些问题,Unity3d的共享资源文件如果不打包的话,会在每个ab包中包含资源,就会增大包的大小。那怎么办呢,是单独指定某些共享包吗?答案是否定的,这样很消耗时间。Unity有一个类AssetImporter,我们就可以用起来,话不多说,贴上代码:
public class AssetBundleTool
{
[MenuItem("Tool/Build AssetBundles")]
public static void BuildAllAssetBundles()
{
//string assetBundleDirectory = "Assets/AssetBundles";
string assetBundleDirectory = AssetBundleConfig.AssetBundle_PATH;
if(!Directory.Exists(assetBundleDirectory))
{
Directory.CreateDirectory(assetBundleDirectory);
}
BuildPipeline.BuildAssetBundles(assetBundleDirectory, BuildAssetBundleOptions.ChunkBasedCompression|BuildAssetBundleOptions.DeterministicAssetBundle,BuildTarget.Android);
}//*/
[MenuItem("Tool/SetNoResourcesAssetBundleName")]
public static void SetAssetBundleName()
{
Object[] selectAssets = Selection.GetFiltered (typeof(Object), SelectionMode.Assets | SelectionMode.ExcludePrefab);
string[] Filtersuffix = new string[]{".mat", ".dds",".fbx" };
if (selectAssets.Length != 1)
return;
string fullPath = AssetBundleConfig.Project_Path + AssetDatabase.GetAssetPath (selectAssets [0]);
if (Directory.Exists (fullPath)) {
DirectoryInfo dir = new DirectoryInfo (fullPath);
FileInfo[] files = dir.GetFiles ("*", SearchOption.AllDirectories);
for (int i = 0; i < files.Length; ++i) {
FileInfo fileinfo = files [i];
EditorUtility.DisplayProgressBar ("设置BundleName", "正在设置Bundlename......" + i + "/" + files.Length, (float)i / (float)files.Length);
foreach (string subfix in Filtersuffix) {
if (fileinfo.Name.EndsWith (subfix,System.StringComparison.OrdinalIgnoreCase)) {
//string path = fileinfo.Directory.FullName.Replace ("\\", "/").Substring (AssetBundleConfig.Application_Path.Length);
string path = fileinfo.FullName.Replace ("\\", "/").Substring (AssetBundleConfig.Project_Path.Length);
AssetImporter importer = AssetImporter.GetAtPath (path);
if (importer) {
//Debug.Log("path="+ path+"directory="+fileinfo.Directory.FullName);
//string dicpath = fileinfo.Directory.FullName.Replace ("\\", "/").Substring (AssetBundleConfig.Application_Path.Length);
string name = fileinfo.FullName.Substring (AssetBundleConfig.Application_Path.Length);
importer.assetBundleName = name.Substring(0,name.LastIndexOf(".")) + AssetBundleConfig.SUFFIX;
}
break;
}
}
}
EditorUtility.ClearProgressBar ();
AssetDatabase.RemoveUnusedAssetBundleNames ();
}
}
[MenuItem("Tool/SetResouceAssetBundleName")]
public static void SetResouceAssetBundleName()
{
Object[] selectAssets = Selection.GetFiltered (typeof(Object), SelectionMode.Assets | SelectionMode.ExcludePrefab);
string[] Filtersuffix = new string[]{ ".prefab",".controller",".anim"};
if (selectAssets.Length != 1)
return;
string fullPath = AssetBundleConfig.Project_Path + AssetDatabase.GetAssetPath (selectAssets [0]);
if (Directory.Exists (fullPath)) {
DirectoryInfo dir = new DirectoryInfo (fullPath);
FileInfo[] files = dir.GetFiles ("*", SearchOption.AllDirectories);
for (int i = 0; i < files.Length; ++i) {
FileInfo fileinfo = files [i];
EditorUtility.DisplayProgressBar ("设置BundleName", "正在设置Bundlename......" + i + "/" + files.Length, (float)i / (float)files.Length);
foreach (string subfix in Filtersuffix) {
if (fileinfo.Name.EndsWith (subfix,System.StringComparison.OrdinalIgnoreCase)) {
//string path = fileinfo.Directory.FullName.Replace ("\\", "/").Substring (AssetBundleConfig.Application_Path.Length);
string path = fileinfo.FullName.Replace ("\\", "/").Substring (AssetBundleConfig.Project_Path.Length);
AssetImporter importer = AssetImporter.GetAtPath (path);
if (importer) {
//Debug.Log("path="+ path+"directory="+fileinfo.Directory.FullName);
//string dicpath = fileinfo.Directory.FullName.Replace ("\\", "/").Substring (AssetBundleConfig.Application_Path.Length);
string name = fileinfo.FullName.Substring (AssetBundleConfig.RESOUCES_PATH.Length);
importer.assetBundleName = name.Substring(0,name.LastIndexOf(".")) + AssetBundleConfig.SUFFIX;
}
break;
}
}
}
EditorUtility.ClearProgressBar ();
AssetDatabase.RemoveUnusedAssetBundleNames ();
}
}
[MenuItem("Tool/SetShaderAssetBundleName")]
public static void SetShaderAssetBundleName()
{
Object[] selectAssets = Selection.GetFiltered (typeof(Object), SelectionMode.Assets | SelectionMode.ExcludePrefab);
string[] Filtersuffix = new string[]{ ".shader"};
if (selectAssets.Length != 1)
return;
string fullPath = AssetBundleConfig.Project_Path + AssetDatabase.GetAssetPath (selectAssets [0]);
if (Directory.Exists (fullPath)) {
DirectoryInfo dir = new DirectoryInfo (fullPath);
FileInfo[] files = dir.GetFiles ("*", SearchOption.AllDirectories);
for (int i = 0; i < files.Length; ++i) {
FileInfo fileinfo = files [i];
EditorUtility.DisplayProgressBar ("设置BundleName", "正在设置Bundlename......" + i + "/" + files.Length, (float)i / (float)files.Length);
foreach (string subfix in Filtersuffix) {
if (fileinfo.Name.EndsWith (subfix)) {
string path = fileinfo.Directory.FullName.Replace ("\\", "/").Substring (AssetBundleConfig.Application_Path.Length);
string pathName = fileinfo.FullName.Replace ("\\", "/").Substring (AssetBundleConfig.Project_Path.Length);
AssetImporter importer = AssetImporter.GetAtPath (pathName);
if (importer) {
//Debug.Log("path="+ path+"directory="+fileinfo.Directory.FullName);
//string name = path.Substring (fullPath.Substring (AssetBundleConfig.Project_Path.Length).Length + 1);
importer.assetBundleName = path + AssetBundleConfig.SUFFIX;
}
break;
}
}
}
EditorUtility.ClearProgressBar ();
AssetDatabase.RemoveUnusedAssetBundleNames ();
}
}
//public static void
[MenuItem("Tool/SetTextureAssetBundleName")]
public static void SetTexutureResouceAssetBundleName()
{
Object[] selectAssets = Selection.GetFiltered (typeof(Object), SelectionMode.Assets | SelectionMode.ExcludePrefab);
string[] Filtersuffix = new string[]{".png",".jpg",".tga" };
if (selectAssets.Length != 1)
return;
Debug.Log("ProjectPath="+ AssetBundleConfig.Project_Path);
string fullPath = AssetBundleConfig.Project_Path + AssetDatabase.GetAssetPath (selectAssets [0]);
if (Directory.Exists (fullPath)) {
DirectoryInfo dir = new DirectoryInfo (fullPath);
FileInfo[] files = dir.GetFiles ("*", SearchOption.AllDirectories);
for (int i = 0; i < files.Length; ++i) {
FileInfo fileinfo = files [i];
EditorUtility.DisplayProgressBar ("设置BundleName", "正在设置Bundlename......" + i + "/" + files.Length, (float)i / (float)files.Length);
foreach (string subfix in Filtersuffix) {
if (fileinfo.Name.EndsWith (subfix)) {
string path = fileinfo.Directory.FullName.Replace ("\\", "/").Substring (AssetBundleConfig.Application_Path.Length);
string pathName = fileinfo.FullName.Replace ("\\", "/").Substring (AssetBundleConfig.Project_Path.Length);
AssetImporter importer = AssetImporter.GetAtPath (pathName);
if (importer) {
//Debug.Log("path="+ path+"directory="+fileinfo.Directory.FullName);
//string name = path.Substring (fullPath.Substring (AssetBundleConfig.Project_Path.Length).Length + 1);
importer.assetBundleName = path + AssetBundleConfig.SUFFIX;
}
break;
}
}
}
EditorUtility.ClearProgressBar ();
AssetDatabase.RemoveUnusedAssetBundleNames ();
}
}
[MenuItem("Tool/GetAllAssetBundleName")]
public static void GetAllAssetBundleName()
{
string[] names = AssetDatabase.GetAllAssetBundleNames ();
foreach (var name in names) {
Debug.Log (name);
}
}
[MenuItem("Tool/ClearAllAssetBundleName")]
public static void ClearResoucesAssetBundleName()
{
Object[] selectAssets = Selection.GetFiltered (typeof(Object), SelectionMode.Assets | SelectionMode.ExcludePrefab);
string[] Filtersuffix = new string[]{ ".prefab", ".mat", ".dds",".fbx" ,".png",".jpg",".tga",".shader"};
if (selectAssets.Length != 1)
return;
string fullPath = AssetBundleConfig.Project_Path + AssetDatabase.GetAssetPath (selectAssets [0]);
if (Directory.Exists (fullPath)) {
DirectoryInfo dir = new DirectoryInfo (fullPath);
FileInfo[] files = dir.GetFiles ("*", SearchOption.AllDirectories);
for (int i = 0; i < files.Length; ++i) {
FileInfo fileinfo = files [i];
EditorUtility.DisplayProgressBar ("清除BundleName", "正在清除Bundlename......"+ i + "/" + files.Length, (float)i / (float)files.Length);
foreach (string subfix in Filtersuffix) {
if (fileinfo.Name.EndsWith (subfix)) {
string path = fileinfo.FullName.Replace ("\\", "/").Substring (AssetBundleConfig.Project_Path.Length);
AssetImporter importer = AssetImporter.GetAtPath (path);
if (importer) {
importer.assetBundleName = null;
}
break;
}
}
}
}
EditorUtility.ClearProgressBar ();
AssetDatabase.RemoveUnusedAssetBundleNames ();
}
}
这段代码作用主要就是:1.可以对指定目录的图片打包,并且根据文件夹名字组成一个包,通过AssetImporter加入到AssetBundle的管理器里面去,这需要我们在资源管理的时候主意不要把不同的图集放在一个文件夹里面了。
2.将文件目录下的预制件单独打成一个包,热更新的时候包体更小。
3.根据资源对应的文件夹将包都打到相应文件夹下面去,更利于查找,也利于原先使用Resource加载资源的同学更能转过来
各个文件夹下面都是对应的包