Skip to content
Random game

Unity WebGL games

Last updated 2026-11-19

This guide is for Unity 2021.3 or newer, Unity 6 included, with the Web (WebGL) build target. You add three things: a WebGL template with the SDK script, a .jslib plugin that calls the SDK, and a C# class your game calls.

1. A WebGL template

  1. Copy the Default template from Unity’s install folder (PlaybackEngines/WebGLSupport/BuildTools/WebGLTemplates/Default) to Assets/WebGLTemplates/FreeGames.
  2. In Assets/WebGLTemplates/FreeGames/index.html, add the SDK to the <head>:
<script src="https://games.foony.com/sdk/v1.js"></script>
  1. Find the createUnityInstance(...) call and report its progress:
const sdk = window.FreeGamesSdk;
sdk.init();
sdk.loadingStart();
createUnityInstance(canvas, config, (progress) => {
  sdk.loadingProgress(progress);
}).then((unityInstance) => {
  sdk.loadingFinished();
});
  1. Pick the template in Player Settings, Resolution and Presentation, WebGL Template.

Make the canvas fill the page. In the template’s CSS, give #unity-container and #unity-canvas a width and height of 100%, and remove the footer.

2. The .jslib plugin

Save this as Assets/Plugins/WebGL/FreeGames.jslib:

mergeInto(LibraryManager.library, {
  FG_GameplayStart: function () {
    window.FreeGamesSdk.gameplayStart();
  },
  FG_GameplayStop: function () {
    window.FreeGamesSdk.gameplayStop();
  },
  FG_GameEnd: function (resultPtr) {
    window.FreeGamesSdk.gameEnd(UTF8ToString(resultPtr));
  },
  FG_CommercialBreak: function () {
    window.FreeGamesSdk.commercialBreak().then(function () {
      SendMessage('FreeGames', 'OnBreakDone');
    });
  },
  FG_RewardedBreak: function () {
    window.FreeGamesSdk.rewardedBreak().then(function (result) {
      SendMessage('FreeGames', 'OnRewardedDone', result.rewarded ? 'yes' : 'no');
    });
  },
});

SendMessage calls a method on the GameObject named FreeGames, so step 3 makes one.

3. The C# class

Add this to a GameObject named FreeGames in your first scene:

using System;
using System.Runtime.InteropServices;
using UnityEngine;

public class FreeGames : MonoBehaviour
{
#if UNITY_WEBGL && !UNITY_EDITOR
    [DllImport("__Internal")] private static extern void FG_GameplayStart();
    [DllImport("__Internal")] private static extern void FG_GameplayStop();
    [DllImport("__Internal")] private static extern void FG_GameEnd(string result);
    [DllImport("__Internal")] private static extern void FG_CommercialBreak();
    [DllImport("__Internal")] private static extern void FG_RewardedBreak();
#else
    private static void FG_GameplayStart() {}
    private static void FG_GameplayStop() {}
    private static void FG_GameEnd(string result) {}
    private static void FG_CommercialBreak() { Instance.OnBreakDone(); }
    private static void FG_RewardedBreak() { Instance.OnRewardedDone("no"); }
#endif

    public static FreeGames Instance;
    private Action onBreakDone;
    private Action<bool> onRewardedDone;

    private void Awake()
    {
        Instance = this;
        DontDestroyOnLoad(gameObject);
    }

    public static void GameplayStart() { FG_GameplayStart(); }
    public static void GameplayStop() { FG_GameplayStop(); }
    public static void GameEnd(string result) { FG_GameEnd(result); }

    public static void CommercialBreak(Action done)
    {
        Instance.onBreakDone = done;
        AudioListener.pause = true;
        FG_CommercialBreak();
    }

    public static void RewardedBreak(Action<bool> done)
    {
        Instance.onRewardedDone = done;
        AudioListener.pause = true;
        FG_RewardedBreak();
    }

    private void OnBreakDone()
    {
        AudioListener.pause = false;
        onBreakDone?.Invoke();
    }

    private void OnRewardedDone(string rewarded)
    {
        AudioListener.pause = false;
        onRewardedDone?.Invoke(rewarded == "yes");
    }
}

In the editor the calls do nothing and breaks finish at once, so you can play in Play mode.

4. Call it from your game

public void StartLevel()
{
    FreeGames.GameplayStart();
    level.Begin();
}

public void OnPlayerDied()
{
    FreeGames.GameplayStop();
    FreeGames.GameEnd("loss");
    gameOverScreen.SetActive(true);
}

public void OnPlayAgainPressed()
{
    FreeGames.CommercialBreak(() => StartLevel());
}

public void OnWatchAdPressed()
{
    FreeGames.RewardedBreak((rewarded) =>
    {
        if (rewarded)
        {
            lives++;
        }
    });
}

Build settings

  • Compression Format: Brotli. Files that end in .br are sent with the right Content-Encoding, so they load at full speed. Turn Decompression Fallback off.
  • Code stripping: High, to make the build smaller. Test after you change it.
  • Memory. Peak JavaScript memory MUST stay under 512 MB. Keep the WebGL memory size as low as your game allows.
  • First scene. Keep it small, and load the rest with Addressables or asset bundles from inside your build. The initial download MUST be 20 MB or less.
  • Phones. Unity WebGL runs on phones, but test in the Inspector’s phone mode before you say your game supports them.