- Home /
Extra WWW download statistics?
My game loads AssetBundles off the internet while players watch a % climb from 0 to 100, which is rather boring. I am working on a more elegant loading interface, and it would be quite cool if I could show players (at a minimum) how many MB they have downloaded, and how many MB are left to download.
I know most webservers provide the total size of a file in the initial http request response, but is there any way to get this information through Unity's WWW object? Accessing www.size before the download is complete results in a runtime error. "You are trying to load data from a www stream which has not completed the download yet."
I hope it is possible too, but I doubt it. Did you find any good solution?
If you don't need async operations, you can do it safely with `WebRequest.GetResponse` but I've need it to be async and I've been trying to do it on `BeginGetResponse` without much success.
Why couldn't you have a simple script find the total size of the file you are downloading, and then with the percentage do a simple calculation. Say it's 23.4$$anonymous$$B, and at 8%, 23.4*.08 = #$$anonymous$$B downloaded.
You could use PHP to do this script, or the assetbundle on the server could be named according to this size... so like, ASSET_23.4.unity3d
, and then use a command to get the 23.4 by removing the .unity3d and information before _.
Because we want to simplify things, $$anonymous$$roni$$anonymous$$
I provided an example in an answer below. Not too hard!
hi x$$anonymous$$roni$$anonymous$$13x , can I get this script in c# for unity... please.
Answer by ina · Dec 19, 2011 at 03:25 AM
Check out responseheaders! http://answers.unity3d.com/questions/23003/http-reponse-headers.html
Awesome!
Unfortunatley, it doesn't work. Unity's WWW class won't share the response headers it received from the server until it has fully downloaded the file from the server. Why this is, I can't imagine...
If I try to access the ResponseHeadersString from the www class directly, I get this: "'UnityEngine.WWW.get_responseHeadersString()' is inaccessible due to its protection level."
Am I stonewalled, or might there be a way around this?
Answer by xKroniK13x · Jun 06, 2013 at 09:26 PM
It's pretty simple to implement a naming system that would work; just add the size into the file name. I wrote a script for doing that. You can have as many underscores in the name as you want, just the size has to be after the last. I used ASSET_BUNDLE_24.1.unity3d
as a tester for this.
var assetBundleExtension : String; //The extension, minus the '.' , so unity3d for .unity3d
function Start() {
assetBundleExtension = "." + assetBundleExtension;
DownloadAssetBundle("http://full/url/to/bundle_size.unity3d");
}
function DownloadAssetBundle (url : String) {
var fileNameWithExt = Path.GetFileName(url);
var fileName = fileNameWithExt.Replace(assetBundleExtension, "");
fileName = fileName.Substring(0, fileName.LastIndexOf('_'));
var fileSize = (fileNameWithExt.Replace(fileName + "_", "")).Replace(assetBundleExtension, "");
print("Downloading asset bundle: " + fileName + ", size: " + fileSize + "MB.");
}
Throw that in a new UnityScript file and check it out. Just call on the DownloadAssetBundle() function with the url in the parenthesis, and it will decipher all of it for you. Then, you can access fileSize, so if it's at, say, 8%, you could do
var currentSize = .08 * fileSize;
That should do it! I didn't test multiplying it by a percent, you may have to parseint on it, if so just google it. Real simple.
You wouldn't. When you create the assetbundle and upload it to the server, you say how big it is. So when you create the bundle, you check out the size. Alternatively, however you upload it, you can have the PHP script rename it to have the size in $$anonymous$$B at the end.
"check out the size" where? Alternatively? What was the first option?! Which PHP script? If it's going to rename the file, again, how can I tell unity to download a file which I don't know its name?!
Clearly I'm not really following how this would work at all. You got a bundle file on the server with its size on its name, but you don't know which version it is and thus what size it is. How can this work?!
You provided it with a URL to make it download in the past, correct? There's no other way than to provide Unity with a URL. It's no different.
Ok, now I'm almost sure you were the one not following. But unfortunately, I don't think I can explain. $$anonymous$$aybe if you try it. Get your script and make it work. $$anonymous$$aybe you will see what I meant. It just can't work. Conceptually broken.
The whole point of getting the file size is that we don't know it before hand. So we can't set the URL with the file size in its name if we don't know it!!
Your answer
Follow this Question
Related Questions
Display Ftp List 1 Answer
Progress when using WWW.LoadFromCacheOrDownload 2 Answers
how to enter another request from www with c# 1 Answer
How to stop a WWW which is in progress? 0 Answers