Skip to content

Instantly share code, notes, and snippets.

@ShinobiWPS
Last active August 29, 2022 16:14
Show Gist options
  • Save ShinobiWPS/4145313ccd32407c13692b7e2560236e to your computer and use it in GitHub Desktop.
Save ShinobiWPS/4145313ccd32407c13692b7e2560236e to your computer and use it in GitHub Desktop.
ShinobiWPS - Tips to optimize a WebGL build of a Unity game
http://docs.unity3d.com/Manual/webgl-building.html
http://docs.unity3d.com/Manual/ReducingFilesize.html
https://www.youtube.com/watch?v=gVUgF2ZHveo - AssetBundles
Webgl game size Info:
When we compile a Webgl project,it generates 3 folders along with html file.
Compressed
Release
TemplateData
index.html
-> "Release" folder has actual .js files converted from unity's code.
-> "Compressed" folder has gzipped compressed files of above js files generated by unity.
If your server is setup properly to use these gzipped files(from compressed folder) rather than actual js files(in Release folder)(our server had been setup properly for this part),than your actual game size that game will load is the size of "Compressed" folder.
http://forum.unity3d.com/threads/webgl-build-size-for-empty-scene-is-huge-optimisation-tips-and-tricks.276929/
Game Size Reduction/Game Optimisation
-> Use "Automatically Crunched" format for textures with various texture size(512,1024,...) and Compression quality as per needs(this can drastically decrease size of textures,hence game size).
This option is available under "Advanced" tab when you click on any texture in game.
-> Asset bundles: Create Asset bundles(Unity Pro only) of heavy models/prefabs/textures,even full scene and retrieve these bundles later in game from server when it actually opens up(retrieving will use unity's www api),this can also reduce game size.
I can suggest this tool to directly create asset bundles of heavy models/textures or full scenes without much effort on our side.
Bundle Manager:
https://www.assetstore.unity3d.com/en/#!/content/14035
-> Mesh Combiners: Use "Mesh Combiners" to create a 'Single Mesh' of various meshes,this drastically reduce size of Mesh(and indirectly reduces polycount in some cases).
Also,by combining various meshes to a single mesh,all textures will also be combined in single atlas,thus,again reducing size of game.
This also decreases "Draw Calls" in game,hence game will be more responsive and playable in webgl.
I would suggest these assets for Mesh Combining and creating atlases of various textures.
MeshBaker:
https://www.assetstore.unity3d.com/en/#!/content/5017
Simple Mesh Combine:
https://www.assetstore.unity3d.com/en/#!/content/8748
-> Atlas: Pack textures in "Single Atlas" and use "Automatically crunched" or "Compressed" format for this single packed texture.
-> Materials : Use least materials as possible,because more the materials in the game,more the drawcalls,so again,use Mesh Combiners suggested above,to create a single material of many materials.
-> Polycount: 3D Model size indirectly depend upon polycount/tris of that model,so use low poly models,or if you don't have any 3d designer available,don't worry smile i would suggest you "Cruncher" tool,this allows us to reduce polycount of a model/full 3d environment that too inside unity editor wink and pretty simple to use,no need to use max/maya to reduce polycount.
Moreover,lower the polycount of model,better the performance of game.
Cruncher:
https://www.assetstore.unity3d.com/en/#!/content/42948
-> Webgl Streaming:
https://www.assetstore.unity3d.com/en/#!/content/3836821
Using this,webgl will compile .data file(textures/models are compacted in this file and file is in Compressed folder) into multiple .data files that are equal to number of scenes in game(like Webgl.0.datagz, Webgl.1.datagz, Webgl.2.datagz..... ),remove default .data file(Webgl.datagz) from there(with streaming,it will not be used).
Size distribution: Webgl.0.datagz(Scene1 data file),+Webgl.1.datagz(Scene2 data file)+ Webgl.2.datagz(Scene3 data file)+.... = Webgl.datagz(data file without using streaming).
When game opens,it will only load the first scene of game(Webgl.0.datagz),exactly same as in webplayer streaming,thus reducing initial load of game smile All other scenes of game(Webgl.1.datagz,Webgl.2.datagz...) will be loaded later when game actually opens up(other .data files ),you can track in unity either other scenes are loaded or not.
@MuhammadFaizanKhan
Copy link

What about the asset bundles compression? is the compress bundle are better or non-compress bundles?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment