Hi All, I have a complex JSON string to parse and normally deserialization process(JSON string to C# class object) happens on MAIN thread, which was consuming around 70-100ms time of game's MAIN thread and this is going to happen 2-5times per frame, which is really bad and blocking game's MAIN thread. As Alternative: Then i thought, i can move deserialization process to OTHER thread (System.Threading, to save MAIN thread time) and merge result to MAIN thread. For testing i wrote this simple code, Code (csharp): void CreateCustomThread() { try { Thread t = new Thread(MyWorkerThread); t.Start(); }catch(Exception e) { Debug.LogError("Error while starting thread: '" + e.Message + "'");[/INDENT] } } const int MAX_ITERATION = 5; void MyWorkerThread() { int l_completedIteraction = 0; while(l_completedIteraction < MAX_ITERATION) { Thread.Sleep(2000); l_completedIteraction++; } } but its throwing "Exception: Thread creation failed.". 1. Is THREADs not supported on WebGL platform? 2. Is there any other way to achieve multi-threading concept on WebGL platform? EDIT: [Temp solution]: I had to hardcode my class type, its fields & properties in JSON library to eliminate using C# Reflection. This increased CPU performance from 200-250ms to 4-7ms and memory 600+KB to 2-5 KB.. Also i had to put ObjectPool concept for my class types in JSON library. This works for me.
from the manual: "Threads are not supported due to the lack of threading supporting in JavaScript. This applies to both Unity’s internal use of threads to speed up performance, and to the use of threads in script code and managed dlls. Essentially, anything in the System.Threading namespace is not supported." not really. The closest thing you can use are coroutines.
Will threads EVER be supported? if not will there be any alternative because im having problems with freeze also when loading textures, its a BIG problem when the game freeze10 times when i load 10 textures. But strange it doesnt freeze when playing a video using WebGLMovieTexture, wouldnt playing a VIDEO (lots of textures) more likely to freeze? but it doesnt, why cant that method be used when loading textures?
Threads are on our roadmap but since we depend on browser vendors we don't have an ETA at the moment.
Can't share memory with web workers atm. What Unity needs is "Shared Array Buffer" support among browsers. It's coming, but not in the initial release of wasm (same as simd support).
But why does it freeze when loading ONE texture, but has ZERO freeze when streaming a video? Wouldnt a video have more demands than a 128x128 png pixel image? loading that small image using WWW causes a freeze, but streaming a video has no freeze.
the WebGLMovieTexture sample streams movies. Textures are loaded synchronously by the main thread, that's why it freezes.
I use coroutines for that, no freezing. dummy: Code (CSharp): Texture 2D myTexture; void tryAssignTexture() { if (myTexture == null) StartCoroutine(downloadTexture); else assignTexture(); } assignTexture() { myMaterial.SetTexture("_MaintTex", texture); } IEnumerator downloadTexture() { //... myTexture = download... assignTexture(); } In case the request can be sent several times in a short interval (or just as good practice) use a global bool downloadPending, to bridge the gap between texture download request and download complete.
Your code looks like it will freeze also, i dont see it doing anything different from what im doing. Here is how you can tell if it will freeze. Make a new scene, create a simple cube, attach a script to that cube that spins the cube constantly in the update function. While the cube is spinning, put your coroutine in there that downloads the texture and assigns it, see if the cube ever pauses spinning for a little, if it pauses then your code freezes just like mine. make sure your code does it over and over, keep downloading and assigning the texture, you will see that the cube keeps stopping when you assign the texture to the cube.
Can some SMART engineer at Unity PLEASE create a coroutine/async version of LoadImage, or LoadRawImage that doesnt freeze the main thread? This is so badly needed because my game and so many other people games are messed up because of this and there is no way around it because those are the only functions we can use use to assign textures, and its on the main thread that freeze the game. My game is especially horrible because im loading many LARGE images from the internet and applying them to various objects in the scene all the time during the game, and its really horrible to watch it glitching like mad.