Sign In Register

How can we help you today?

Start a new topic
Answered

Problem downloading an assetbundle from Gamesparks Servers

Hi there !


I'd like to get an assetBundle which I uploaded on the gameSparks Servers just before.


Here's my code:

  

using System;
using UnityEngine;
using System.Collections;
using GameSparks.Api;
public class AssetbundleController : MonoBehaviour
{
    public string AssetName;
    public string GameSparksDLCShortCode;
    public int version;
    private string gamesparksURLResponse;
    void Start()
    {
        GameController.OnAuth += CheckUpdate;
    }
    void CheckUpdate()
    {
        StartCoroutine(DownloadAndCache());
    }
    IEnumerator DownloadAndCache()
    {
        // Wait for the Caching system to be ready
        while (!Caching.ready)
        {
            yield return null;
        }
        new GameSparks.Api.Requests.GetDownloadableRequest().SetShortCode(GameSparksDLCShortCode).Send((response) =>
            {
                if (response.HasErrors)
                {
                    Debug.Log("Failed to download");
                }
                else
                {
                    gamesparksURLResponse = response.Url;
                    Debug.Log("DLC URL: " + gamesparksURLResponse);
                }
            });
        // Load the AssetBundle file from Cache if it exists with the same version or download and store it in the cache
        using (WWW www = WWW.LoadFromCacheOrDownload(gamesparksURLResponse, version))
        {
            yield return www;
            if (www.error != null)
                throw new Exception("WWW download had an error:" + www.error);
            AssetBundle bundle = www.assetBundle;
            if (AssetName == "")
            {
                UnityEngine.Object[] tmp = bundle.LoadAllAssets();
                foreach(UnityEngine.Object a in tmp)
                {
                    Instantiate(a);
                }
            }
            else
                Instantiate(bundle.LoadAsset(AssetName));
            // Unload the AssetBundles compressed contents to conserve memory
            bundle.Unload(false);
        } // memory is freed from the web stream (www.Dispose() gets called implicitly)
    }
} 

  

I can get my url through this code. When I put it in my browser, I can download my asset bundle. But when I start play in the Unity Editor, it does'nt work, here's the error I get:


Exception: WWW download had an error:<url> malformed (line 49)


The URL I get with the GetDownloadableRequest() is working because I can download my file with the URL if I put it in my Chrome browser, but it doesn't work in Unity Editor.


Thank you in advance for your help !


Best Answer
Okay Dimitri, here is a better solution using a callback...

 

	public delegate void GetDownloadablsCallback(AssetBundle _bundle);
	public void GetDownloadable(GetDownloadablsCallback _getDownloadableCallback)
	{
		Debug.Log("GS| Fetching download URL...");
		new GameSparks.Api.Requests.GetDownloadableRequest()
			.SetShortCode("GSKit_TESTBNDL")
			.SetDurable(true)
			.Send((response) => {
					
					if(!response.HasErrors)
					{
						Debug.Log("GS| Got URL...");
						using (WWW www = WWW.LoadFromCacheOrDownload(response.Url, 1))
						{
							if (www.error != null)
							{
								throw new Exception("WWW download had an error:" + www.error);
							}
							else
							{
								Debug.Log("GS| Got Asset Bundle...");
								_getDownloadableCallback(www.assetBundle);
							}
						}
					}
				});
	}

 

 How when you what to download the asset-bundle you call the GetDownloadable, and assign a callback where you can handle the asset-bundle and instantiate those assets...


 

GetDownloadable(GetAssetBundle); // use where you call the method


	void GetAssetBundle (AssetBundle _bundle) 
	{
		AssetBundle bundle = _bundle;
		UnityEngine.Object[] assetList = bundle.LoadAll();
		// instantiate assets //
	}

 

This should work for you without any mess,

-Sean



Hey Dimitri,

So the problem here is that the Gamesparks request needs some time to get the response back. In your code, the gamesparks request is sent, and then code will step over the response and ask for the download before the url has ever been returned the url. Since we are talking about a couple-hundred milliseconds here, it looks like everything went okay, but really the url is still empty when you sent it.

To fix this all i had to do is stick your WWWform request directly after you get the url response back, so you request the asset bundle immediately after the response is returned. I've tested that solution with your code and it works no problems.

- Sean

 

Another alternative is to use a callback function when you get the response and you can call the callback method where you can instantiate your assets.

 

Answer
Okay Dimitri, here is a better solution using a callback...

 

	public delegate void GetDownloadablsCallback(AssetBundle _bundle);
	public void GetDownloadable(GetDownloadablsCallback _getDownloadableCallback)
	{
		Debug.Log("GS| Fetching download URL...");
		new GameSparks.Api.Requests.GetDownloadableRequest()
			.SetShortCode("GSKit_TESTBNDL")
			.SetDurable(true)
			.Send((response) => {
					
					if(!response.HasErrors)
					{
						Debug.Log("GS| Got URL...");
						using (WWW www = WWW.LoadFromCacheOrDownload(response.Url, 1))
						{
							if (www.error != null)
							{
								throw new Exception("WWW download had an error:" + www.error);
							}
							else
							{
								Debug.Log("GS| Got Asset Bundle...");
								_getDownloadableCallback(www.assetBundle);
							}
						}
					}
				});
	}

 

 How when you what to download the asset-bundle you call the GetDownloadable, and assign a callback where you can handle the asset-bundle and instantiate those assets...


 

GetDownloadable(GetAssetBundle); // use where you call the method


	void GetAssetBundle (AssetBundle _bundle) 
	{
		AssetBundle bundle = _bundle;
		UnityEngine.Object[] assetList = bundle.LoadAll();
		// instantiate assets //
	}

 

This should work for you without any mess,

-Sean


Login to post a comment