Recursive AI

Compile a C# game to WebAssembly with AOT

onegame is a free, open-source game engine for .NET from Recursive AI. MIT or Apache-2.0, no editor required.

Ahead-of-time (AOT) compilation happens at export. Build on Linux and play in a WebGPU browser.

Terminal
dotnet tool install -g RecursiveAI.OneGame.Cli

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

Unzip the example. In its folder, run:

Terminal
dotnet workload install wasm-tools
onegame export --target web --aot

Serve the exported folder with Python 3, then open http://[::1]:8000/ in a WebGPU browser:

Terminal
python3 -m http.server --bind ::1 --directory export/example-1.0.0-web

The export’s status line starts with:

Text
exported example 1.0.0 for the web (Release, AOT):

Unzip the example, then dotnet run.

The full program

The zip contains a game that displays “Ready” and pins RecursiveAI.OneGame.Sdk/2.5.3 in example.csproj. The SDK brings in the engine and its source generators. The example uses no external assets. The export was run with CLI 2.5.3.

XML
<Project Sdk="RecursiveAI.OneGame.Sdk/2.5.3">
  <PropertyGroup>
    <TargetFramework>net10.0</TargetFramework>
    <AssemblyName>Example</AssemblyName>
    <RootNamespace>Example</RootNamespace>
  </PropertyGroup>
</Project>
C#
using OneGame.Core;
using OneGame.Host;
using OneGame.UI;
using Example;
 
return GameApp.Run(new ExampleGame(), args.Length == 0
    ? ["--headless", "--gpu", "off", "--frames", "2", "--json"] : args);
 
namespace Example
{
public sealed class ExampleGame : GameDefinition
{
    public override string Name => "example";
    public override void RegisterEntities(EntityRegistry registry) { }
    public override void Setup(World world) { }
    public override void DefineScreens(ScreenSet screens) =>
        screens.Add("main", _ => Ui.Label("Ready"));
}
}

The default headless arguments are only for the desktop dotnet run check and don’t affect the web export. The desktop check runs two frames with no window or GPU and prints:

JSON
{"ok":true,"game":"example","headless":true,"frames":2,"tick":2,"hash":"8040b984a6630215","width":1280,"height":720}

Run the export command above for the browser build. If wasm-tools is missing, the CLI stops and prints the workload install command.

Variations

Run the included browser startup scenario against an AOT export. The CLI builds a fresh export with development tools and runs it in headless Chrome:

Terminal
onegame web test --aot

The input file is scenarios/start.yaml:

YAML
name: start
steps:
  - step: 2
Text
PASS start
1 passed, 0 failed (in headless Chrome)

The short answer

Install the .NET 10 wasm-tools workload and run onegame export --target web --aot in your OneGame project. The --aot flag is what changes compilation: the default web export runs the game through the .NET interpreter; this one compiles game code to WebAssembly during export. Both produce a browser site, not a standalone desktop executable.

How do you compile C# to WebAssembly ahead of time?

OneGame’s CLI creates a wrapper project with the .NET WebAssembly SDK. It compiles the game project’s own files and references and adds the page and scripts that boot the game in the browser. You don’t have to write a separate JavaScript entry point or clone the engine repository to run the example.

AOT uses the Release configuration even when you add --dev. The latter keeps development tools available; it does not request a Debug compilation. If you want a project to opt in to AOT by default, set OneGameWebAot to true in its project file instead of passing the flag each time. See the web export guide.

Which .NET workload do you need for WebAssembly AOT?

Install wasm-tools with the .NET SDK that will build the project. If you have several SDKs, check which one is selected when you’re in the game folder; installing a workload with another SDK won’t fix an error from the SDK actually doing the export. The default interpreted OneGame web export does not require that workload. The fact that an interpreted build works, then, doesn’t tell you whether AOT is installed.

How do you run a WebAssembly game in a browser?

Serve the whole output folder with a static web server, not by opening index.html as a local file. Keep index.html, the scripts and the _framework directory together, and serve .wasm as application/wasm. The page and scripts load the runtime and program from that folder. A browser still needs WebGPU to render the game; the AOT flag does not change the graphics backend.

Use the startup scenario to check that the browser can boot and step the AOT build. For a game of your own, add a scenario that touches the systems you depend on in Release, such as loading a scene or stepping the physics world. The desktop output in this example confirms that the project itself starts, but it doesn’t test the browser loader or graphics setup.

All onegame recipes