0

I'm trying to download image from a server to my Unity WebGL app. I used WWW and it works but it has problems where it freezes the whole WebGL app in the browser while its downloading. This simple code works, but it stops the app while its download the image.

WWW loader = new WWW(url);
yield return loader;

I already exhausted all possibilities of using Unity to download the image, since it either freeze or dont work,

Instead, I want to see if its possible to for unity to tell the browser to download the image for me using javascript, then have it pass that image back to my unity c# code somehow.

Anybody know how to do that using external java script? I know it can do it because I have code that can send a picture from Unity to the server using external java script and it works without freezing the app, so it must can work the other way also.

Thanks!

cweiske
  • 30,033
  • 14
  • 133
  • 194
Programmer2
  • 69
  • 11

1 Answers1

0

I haven't seen instance of WWW freezing in WebGL build. You are likely doing something wrong and the two lines of code in your question cannot be used to tell what the problem is.

If the WWW API is indeed the problem then use UnityWebRequest. There is no need to use Javascript for this.

void Start()
{
    StartCoroutine(downloadTexture("http://www.myUrl.png"));
}

IEnumerator downloadTexture(string url)
{
    using (UnityWebRequest www = UnityWebRequest.GetTexture(url))
    {
        yield return www.Send();

        if (www.isError)
        {
            Debug.Log(www.error);
        }
        else
        {
            Texture myTexture = DownloadHandlerTexture.GetContent(www);
        }
    }
}
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • There is a freeze using WWW in WebGL, I also tried that code you have, it gives an error on the last line when i run it "error m_instandeID!=0" please try it and see – Programmer2 Dec 23 '16 at 01:18
  • All you have to do is spin a cube while downloading several pics using WWW and you will see the cube stop and start spinning many times as its downloading the pics, even in the editor it happens. This is a big problem because the user will see that stop and start looks really crappy!!! – Programmer2 Dec 23 '16 at 02:32
  • As the error, that is a bug. See [here](http://stackoverflow.com/q/39628951/3785314) for your `error m_instandeID!=0` error. Simply add `yield return new WaitForEndOfFrame();` before calling the `DownloadHandlerTexture.GetContent` function. If you still have problems after this, please Update to Unity 5.5. It is very likely that any bug related to this is now fixed. – Programmer Dec 23 '16 at 02:57
  • 1
    @Programmer, listen to the Programmer, he's a programmer – Nika Kasradze Dec 23 '16 at 12:15
  • @programmer, i did the WaitForEndOfFrame, and the yield, it still gives the error. I have unity 5.4, Version 5.5 breaks other things i have in my code, thats why i havnt upgraded to it. I cant believe this EndOfFrame and WWW bug is in there all this time and nobody in unity has fixed it. WWW is the most fundamental thing in unity, if that doesnt work then they may as well hang it up. – Programmer2 Dec 23 '16 at 12:24
  • I understand your frustration. Can you modify your question with the code the complete code that does not work? Also include the url. I will try this and decide if this is a bug. – Programmer Dec 23 '16 at 17:35
  • @NikaKasradze lol we share the-same name its hard to tell who said what – Programmer Dec 23 '16 at 17:35