Unity中使用UnityWebRequest加载AssetBundle

1.使用UnityWebRequest需要引用using UnityEngine.Networking.
2.UnityWebRequest中有几个方法,UnityWebRequest.GetAssetBundle(URL)获取assetBundle资源,有一个返回资源的函数SendWebRequest,用来下载资源,DownloadHandlerAssetBundle.GetContent()未获取assetBundle资源包,接着就是LoadAsset<>()加载,实例化。
3.代码如下
void Start () {
StartCoroutine(Load());
}

private IEnumerator Load()
{
    string url = "";//此为AssetBundle资源路径,可为本地,也可以是服务端
   UnityWebRequest request=  UnityWebRequest.GetAssetBundle(url);
    yield return request.SendWebRequest ();
    // AssetBundle ab = DownloadHandlerAssetBundle.GetContent (request );

    AssetBundle ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
    GameObject ao = ab.LoadAsset("Man1");
    Instantiate(ao);
}

4.多看看UnityApi。

你可能感兴趣的:(unity,assetbundle)