WWW类废除,Unity提供了更加快捷的获取方式 ,如下:
IEnumerator GetAssetBundle()
{
//AB包路径
string bundlePath = Application.streamingAssetsPath + “/textrue”;
//直接使用UnityWebRequestAssetBundle获得AB包
UnityWebRequest www = UnityWebRequestAssetBundle.GetAssetBundle(bundlePath);
yield return www.SendWebRequest();
//最终获得
AssetBundle ab= DownloadHandlerAssetBundle.GetContent(www);
}
eg:2
///
/// 加载非预制对象
///
/// 本地路径or远端地址
/// 操作且显示的对象
/// 欲加载资源的名称
///
IEnumerator LoadNoObjAsset(string URL,GameObject showObj,string assetName)
{
//直接使用UnityWebRequestAssetBundle获得AB包
using (UnityWebRequest www = UnityWebRequestAssetBundle.GetAssetBundle(URL))
{
yield return www.SendWebRequest();
//最终获得
AssetBundle ab = DownloadHandlerAssetBundle.GetContent(www);
if (ab != null)
{
showObj.GetComponent().material.mainTexture = ab.LoadAsset(assetName);
//卸载资源(只卸载AB包本身)
ab.Unload(false);
}
else Debug.Log(GetType() + “下载的AB包为空!” +“错误信息”+www.error);
}
}