- Home /
Sending multiple requests at the same time kills performance (FPS 120 -> 9)
Hello guys,
My VR app is reading JSON feed and displays it's items as game objects. Each item (10-50 item) contains url with image (1280x800 png) used as texture.
I have to send request for (10-50) images and create textures when I receive responses. This is killing my game performance. It blocks UI completely. It can't render even background sky.
It is slow in editor but worse on Android as my target platform.
Can this be done in background? Can I somehow send multiple request as a more efficient batch? Thanks in advance.
Details:
I'm building game object for each cards by initializing it from prefab. During build I'm filling properties and requesting it's texture. Code doing that:
private void moveModelToControl(List<Cards> cards){
cards.ForEach(c => loadImage(c));
}
private void buildCardGO(c) {
// 1. Init game object from prefab
// 2. Set properties
// 3. Ask for image/texture
loadImage(c);
}
private void loadImage(Card c) {
ImageLoader imgLoader = new ImageLoader();
imgLoader.loadImage ();
}
where getting images from internet is done using WWW and Coroutine
public class ImageLoader {
private readonly Card card;
public ImageLoader(Card card) {
this.card = card;
}
public void loadImage(){
WWW www = new WWW(card.url);
StartCoroutine(WaitForImage(www, onOkAction, handleError));
}
private IEnumerator WaitForImage(WWW www, Action<WWW> onOkAction, Action<WWW> onErrorAction) {
yield return www;
if (www.error == null) {
onOkAction (www);
} else {
onErrorAction (www);
}
}
private void onOkAction(WWW www) {
Texture2D texture = new Texture2D (2, 2, TextureFormat.DXT1, true);
www.LoadImageIntoTexture (texture);
Sprite newImage = Sprite.Create (texture, new Rect (0, 0, (int)texture.width, (int)texture.height), Vector2.zero);
card.sprite = newImage;
}
private void handleError(WWW www) {
///handle error ....
}
}
Your answer
