TExcelTAD
Home › Unity with C# › Build Profiles, IL2CPP & Platforms

Build Profiles, IL2CPP & Platforms

A game nobody can install isn't finished. This lesson covers how Unity 6 builds players, the two scripting backends, code stripping, and the platform-specific steps for Android, iOS and the web.

Build Profiles

Unity 6 replaces the old Build Settings window with Build Profiles (File > Build Profiles):

  1. The Platforms list shows each installed platform (install more through Unity Hub modules). Select one and Switch Platform; Unity reimports assets for it, which can take a while on big projects.
  2. The Scene List says which scenes go into the build. Index 0 loads first.
  3. Right-click a platform and choose Copy to new profile to make named build profiles (Windows Demo, Android Release) with their own scenes, scripting defines and settings overrides.
  4. Player Settings (button in the window, or Project Settings > Player) hold the company and product name, icons, version, resolution, and per-platform options.
  5. Development Build adds debugging, the Profiler connection and on-screen error reporting. Never ship it.

Mono or IL2CPP

The Scripting Backend (Player Settings > Other Settings > Configuration) decides how your C# runs:

Mono IL2CPP
How JIT-compiles IL at runtime converts IL to C++ at build time, then compiles it natively (AOT)
Build time fast slow (a full C++ compile)
Runtime speed good usually faster
Platforms desktop, Android required for iOS, consoles and the web; recommended for Android (Google Play requires 64-bit, which needs IL2CPP)
Reverse engineering trivial (DLLs) harder (native code + metadata)

Use Mono for quick desktop iteration, IL2CPP for release builds. IL2CPP needs the IL2CPP module installed for the target in Unity Hub.

Managed code stripping

IL2CPP builds strip unused code to shrink the binary (Managed Stripping Level: Minimal, Low, Medium, High). The linker keeps only what it can see being used from the roots (scenes, scripts, assemblies). Code reached only through reflection is invisible to it, so it gets removed and fails at runtime, often only in release builds. JSON libraries creating types by reflection are the classic victim. The model runs the same reachability analysis:

csharp · try it yourself
using System;
using System.Collections.Generic;
using System.Linq;

// type -> types it references directly in code
var calls = new Dictionary<string, string[]>
{
    ["Boot"]            = new[] { "GameManager", "SaveSystem" },
    ["GameManager"]     = new[] { "Player", "EnemySpawner" },
    ["Player"]          = new[] { "Health" },
    ["EnemySpawner"]    = new[] { "Enemy" },
    ["Enemy"]           = new[] { "Health" },
    ["SaveSystem"]      = new[] { "JsonConvert" },     // deserializes SaveData by reflection only
    ["JsonConvert"]     = new string[0],
    ["SaveData"]        = new[] { "InventoryData" },
    ["InventoryData"]   = new string[0],
    ["DebugCheats"]     = new[] { "Player" },          // nothing references it
    ["Health"]          = new string[0],
};

foreach (var preserved in new[] { new string[0], new[] { "SaveData" } })
{
    var keep = Reachable(calls, new[] { "Boot" }.Concat(preserved));
    var stripped = calls.Keys.Where(t => !keep.Contains(t)).OrderBy(t => t);
    Console.WriteLine(preserved.Length == 0 ? "stripping without link.xml:" : "with SaveData preserved:");
    Console.WriteLine(
quot; kept: {string.Join(", ", keep.OrderBy(t => t))}"); Console.WriteLine(
quot; stripped: {string.Join(", ", stripped)}"); } static HashSet<string> Reachable(Dictionary<string, string[]> graph, IEnumerable<string> roots) { var seen = new HashSet<string>(); var stack = new Stack<string>(roots); while (stack.Count > 0) { var t = stack.Pop(); if (!seen.Add(t)) continue; foreach (var next in graph[t]) stack.Push(next); } return seen; }
stripping without link.xml:
  kept:     Boot, Enemy, EnemySpawner, GameManager, Health, JsonConvert, Player, SaveSystem
  stripped: DebugCheats, InventoryData, SaveData
with SaveData preserved:
  kept:     Boot, Enemy, EnemySpawner, GameManager, Health, InventoryData, JsonConvert, Player, SaveData, SaveSystem
  stripped: DebugCheats

Without help, SaveData vanishes and loading a save crashes in the release build only. Fix it with [UnityEngine.Scripting.Preserve] on the type, or a link.xml file in Assets/:

xml
<linker>
    <assembly fullname="Game.Data">
        <type fullname="Game.Data.SaveData" preserve="all" />
        <type fullname="Game.Data.InventoryData" preserve="all" />
    </assembly>
</linker>

Platform notes

Android

  1. Install Android Build Support (with OpenJDK and the Android SDK & NDK tools) in Unity Hub.
  2. Set the package name (com.studio.game), minimum and target API levels, IL2CPP and the ARM64 architecture.
  3. Create a keystore in Publishing Settings and keep it (and its passwords) backed up: you need the same signing key for every update unless you use Play App Signing.
  4. Tick Build App Bundle (Google Play) to produce an .aab for the store; .apk for side-loading and testing.

iOS

  1. Requires a Mac with Xcode. Unity builds an Xcode project, not an app.
  2. Set the bundle identifier and signing team (in Player Settings or in Xcode), then archive and upload from Xcode.

Web (the platform called WebGL in older versions)

  1. Builds to WebAssembly + JavaScript; must be served over HTTP(S) from a web server, not opened from disk.
  2. No threads for C# by default, limited memory, and compressed builds need the right server headers (Content-Encoding) or the Decompression Fallback option.
  3. Test on mobile browsers early if you target them.

Automating builds

csharp
// Assets/_Game/Editor/BuildScript.cs (in an editor-only assembly)
using UnityEditor;
using UnityEditor.Build.Reporting;
using UnityEngine;

public static class BuildScript
{
    public static void BuildWindows()
    {
        var options = new BuildPlayerOptions
        {
            scenes = new[] { "Assets/_Game/Scenes/Boot.unity", "Assets/_Game/Scenes/Level1.unity" },
            locationPathName = "Builds/Windows/CrystalRun.exe",
            target = BuildTarget.StandaloneWindows64,
            options = BuildOptions.None,
        };
        BuildReport report = BuildPipeline.BuildPlayer(options);
        Debug.Log(
quot;Build {report.summary.result}: {report.summary.totalSize / (1024 * 1024)} MB"); if (report.summary.result != BuildResult.Succeeded) EditorApplication.Exit(1); } }
bash
# CI: build without opening the editor UI (path to the editor depends on your install)
Unity -batchmode -quit -projectPath . -executeMethod BuildScript.BuildWindows -logFile -

GameCI (GitHub Actions) and Unity Build Automation run such builds in the cloud; check their current pricing and licence requirements. Always keep the build log, and read the Build Report (sizes per asset) when the build grows unexpectedly.

Test yourself

Which scripting backend is required for iOS builds?

  1. Mono
  2. IL2CPP
  3. Either
  4. .NET Native

A type only created through reflection disappears in an IL2CPP release build. What fixes it?

  1. Switch to Development Build
  2. Preserve it with [Preserve] or link.xml
  3. Rename the type
  4. Disable Burst

Where do you choose which scenes go into a Unity 6 build?

  1. Project Settings > Editor
  2. The Scene List of the build profile in File > Build Profiles
  3. Package Manager
  4. The Lighting window