Hello,
Im downloading some textures using WWW every now and then, when i do the whole app freeze. The code is very simple, you can see the freeze even in the editor.
Jjust create a blank scene, then add a 3D cube, add the script below to the cube and run it, you will see cube spinning, but the spinning stops every now and then while its downloading the texture.
Anybody knows how to solve it?
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<Renderer>().material.mainTexture = www.texture;
busy = false;
}
void Update()
{
transform.Rotate(new Vector3(Time.deltaTime * 10, Time.deltaTime * 30, Time.deltaTime * 20));
}
}
It even happens in the Unity Editor, Im downloading a lot of textures and each time it freeze the whole game for a few milliseconds for each texture, its really annoying to the player.
Maybe the freeze happens after the image is downloaded, but while unity is decoding the texture image data (png, jpg etc), maybe it decodes the image data in one shot freezing the entire game.
I have a lot of this WWW everywhere in the code, calling it all day long to fetch different things from server (text), but it never freeze unless im downloading a image.
can someone help, the code is simple (see previous post) just create a new scene, add a cube, and then paste that script above onto the cube, you can see the cube spinning and then the spinning stops and then starts up again for a few milliseconds, and it keeps stopping each time code loads the texture.
any help is appreciated because im screwed with this!!
Since WWW is processed on the main thread, you freezes are normals.
Option 1:
You can try the new Web API instead (Will make WWW deprecated probably).
Option 2:
You can also try to save the textures your are downloading on the disk and load them from it when needed.
To retrieve a texture from the disk, you can use System.IO instead of WWW.
You will have a byte[ ] array you can use with Texture2D.LoadImage.
With System.IO will you also be able to increase the loading time to avoid overhead on your main thread.
But it will still have a high CPU Cost on Texture2D.LoadImage and you probably can’t do nothing about it unless you create a Compute Shader to fill the texture GPU Side. (Not for mobile platforms).
Thanks for your comments fabian, but i cant store the images locally. I will try the WebAPI you mentioned and let you know.
My situation is really horrible since im loading a lot of textures from server and the players character keeps freezing while they are walking or running, looks really crappy and will make them not want to play it, and makes it look like it full of bugs.
The textures are loaded when the player gets near to certain objects, and unloaded when they move away from it. There are a lot of objects so i cant preload them all becuase it was running out of memory before and also taking a long time for the game to start preloading all the textures). Right now as they get near the whole game freeze while its loading the texture.
Im wondering why when a browser loads a webpage containing lots of images its very smooth and does NOT freeze like Unity? Maybe they are using threads?
Maybe i can pass the url of the image i want to load to the external browser, let it load it without the freeze, and then have it pass the image back to unity somehow? Or let it use worker threads to prevent a freeze since the browser has threads?
Do you think when WebGL gets thread capability i could use that to cure this problem?
First, A web page is using JPEG, not PNG (less weight but quality loss).
PNG is not compressed.
Then, A web page display images “as it is” when Unity have to convert it into his own compression format.
You can still try UnityWebRequest instead of WWW to see if it improve the loading time/cost.
I’ve never developed an application on WebGL (Unity or not) but the advice I usually give is to directly use BabylonJS or ThreeJS to make a WebGL app.
When Unity export for WebGL it have to convert your code to C++ then it convert it to JS with IL2CPP and I think you are still very constrained (like no mobile support).