Problem downloading an assetbundle from Gamesparks Servers
D
Dimitri Laaraybi
started a topic
over 6 years ago
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
C
Customer Support
said
over 6 years ago
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 //
}
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
Customer Support
said
over 6 years ago
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.
Customer Support
said
over 6 years ago
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 //
}
Dimitri Laaraybi
Hi there !
I'd like to get an assetBundle which I uploaded on the gameSparks Servers just before.
Here's my code:
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 !
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...
This should work for you without any mess,
-Sean
- Oldest First
- Popular
- Newest First
Sorted by Oldest FirstCustomer Support
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
Customer Support
Customer Support
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...
This should work for you without any mess,
-Sean
-
Documentation Notes
-
Design issues with user events
-
Using NoSQL
-
Runtime Collections vs Metadata Collections
-
Anonymous authentication from browser app
-
Modules
-
Movement With Unity
-
Problem with url parameters for downloadables
-
Querying NoSql GameSparks database
-
Challenge accesType
See all 2487 topics