1

I'm making a game for webgl. But when i upload the assetbundles on the link and then loading it in the game it show error.

error: Failed to decompress data for the AssetBundle 'Memory'. UnityEngine.WWW:get_assetBundle()

NullReferenceException: Object reference not set to an instance of an object DownloadScript+d__2.MoveNext () (at Assets/DownloadScript.cs:27)

When i load asset from my system(local), it load it perfectly. But when i upload it on a live link it is not loading it.

This is my code to load it from server

'''

public class DownloadScript : MonoBehaviour
{
public string url;

// Start is called before the first frame update
[System.Obsolete]
void Start()
{
    StartCoroutine(DownloadModel());
}

[System.Obsolete]
IEnumerator DownloadModel()
{
    WWW wwws = new WWW(url);
    yield return wwws;
    //UnityWebRequest www = UnityWebRequest.GetAssetBundle();
    //yield return www.SendWebRequest();


    AssetBundle assetBundle = wwws.assetBundle;
    Instantiate(assetBundle.LoadAsset("cube"));

}

'''

Code to load from local system:

'''

AssetBundle myLoadedAssetbundle;
public string path;
public string bundleAsset;
void Start()
{
    LoadAssetBundle(path);
    //InstantiateObjectFromBundle(bundleAsset);
    StartCoroutine(DownloadAndCache(path));
}

void LoadAssetBundle(string bundleUrl)
{
    myLoadedAssetbundle = AssetBundle.LoadFromFile(bundleUrl);
    //myLoadedAssetbundle = AssetBundle.

    Debug.Log(myLoadedAssetbundle == null ? "Failed to load AssetBundle" : "AssetBundle Succesfully Loaded");
}

void InstantiateObjectFromBundle(string assetName)
{
    var prefab = myLoadedAssetbundle.LoadAsset(assetName);
    Instantiate(prefab);
}

'''

gman
  • 100,619
  • 31
  • 269
  • 393
Aqib Ahmed
  • 29
  • 1
  • 7

1 Answers1

2

Are you sure you are also building your assetbundles with the right platform in mind? Assetbundles built for desktop wont necessarily work for webgl.

TJHeuvel
  • 12,403
  • 4
  • 37
  • 46
  • is the way of generating assetbundles is different for webgl? – Aqib Ahmed Oct 04 '19 at 09:03
  • Right now platform is webgl selected for the game. With this when i used to load it from local in unity, it works fine. But when in unity i try to load it from the server its doesn't work – Aqib Ahmed Oct 04 '19 at 09:05
  • 1
    > 'is the way of generating assetbundles is different for webgl' Yes, very much so. When you create assetbundles you supply a platform, make sure you are using that platform. – TJHeuvel Oct 04 '19 at 13:14
  • Can you give little bit more brief on this? I didn't understand it – Aqib Ahmed Oct 04 '19 at 14:34
  • Asset bundles are platform specific. When you generate one you must specify what platform it is for. Different platforms have different needs and limitations. For instance, memory in WebGL is significantly more limited then in a desktop app. – user430788 Jun 30 '21 at 16:47