简体   繁体   中英

Loading AssetBundle from URL

I'm trying to load assetbundle form server.

My assetbunles is live on URL. Right now my game in editor on webgl platform.

My load assetbundles script:

[Obsolete]
IEnumerator Start()
{
    while (!Caching.ready)
        yield return null;

    using (var www = WWW.LoadFromCacheOrDownload("http://dev71.onlinetestingserver.com/assetBundles/cube", 5))
    {
        yield return www;
        if (!string.IsNullOrEmpty(www.error))
        {
            Debug.Log(www.error);
            yield return null;
        }
        var myLoadedAssetBundle = www.assetBundle;
        var asset = myLoadedAssetBundle.mainAsset;
        Instantiate(www.assetBundle.LoadAsset("cube"));
    }
}

but it gives error:

NullReferenceException: Object reference not set to an instance of an object LoadAssetBundles+d__0.MoveNext () (at Assets/LoadAssetBundles.cs:76) UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Scripting/Coroutines.cs:17)

I just want that when I play my game, it loads asset bundle perfectly. I already created asset bundles.

You aren't checking that for null.

using (var www = WWW.LoadFromCacheOrDownload("http://dev71.onlinetestingserver.com/assetBundles/cube", 5))
{
    yield return www;  <--- www MIGHT BE NULL
    if (!string.IsNullOrEmpty(www.error))
    {
        Debug.Log(www.error);
        yield return null;
    }
    var myLoadedAssetBundle = www.assetBundle; 

    var asset = myLoadedAssetBundle.mainAsset; <--- www.assetBundle MIGHT BE NULL
    Instantiate(www.assetBundle.LoadAsset("cube"));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM