1.加载AB包的流程
- 指定资源的Asset Bundle属性
- 构建Asset Bundle包
- 上传AB包
- 加载AB包和包里的资源
2.指定资源的AssetBundle属性
3.构建Asset Bundle包
4. 加载AB包和包里的资源(加载本地的AB包)
5.AB包分组策略
6.解压方式
7.依赖包
8.加载AB包的几种方式
- 从内存中异步加载AB包(LoadFromMemoryAsync)
///异步从内存中加载AB
IEnumerator LoadFromMemoryAsync()
{
//从内存中异步加载Ab
AssetBundleCreateRequest request= AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes("AssetBundle/wall.unity3d"));
yield return request;//等待加载完毕
//获取AB包
AssetBundle ab = request.assetBundle;
//加载AB包中的资源
GameObject obj= ab.LoadAsset<GameObject>("Cube");
//实例化AB包中的资源
Instantiate(obj);
}
2.从本地异步加载AB包(LoadFromFileAsync)
/// <summary>
/// 异步从本地加载AB
/// </summary>
/// <returns></returns>
private IEnumerator LoadFromFileAsync()
{
AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync("AssetBundle/wall.unity3d");
yield return request;//等待加载完毕
//获取AB包
AssetBundle ab = request.assetBundle;
//加载AB包中的资源
GameObject obj = ab.LoadAsset<GameObject>("Cube");
//实例化AB包中的资源
Instantiate(obj);
}
3.使用WWW类加载(LoadFromCacheOrDownload)
/// <summary>
/// www类加载AB包(被弃用)
/// </summary>
/// <returns></returns>
private IEnumerator LoadFromCacheOrDownload()
{
while (Caching.ready==false)//判断是否可以缓存
{
yield return null;
}
//开始本地加载
// WWW www = WWW.LoadFromCacheOrDownload(@"file:///G:\UnityProjects\Unity2018.2.0Projects\AssetBundle\AssetBundle\wall.unity3d",1);
//开始从服务器上加载
WWW www = WWW.LoadFromCacheOrDownload(@"http://localhost/AssetBundle/AssetBundle/wall.unity3d",1);
yield return www;//等待加载完毕
if (string.IsNullOrEmpty(www.error)==false)//判断是否有错误
{
Debug.Log(www.error);
yield break;
}
///使用资源
AssetBundle ab = www.assetBundle;
Instantiate( ab.LoadAsset<GameObject>("Cube"));
}
4.使用UnityWebRequest加载
/// <summary>
/// 使用UnityWebRequest加载
/// </summary>
/// <returns></returns>
private IEnumerator UnityWebRequestAB()
{
//从本地加载AB包
// UnityWebRequest request= UnityWebRequestAssetBundle.GetAssetBundle(@"file:///G:\UnityProjects\Unity2018.2.0Projects\AssetBundle\AssetBundle\wall.unity3d");
//从服务器端加载AB包
UnityWebRequest request= UnityWebRequestAssetBundle.GetAssetBundle(@"http://localhost/AssetBundle/wall.unity3d");
yield return request.SendWebRequest();//发送Web请求
//第一种得到AssetBundle对象
//AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);
//第二种得到AssetBundle对象
AssetBundle ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
//实例化
Instantiate(ab.LoadAsset<GameObject>("Cube"));
}
9.通过Manifest加载某个包的依赖包
/// <summary>
/// 加载依赖包
/// </summary>
private void LoadDependencies()
{
AssetBundle manifestAB = AssetBundle.LoadFromFile("AssetBundle/AssetBundle");//加载AssetBundle的AB包
//加载AssetBundle对应的Manifest
AssetBundleManifest manifest = manifestAB.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
//得到所有的AB包
//foreach (string name in manifest.GetAllAssetBundles())
//{
// print(name);
//}
//得到所有名字为wall.unity3d的依赖包
string[] str = manifest.GetAllDependencies("wall.unity3d");
foreach (string name in str)
{
print(name);
//加载依赖包
AssetBundle.LoadFromFile("AssetBundle/"+name);
}
}