Threading and WebGL

Anyone know solution for this problem: Screenshot by Lightshot Im using socket.io plugin by fpanteieri GitHub - fpanettieri/unity-socket.io-DEPRECATED: Socket.IO client for Unity3d i rebuilded game with that plugin dissabled and game works. Whole error message in console log says something about “threading”.

Also, this happens only when I build WebGL. As “exe” game works perfectly.

WebGL doesn’t support threading right now. It requires upgraded browser support so there’s not much Unity can do about it except wait.

–Eric

Need threading badly, every time i download a texture using WWW in a coroutine, the whole game still freezes, and i have to download a lot of textures, so the player constantly freeze in place it really sucks, this is the very worst part of my game right now.

It would be great if i coul d put that part of it on a separate thread.

When do you think threading for WebGl will be ready?

That’s unrelated to threading; coroutines don’t use threads anyway.

–Eric

I know coroutines are not the same as threading. Coroutines are no good for downloading things while the game is running, it makes the game freeze (it shouldnt, but it does). threads would be better!

Asking for a feature to be threaded in Unity is quite different from this topic, which is about threading in WebGL. Anyway I think the problem may be something you’re doing in your code, since downloading textures with WWW doesn’t cause any freezing in the editor or with WebGL here.

–Eric

The whole game freeze for a few milli-seconds everytime it downloads a texture, and its downloading a LOT of textures! Here is the code im using, please let me know if you see anything wrong that will cause the freeze, thanks


… StartCoroutine(getPic(pic,“http://www.website.com/pic.jpg”));

IEnumerator getPic(GameObject pic,string url)
{
WWW loader = new WWW(url);
yield return loader;

DestroyImmediate(pic.GetComponent().material.mainTexture);
pic.GetComponent().material.mainTexture = null;

Texture2D Tex = new Texture2D(loader.texture.width, loader.texture.height, TextureFormat.DXT1, false);
loader.LoadImageIntoTexture(Tex);
pic.GetComponent().material.mainTexture = Tex;

DestroyImmediate(loader.texture);
loader.Dispose();
loader = null;
Resources.UnloadUnusedAssets();
}

Try it without all of the destroying/setting to null/dispose/etc., which seems like overkill and is what’s probably causing the freezing.

–Eric

No it still freeze, I made a new project containing only a cube, and a script to spin the cube while loading a texture form a url using www, and i can easily see the cube stopping while the texture is being loaded. The code doesnt even asign the texture to anything or destroy anything, yet the cube still freeze during loading.

This is exactly whats happening in my game.

Maybe you could post your code that you say doesnt freeze?

A 3.5MB 2560 x 1920 .png with no freezing: starscenesoftware.com - starscenesoftware Resources and Information.

#pragma strict
import UnityEngine.UI;

var text : Text;

function Start () {
    var www = new WWW ("https://starscenesoftware.com/test/pic.png");
    while (!www.isDone) {
        text.text = www.progress.ToString();
        yield;
    }
    GetComponent(Renderer).material.mainTexture = www.texture;
    text.text = "";
}

–Eric

Your code is located in the start function, so if there is a freeze you cant tell.

If you want to see the freeze in the editor, simple create a new scene with just a 3D cube, then attach the script below to the cube, then run it, you can see the cube spinning, then it freeze every few seconds right there in the editor.

Please show me how to modify the script so that the cube never freeze.

Thanks!

using UnityEngine;
using System.Collections;

public class SpinCube : MonoBehaviour {
bool busy = false;

void Start()
{
FetchPic();
}

void FetchPic()
{
if (!busy) {
StartCoroutine(GetPic());
}
Invoke(“FetchPic”, 1.0f);
}

IEnumerator GetPic()
{
busy = true;
var www = new WWW(“https://starscenesoftware.com/test/pic.png”);
yield return www;
gameObject.GetComponent().material.mainTexture = www.texture;
busy = false;
}

void Update()
{
transform.Rotate(new Vector3(Time.deltaTime * 10, Time.deltaTime * 30, Time.deltaTime * 20));
}
}

Er, no it’s a coroutine. Did you actually read it, or look at the demo? (Use code tags when posting code.)

–Eric

Oh, sorry I didnt notice the demo link, yes i checked it out thanks, Could you look at my code above and let me know whats causing the pausing?

Well made demo!
It doesn’t freeze for the whole downloading process, but just for a second or a fraction of second, just before the image appears on the screen.

I get a brief hiccup, which is the texture being uploaded to the GPU once the download is complete; that happens in any kind of build. Although it might be a bit slower with WebGL.

–Eric

Agree, this is not freezing, but a hiccup.:slight_smile:

Yes hiccup when loading just ONE texture, but im loading maybe 20 of them, results in a freeze during that time.

I am getting the same kind of hiccups, when using:

uploadedTexture.LoadImage(File.ReadAllBytes(texturePath));

(in WebGL), where texturePath points to a file within persistentDataPath.

Uploading a texture to the GPU takes time, depending on hardware and the size of the texture.

–Eric