unity 5.0以后修改了 bundle的机制 简单来说易用行更强了
在任何的 prefab 或者 材质 资源 下方都有一个 assetbundle的选项
建议命名规则为 物体名称.assetbundle
比如 我有一个 player的预制体
设置其 bundle 名称为 player.assetbundle
打包bundle的方法为
BuildPipeline.BuildAssetBundles(outputPath, BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.StandaloneWindows);
第一个参数为输出路径,第二个为压缩类型,(建议选择LZ4压缩,在解包的时候 unity 会自动解压缩的,第三个就是平台了)
关于加载bundle代码如下
<pre name="code" class="csharp"> AssetBundle manifestBundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/Windows/Windows");
if (manifestBundle != null)
{
Debug.Log("加载本地bundle");
AssetBundleManifest manifest = (AssetBundleManifest)manifestBundle.LoadAsset("AssetBundleManifest");
foreach (var item in Bund)
{
Debug.Log("缓存本地资源信息:"+ item);
string[] bunds = manifest.GetAllDependencies(item);
AssetBundle[] dependsAssetbundle = new AssetBundle[bunds.Length];
for (int index = 0; index < bunds.Length; index++)
{
dependsAssetbundle[index] = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/Windows/" + bunds[index]);
}
AssetBundle Bundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/Windows/" + item);
if (item.Contains("skins"))
{
string _skins = item.Split('.')[0];
Material obj = Bundle.LoadAsset(_skins) as Material;
skins.Add(_skins, obj);
}
else
{
string name = item.Split('.')[0];
try
{
GameObject obj = Bundle.LoadAsset(name) as GameObject;
allEquip.Add(name, obj);
}
catch
{
Debug.LogError("严重错误,本地资源预加载缓存出错,错误的bundle名称为name");
}
}
foreach (AssetBundle _item in dependsAssetbundle)
{
_item.Unload(false);
}
Bundle.Unload(false);
_InsAssetBundle.Add(Bundle);
}
}
isInit = true;
print("bundle初始化完成");
具体流程为 首先加载关联信息的主bundle 然后逐个加载需要的bundle的关联bundle 然后加载使用的bundle, 加载内容为bundle的名称(这就是和为什么我们前面为什么要用命名规则) 中间判断否好友 skin 是我项目里的 请忽略