- Home /
WebPlayer dataPath
Hello! I want to load an assetBundle from the server to economy the loading time. I searched a huge variety of topics and finally I read that the best method to load assetBundles is using the Application.dataPath (read it here).
First I create a Bundle, using BuildPipeline.BuildAssetBundle from documentaion (here). Then I used this code to load it:
function Start () {
var www = new WWW("file://" + Application.dataPath + "/myBundle.unity3d");
Instantiate(www.assetBundle.mainAsset);
}
Worked fine for editor (my bundle was in the Assets folder) and also worked in .exe, when I changed the code like this: "file://" + Application.dataPath + "/../myBundle.unity3d (my bundle was in the same directory, as the .exe file)
But nothing works in WebPlayer! I feel it should be very simple, but I really can't understand what to change. Help me please, sure that this question is interesting for many people.
Thank you very much!
Answer by Spatiulus · Jul 29, 2012 at 11:11 AM
THE PROBLEM IS SOLVED! I added a GUI text, which shows the log to watch it in WebPlayer. So he wrote me, that the WebPlayer just didn't had time to download my bundle on function Start! The action should be repeated, until the download is over. Through my ignorance, I missed yield www, from the assetBundle documentation. Just because didn't knew it's meaning. Hope this experience will help somebody like me. :)
Here is my working code (a little modified):
var assetPath;
function Start () {
if (Application.isEditor) {
assetPath = "file://" + Application.dataPath + "/Bundle.unity3d";
}
else {
assetPath = Application.dataPath + "/Bundle.unity3d";
}
www = new WWW(assetPath);
yield www; // HERE YOU ARE!
Instantiate(www.assetBundle.mainAsset);
}
Answer by earth5 · Jul 29, 2012 at 11:10 AM
As I understand it, Web Player can't use Application attributes/functions/etc. They need to be stored on a remote server and accessed via http:// instead. This was driving me nuts too.
Thanks for answering, but I solved the problem! The solution is moderating now, and should be published here soon! :)
According to Unity Script Reference http://docs.unity3d.com/Documentation/ScriptReference/Application-dataPath.html Application.dataPath does exist for Web Player, and it points to the URL to the player data file folder