打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Unity5.x版本AssetBundle打包研究 | 是幻觉

Unity5的AssetBundle打包机制和以前版本不太一样。简单的说就是,只要给你要打包的资源设置一个AssetBundleName ,Unity自身会对这些设置了名字的资源进行打包,如果一个资源依赖了另一个资源。Unity自己会处理依赖关系,AssetBundleManifest文件就保存着这些资源的依赖关系。
比如一个UI面板.Prefab,依赖了一个图集Atlas,一个字体文件
做个测试:
只给UI面板3.prefab设置AssetBundleName。


打出包来看,别看只有371KB,那是因为我拿得面板不是很复杂,依赖的图集,字体,本身就不是很大。
要是项目中的话,你不处理依赖打包的话,几M都是有的。

要是有其它的UI面板,设置AssetBundleName,打出包,都是这么大的

依赖文件显示资源没依赖,这是因为每一个面板里面都单独打包了一份图集资源,字体资源。显然这是不可取的。
对于同类型的UI面板来说,这些图集和字体文件,大家用的都是同一份,只要打包出一份,大家共享就好了。
接下给图集资源,字体文件都设置AssetBundleName,再进行打包,可以看到变小了。

在看.manifest文件,有了依赖关系。

项目中,资源辣么多,总不能在编辑器里一个一个给资源进行设置AssetBundleName吧,那会蛋疼死的,是吧。
上代码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
using UnityEngine;using System.Collections;using UnityEditor;using System.IO; /// <summary>/// 把Resource下的资源打包成.unity3d 到StreamingAssets目录下/// </summary>public class Builder : Editor{	public static string sourcePath = Application.dataPath + "/Resources";	const string AssetBundlesOutputPath = "Assets/StreamingAssets"; 	[MenuItem("Tools/AssetBundle/Build")]	public static void BuildAssetBundle()	{		ClearAssetBundlesName (); 		Pack (sourcePath); 		string outputPath = Path.Combine (AssetBundlesOutputPath,Platform.GetPlatformFolder(EditorUserBuildSettings.activeBuildTarget));		if (!Directory.Exists (outputPath))		{			Directory.CreateDirectory(outputPath);		} 		//根据BuildSetting里面所激活的平台进行打包		BuildPipeline.BuildAssetBundles (outputPath,0,EditorUserBuildSettings.activeBuildTarget); 		AssetDatabase.Refresh (); 		Debug.Log ("打包完成"); 	} 	/// <summary>	/// 清除之前设置过的AssetBundleName,避免产生不必要的资源也打包	/// 之前说过,只要设置了AssetBundleName的,都会进行打包,不论在什么目录下	/// </summary>	static void ClearAssetBundlesName()	{		int length = AssetDatabase.GetAllAssetBundleNames ().Length;		Debug.Log (length);		string[] oldAssetBundleNames = new string[length];		for (int i = 0; i < length; i++) 		{			oldAssetBundleNames[i] = AssetDatabase.GetAllAssetBundleNames()[i];		} 		for (int j = 0; j < oldAssetBundleNames.Length; j++) 		{			AssetDatabase.RemoveAssetBundleName(oldAssetBundleNames[j],true);		}		length = AssetDatabase.GetAllAssetBundleNames ().Length;		Debug.Log (length);	} 	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"))				{					file (files[i].FullName);				}			}		}	} 	static void file(string source)	{		string _source = Replace (source);		string _assetPath = "Assets" + _source.Substring (Application.dataPath.Length);		string _assetPath2 = _source.Substring (Application.dataPath.Length + 1);		//Debug.Log (_assetPath); 		//在代码中给资源设置AssetBundleName		AssetImporter assetImporter = AssetImporter.GetAtPath (_assetPath);		string assetName = _assetPath2.Substring (_assetPath2.IndexOf("/") + 1);		assetName = assetName.Replace(Path.GetExtension(assetName),".unity3d");		//Debug.Log (assetName);		assetImporter.assetBundleName = assetName;	} 	static string Replace(string s)	{		return s.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;		}	}}

有了这个包含所有资源的依赖关系的.manifest文件,那么在加载使用一个资源的时候,就要根据这个文件,先去加载这个资源依赖的所有资源,然后再加载这个资源,然后就可以使用啦。加载这块,下次再整理。
代码都在这了,工程我就不上传了。
拜了个拜!

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Unity3d热更新(四):压缩文件
Unity3D研究院之Assetbundle的实战(六十三)
【新提醒】【【知识摘录】 Unity3D AssetBundle资源加载-IOS (二)】
【Unity3d基础】Unity3d自动生成asset bundle name(2)
[unity3d]保存文件到本地and加载本地文件
Unity3D 5.3 新版AssetBundle使用方案及策略
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服