WWW废除后使用UnityWebRequest下载AB包(AssetBundle)

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);

    }
     
}
原创文章 1 获赞 1 访问量 41

猜你喜欢

转载自blog.csdn.net/qq754563639/article/details/105795645