Tips and tricks for using WebGL on desktop and mobile (tested up to 2021.3.11f1)

$(document).blur(... unityInstance.sendMessage(...) ...) my dude, from https://api.jquery.com/blur/ and https://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html

1 Like

Thanks. Let me try and give an update first. Do you know how to close popup window, browser tab from the game instead of redirecting to a blank page?

That’s more of a general Javascript question. “You can’t close any tab via JavaScript. ‘This method (window.close) is only allowed to be called for windows that were opened by a script using the window.open method.’ In other words, you can only use JavaScript to close a window/tab that was spawned via JavaScript.”

So, as I understood is, I need to call JS function from the script when player terminates the game. Isn’t it?

I tried following every step you did but i still cant to get the webgl working on any of my IOS mobile device . I tried with mac book / android phones / windows and etc works fine. but as for IOS mobile device the loading stuck at 90% even if i’m using Safari browser / chrome on Iphone X … anyone have any updates or solution on this ?

Addressables is exactly the same as Asset Bundles. The issue you speak of is solved by leaving the bundles uncompressed, then doing a gzip/brotli compression after they’re built. Then on your content server, set the headers as gzip/br and when it’s downloaded through addressables or assetbundles, the browser will automatically decompress them using whatever system resources it wants (off the main thread I think). Plus, once it’s decompressed, it will be stored in the indexedDB in an uncompressed state, thus improving your load times from cache (no need to decompresss).

Try select “asm.js” as a linker target for the build.

I guess it’s relevant here
https://gist.github.com/De-Panther/f08cb4902bd21ee403d579a12510be2c

Hi @doctorpangloss
im aiming to build webgl for recent ios devices , do you still see that necessary to compile for asm.js ? my project with brotli compression format is about 250 MB, do you consider that as a large build ?( i havent had chance to upload it yet )
and as figures in https://caniuse.com/#feat=wasm , seems that wasm is now fully supported on all devices.

If that’s the size of your build with everything in it, that’s huge! My entire project is about 190 MB, but most of that is assetbundles. The code is 3.6 MB and built-in assets are 2.0 MB. We just dynamically load our bundles as they are needed so that the initial load time is low.

iOS sucks! They “officially” support wasm, but that doesn’t mean it always works. iOS 12 worked with wasm, 13.0 and 13.1 broke wasm, and they “fixed” it with 13.2, but it still doesn’t load 100% of the time. I noticed a failure rate of ~10% when I was testing (not sure if it has gotten better since then, haven’t tested in a while). Asm seems to work 100% of the time, but is so slow, it’s unusable. What’s worse, with iOS 13, they started lying in the browser what device it is on iPads, so you can’t tell if it’s an iPad or a Mac to tell it to load wasm or asm.

1 Like

Thanks for your respond @ProtoTerminator

huge to load or huge for ram? i assumed that in worst case it would occupy something like 2GB of ram, besides my projects are kinda light for cpu, mainly consist of textures

I have been trying to make my webgl build work on ios mobile devices too, and it is really frustrating. Even on identical devices (say, 3 identical iPad Air 2), one will work and two got stuck at 90%. Sometimes it will depend on network speed, when I tried the ipads and iphones using the shop’s network, none will load. But if I connect them to my phone’s hotspot, all can load the app without any problem…

The build works great on desktop browsers and Android devices (interestingly, those devices can load fine though they are connected to the previouisly mentioned shop wifi network).

this 90% stuck stems from low ram issue, how large is your build ? wasm or asm.js ?
try it on more recent ios devices

My app is very small, the whole Build folder is only about 40MB. I am using the latest Unity version, so should be wasm.

I have tried on the latest ios devices (ipad, ipad air, iphone11, etc) and saw similar behavior. It is rather hit or miss. If network situation is good, then there is a higher chance of successfully loading the app. So strange…

idont know exactly but as i tested a 40mb wasm build on my iphone6(1gb ram) , it stuck on 90% too.
i would try asm.js tomorrow but , its weird cuz safari officially supports wasm,and i read some unity testing benchmarks on safari three years ago , idk maybe its just doesnt work with ios safari !
just remember that if your gonno build as asm.js you should identify allocated ram too like : PlayerSettings.WebGL.memorySize = 256;

Using the latest Unity version, no longer has the option to choose asm/wasm or to set memory size. Have tried on latest iOS models, still some can load and some cannot…

its not in UI anymore because its deprecated but you can change it via editor script

using UnityEditor;
using UnityEngine;

[InitializeOnLoad]
class asm
{
    static asm()
    {
        PlayerSettings.WebGL.linkerTarget = WebGLLinkerTarget.Asm;
        PlayerSettings.WebGL.memorySize = 512;
    }
}

just put this script under Assets/Editor/
btw just keep that in mind that amount of ram which is gonno be occupied by your app isn’t defined by the build size actually ,it may differs so much based on your content , you could track it by Unity profiler under ram section/total ram allocated
as i own an iphone6 i can’t recommend my statistics to you ,but ill post here when i’m done to have a comprehensive
data to be comparable.
Just for everyone who may read this post in future i’m gonno post the memory allocation limit on ios devices :

device: (crash amount/total amount/percentage of total)

  • iPad1: 127MB/256MB/49%
  • iPad2: 275MB/512MB/53%
  • iPad3: 645MB/1024MB/62%
  • iPad4: 585MB/1024MB/57% (iOS 8.1)
  • iPad Mini 1st Generation: 297MB/512MB/58%
  • iPad Mini retina: 696MB/1024MB/68% (iOS 7.1)
  • iPad Air: 697MB/1024MB/68%
  • iPad Air 2: 1383MB/2048MB/68% (iOS 10.2.1)
  • iPad Pro 9.7": 1395MB/1971MB/71% (iOS 10.0.2 (14A456))
  • iPad Pro 10.5”: 3057/4000/76% (iOS 11 beta4)
  • iPad Pro 12.9” (2015): 3058/3999/76% (iOS 11.2.1)
  • iPad Pro 12.9” (2017): 3057/3974/77% (iOS 11 beta4)
  • iPad Pro 11.0” (2018): 2858/3769/76% (iOS 12.1)
  • iPad Pro 12.9” (2018, 1TB): 4598/5650/81% (iOS 12.1)
  • iPad 10.2: 1844/2998/62% (iOS 13.2.3)
  • iPod touch 4th gen: 130MB/256MB/51% (iOS 6.1.1)
  • iPod touch 5th gen: 286MB/512MB/56% (iOS 7.0)
  • iPhone4: 325MB/512MB/63%
  • iPhone4s: 286MB/512MB/56%
  • iPhone5: 645MB/1024MB/62%
  • iPhone5s: 646MB/1024MB/63%
  • iPhone6: 645MB/1024MB/62% (iOS 8.x)
  • iPhone6+: 645MB/1024MB/62% (iOS 8.x)
  • iPhone6s: 1396MB/2048MB/68% (iOS 9.2)
  • iPhone6s+: 1392MB/2048MB/68% (iOS 10.2.1)
  • iPhoneSE: 1395MB/2048MB/69% (iOS 9.3)
  • iPhone7: 1395/2048MB/68% (iOS 10.2)
  • iPhone7+: 2040MB/3072MB/66% (iOS 10.2.1)
  • iPhone8: 1364/1990MB/70% (iOS 12.1)
  • iPhone X: 1392/2785/50% (iOS 11.2.1)
  • iPhone XS: 2040/3754/54% (iOS 12.1)
  • iPhone XS Max: 2039/3735/55% (iOS 12.1)
  • iPhone XR: 1792/2813/63% (iOS 12.1)
  • iPhone 11: 2068/3844/54% (iOS 13.1.3)
  • iPhone 11 Pro Max: 2067/3740/55% (iOS 13.2.3)

you could find the ongoing post here :
https://stackoverflow.com/questions/5887248/ios-app-maximum-memory-budget/15200855#15200855

  • please post your experiences with mentioning your ios device model/ios version/build size/build format here to help others
2 Likes

Seems not that related to the project size. When it can’t load, it can’t load and will stuck at 90%. Even for the latest model like the iPhone 11 Pro.

may you do the same thing as i did in the post below with newest ios device you have and share the results
(just add few more maps to test the limit)
https://discussions.unity.com/t/788831
for me it run with 3 4k maps.and stuck on 4.

First…thanks a lot for all the Infos…really strong and helpful stuff…
Unfortunately…this is seemingly not correct.

I deploy to IPads with iOS 13.3, 13.4, 13.5…it always responds with Application.isMobilePlatform = false (maybe because it’s not a Phone, but a Tablet?)

Anyway…so far, i haven’t found any solution to determine, whether i am running on iPad or Mac…because all the SystemInfo-Callbacks return crap as well (OS returns as MacOS, deviceType returns as Safari)

Any other recommendations to that?