二、AssetBundle下载
一、Downloading AssetBundles(下载资源包)
这里有两种方式下载AssetBundles:
1、非缓存方式下载:通过创建一个WWW类对象来下载(WWW www = new WWW(url)),这种方式不会缓存到本地存储的文件中。
2、缓存方式下载:通过使用WWW.LoadFromCacheOrDownload()进行下载,这种方式会自动缓存在本地存储设备的Unity缓存文件夹中。PC/MAC独立应用程序以及IOS/Android应用程序都有4GB的大小限制。对于其他平台,可以查看相关的脚本文档。
这边直接看官方给的例子(非缓存方式下载):
using System;
using UnityEngine;
using System.Collections;
class NonCachingLoadExample : MonoBehaviour
{
public string BundleURL;
public string AssetName;
IEnumerator Start()
{
BundleURL = "file://"+Application.dataPath + "/StreamingAssets/cube";
// Download the file from the URL. It will not be saved in the Cache
using (WWW www = new WWW(BundleURL))
{
yield return www;
if (www.error != null)
throw new Exception("WWW download had an error:" + www.error);
AssetBundle bundle = www.assetBundle;
if (AssetName == "")
Instantiate(bundle.mainAsset);
else
Instantiate(bundle.LoadAsset(AssetName));
// 实例化完需要从缓存中卸载。
bundle.Unload(false);
} // 自动从Web流中释放内存(使用using()会隐式WWW.Dispose()进行释放)
}
}