reduce Unity WebGl build size with Asset Bundles

We have an Unity app that contains more than 700 images, right now the build size is a real problem for us, so we're looking into how to reduce the build size.

We wanted to download the images from our server, so we created a little script that downloads images:

public IEnumerator LoadImages(List<string> imageNames, System.Action<int> callback) {
  int downloadedImages = 0;
  foreach (string imageName in imageNames) {
    string imageUrl = spriteNamesAndUrls[imageName];
    var www = new WWW(imageUrl);
    yield return www;
    Texture2D texture = new Texture2D(1, 1);
    www.LoadImageIntoTexture(texture);
    Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.one/2);
    sprites.Add(imageName, sprite);
    downloadedImages += 1;
  }
  callback(downloadedImages);
}

(i simplified the code for this question)

The problem with this script, is that i'm forced to convert hundreds of images into Sprites/Textures, and this is an unnecessary problem to deal with, taking into account that we'll never need to render more than 20 sprites in one scene.

So now i'm looking into using Asset bundles, which seems to be a built-in solution.

My question is,

Is an "Asset Bundle" a good solution for reducing the size of my WebGl build, compared to the script from above?

take into account that i a have an assets folder with more than 700 pictures (19MB total)

Upvotes: 2

Views: 2972

Answers (2)

Oswald Moreno
Oswald Moreno

Reputation: 1

Is an "Asset Bundle" a good solution for reducing the size of my WebGl build, compared to the script from above?

Yes it is, Asset Bundle is a much more powerful tool. You could download and cache any type of assets, including complete scenes.

You could read more about use cases of Assets Bundle here:

https://unity3d.com/es/learn/tutorials/topics/scripting/assetbundles-and-assetbundle-manager

Some best practices while using it:

https://unity3d.com/es/learn/tutorials/topics/best-practices/assetbundle-usage-patterns

Upvotes: 0

Umair M
Umair M

Reputation: 10720

the app just crashes due to memory issues, probably because of the hundreds of http calls.

This does not have anything to do with WebGL biuld size. If you look closely, there is a memory size parameter in webGL build/player settings. where you can increase this to resolve the problem.

enter image description here

However you need to be careful while setting memory size, if it is too large, browser would deny the requested memory and will fail to load the player.

Read more about this here: Memory Considerations when targeting WebGL

HTTP calls is not the issue, the issue is large number of images loaded into browser's memory. If you use asset bundle, you are going to face same problem because you will be loading all those images into browser's memory by creating sprites or however else.

Hope this helps.

Upvotes: 1

Related Questions