- 5.0以后打包更简单,调用BuildPipeline.BuildAssetBundles,引擎将自动根据资源的assetbundleName属性批量打包,自动建立Bundle以及资源之间的依赖关系。
- 5.0以后的版本中,一些4.x中的旧有策略被默认开启。 CompleteAssets:用于保证资源的完备性,默认开启;CollectDependencies:用于收集资源的依赖项,默认开启;DeterministicAssetBundle:用于为资源维护固定ID,默认开启。
- 5.0以后版本打包时,无法指定Assetbundle.mainAsset,因此无法再通过mainAsset来直接获取资源。
- 如果项目中只会运用到.prefab,那么只需要给prefab设置assetbundleName,相关的贴图、材质不必设置,最后加载prefab并实例化后材质贴图仍会存在。但如果需单独运用prefab身上的贴图或材质,则这些贴图材质也需要设置assetbundleName。
以Window平台为例:
--------------------------------------打包------------------------------------------
- 5.X版本打包时与4.X版本打包时,注意文件的名称不同,5.X:名称为assetbundleName,4.X:名称为xxx.assetbundle。
- 当AssetBundle标记的对象很多时,想要看到包含某个字符串的AssetBundle(可能有多个)中的资源时,可以单击AssetBundle的名称选项,在弹出的菜单中选择Filter Seleted Name;或者在Project视图搜索“b.AssetBundle名称”。
--------------------打包方法一:提前设置AssetBundleName----------------------
public static AssetBundleManifest BuildAssetBundles(string outputPath, BuildAssetBundleOptions assetBundleOptions, BuildTarget targetPlatform);
[MenuItem("AssetBundle/AssetBundleWindow5.0Method1")]
public static void BuildAssetToWindow5_0Method1()
{
string outPath =
Application.dataPath + "/../BuildAssetBundle/Window/";
CreateDir(outPath);
BuildPipeline.BuildAssetBundles(outPath,
BuildAssetBundleOptions.ForceRebuildAssetBundle, BuildTarget.StandaloneWindows);
AssetDatabase.Refresh();
}
public void CreateDir(string path)
{
DirectoryInfo info = new DirectoryInfo(path);
if (!info.Exists)
{
info.Create();
}
}
设置assetbundleName: 将打包得到的文件拷贝到StreamingAssets中:
--------------------打包方法二:动态设置AssetBundleName----------------------
public static AssetBundleManifest BuildAssetBundles(string outputPath, AssetBundleBuild[] builds, BuildAssetBundleOptions assetBundleOptions, BuildTarget targetPlatform);
- 所要打包资源的后缀名要写。
[MenuItem("AssetBundle/AssetBundleWindow5.0Method2")]
public static void BuildAssetToWindow5_0Method2()
{
string outPath = Application.dataPath + "/../BuildAssetBundle/Window/";
CreateDir(outPath);
AssetBundleBuild[] builds = new AssetBundleBuild[1];
builds[0].
assetBundleName = "moshushi";
string[] paths = new string[5];
paths[0] = "Assets/AssetBundle/MoShuShi.prefab";
paths[1] = "Assets/Model/Materials/fk02_02_moshushi.mat";
paths[2] = "Assets/Model/Materials/fk02_02_tuzi.mat";
paths[3] = "Assets/Model/fk02_02_moshushi.jpg";
paths[4] = "Assets/Model/fk02_02_tuzi.jpg";
builds[0].
assetNames = paths;
BuildPipeline.BuildAssetBundles(outPath,
builds,
BuildAssetBundleOptions.ForceRebuildAssetBundle, BuildTarget.StandaloneWindows);
AssetDatabase.Refresh();
}
--------------------------------------加载------------------------------------------
- 5.X版本打包出来后与4.X版本打包出来后进行下载时,注意下载文件的名称,5.X:名称为assetbundleName,4.X:名称为xxx.assetbundle
using System.Collections;
using System.IO;
using UnityEngine;
public class DownLoadASB : MonoBehaviour
{
public GameObject Sphere;
private string ModelPath;//服务器路径
private string LocalPath;//本地路径
void Awake()
{
#if UNITY_EDITOR
ModelPath = "file:///" + Application.streamingAssetsPath + "
/moshushi";
LocalPath = Application.persistentDataPath + "/Model/
moshushi";
#endif
#if UNITY_ANDROID
ModelPath = Application.streamingAssetsPath + "/moshushi";
LocalPath = Application.persistentDataPath + "/Model/moshushi";
#endif
#if UNITY_IPHONE
ModelPath = "file:///"+Application.streamingAssetsPath + "/moshushi";
LocalPath = Application.persistentDataPath + "/Model/moshushi";
#endif
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (File.Exists(LocalPath))//本地存在
{
print("本地下载" + LocalPath);
StartCoroutine(DownLoadModel("file:///" + LocalPath));
}
else
{
print("服务器下载");
StartCoroutine(DownLoadModel(ModelPath));
}
}
}
IEnumerator DownLoadModel(string path)
{
WWW www = new WWW(path);
yield return www;
if (www.error == null)
{
byte[] bytes = www.bytes;
CreateDir(Application.persistentDataPath + "/Model");
if (!File.Exists(LocalPath))
{
File.WriteAllBytes(LocalPath, bytes);
}
AssetBundle bundle = www.assetBundle;
--------------------------加载资源方法一:同步加载------------------------------
GameObject go = bundle.LoadAsset("MoShuShi") as GameObject;
Instantiate(go, Vector3.zero, Quaternion.identity);
Material mat = bundle.LoadAsset("fk02_02_moshushi.mat") as Material;
Sphere.GetComponent().material = mat;
--------------------------加载资源方法二:异步加载------------------------------
AssetBundleRequest request1 = bundle.LoadAssetAsync("MoShuShi");
yield return request1;
if (request1.isDone)
{
GameObject Model = request1.asset as GameObject;
if (Model != null)
{
Instantiate(Model, Vector3.zero, Quaternion.identity);
}
else
{
print("资源不存在");
}
}
AssetBundleRequest request2 = bundle.LoadAssetAsync("fk02_02_moshushi");
yield return request2;
if (request2.isDone)
{
Material newmat = request2.asset as Material;
if (mat != null)
{
Sphere.GetComponent().material = newmat;
}
else
{
print("贴图不存在");
}
}
bundle.Unload(false);
}
else
{
print(www.error);
}
}
public void CreateDir(string path)
{
DirectoryInfo info = new DirectoryInfo(path);
if (!info.Exists)
{
info.Create();
}
}
}
本地路径:
项目运行前: 项目运行后: