Bad ideas, built well.

Silly games, taken way too seriously. Just hit play.

Quick tourney Bridge Babe begins in · 2 of 8 in

Daily Play · · Building games · 4 min read

My title screen was idling a 52 MB engine like a car in the driveway

Unity boots on page load. Click-to-load makes people wait after they commit. The third way: download quietly, start on the button.

Every winter my dad would go out at seven, start the car, and leave it running in the driveway "to warm up." Then he would come back inside, make toast, look for his keys, and argue with the radio. Forty minutes later we would leave. The car had been very warm for a very long time.

Unity, by default, is my dad.

When a Unity WebGL page loads, the template instantiates the game right away. Engine up, heap allocated, everything humming. On our builds that is a 52 MB heap and a running engine sitting behind the title screen, behind the menus, behind the player reading the instructions and wondering if this is the one with the frog. Nobody is in the car. The car is running anyway.

The two obvious answers are both wrong

There are two common ways to load a browser game, and I have opinions about both.

Option one: boot on page load. The Unity default. Great if the player hits Play the instant the page appears. Terrible for everyone else, which is most people. You pay the full memory and CPU cost of a running engine for a title screen that could have been a picture and a button.

Option two: click to load. The itch.io way. Show a button, do nothing until it is pressed, then download and start everything. Polite to the machine. Rude to the human. The player has just committed - they pressed Play, they want to play - and now they get a loading bar for the entire download. You have taken their enthusiasm and asked it to queue.

So one wastes the computer's time and the other wastes the player's. I have two kids. I know which one complains louder.

Download now, start later

What we do instead is split "loading" into the two things it actually is:

  1. Getting the bytes. The build files, which are big and slow to arrive.
  2. Starting the engine. Decompressing, compiling, allocating the heap, running the thing.

These do not have to happen at the same time. So we don't make them.

The title screen shows immediately. In the background, quietly, the page fetches the build files into the browser cache. It does not start the engine. It does not allocate the heap. It just puts the groceries in the fridge. When the player presses the button, Unity's loader asks for the same files, finds them already in the cache, and gets on with starting.

The car is not running. But it is parked by the door with the keys in it.

Here is the shape of it. A sketch, not our exact code:

const conn = navigator.connection;              // Safari: undefined
const phone = matchMedia('(pointer: coarse)').matches;
const slow = conn && (conn.saveData || /2g/.test(conn.effectiveType));

if (!phone && !slow) {
  buildUrls.forEach(u => fetch(u).catch(() => {}));  // warm cache, no boot
}
playButton.onclick = () => createUnityInstance(canvas, config);

That's most of it. The fetch results are thrown away on purpose. We don't want the bytes in JavaScript. We want them in the HTTP cache, where the loader will find them.

Where the waiting goes

Engine idling, nobody playingPlayer waiting after pressing

You can't make the wait vanish. You can stop making the person who just said yes sit through it. Not real data. Painfully accurate, though.

Who waits for the press anyway

Background downloading is lovely until it is someone's phone bill. So some players still get the old click-to-load behaviour, on purpose:

  • Phones. Anything with a coarse pointer waits for the button.
  • Save-Data. If the browser says the user asked to save data, we believe them.
  • Slow connections. If the browser reports 2g or slow-2g, prefetching a game would mostly just fight with the page for bandwidth.

The phone rule is the blunt one, and it is blunt because of Safari. Safari has no navigator.connection. None. Which means on an iPhone there is no way to ask whether you are on Wi-Fi or cellular. I would love to prefetch for the iPhone on home Wi-Fi and hold off for the iPhone on a train. I cannot tell them apart. So both wait.

Is that a shame for the Wi-Fi iPhone? A little. Is it better than quietly downloading a game over somebody's data plan because they glanced at our title screen? Yes, and it is not close. When you cannot know, pick the option that does not cost the stranger money.

The bits that will bite you

If you try this, a few things matter more than they look like they should.

  • Same URLs, exactly. The cache is keyed on the URL. If your prefetch asks for game.data and the loader asks for game.data?v=3, you have downloaded the game twice and taught the browser nothing.
  • Your cache headers have to allow it. If the server says don't store these files, the prefetch is just a very elaborate way of burning bandwidth. (I have written before about telling browsers to cache Unity files for a year. They listened. This is the one time that is exactly what you want.)
  • It is not instant. The press still has to decompress, compile and start the engine. What you have removed is the network wait, which on a big build is usually the long part. Don't promise "instant." Promise "noticeably less annoying," which is my life motto.
  • Don't parse it yourself. No reading the response, no stuffing it into memory, no clever blobs. The moment you hold the bytes in JavaScript you are back to paying for things nobody is using.

That last one is the whole point, really. The win is not "load faster." The win is "don't pay for anything until someone asks." A title screen should cost what a title screen costs. The engine starts when a human says go.

It is, I'll admit, a bit of a cache-and-carry approach. I'm sorry. I had to.

My dad, for the record, still warms the car up. I have stopped arguing. But I did fix it in the browser, where the car does what I tell it, and nobody runs a 52 MB engine in the driveway while they look for their keys.

unitywebglloadingperformancebrowser-cache