{
  "schemaVersion": "0.11.0",
  "canonical": "https://www.pystone.net/notes/unity-mono-il2cpp-dotnet-relationship/",
  "atlas": "https://www.pystone.net/?node=unity-mono-il2cpp-dotnet-relationship#knowledge-atlas",
  "markdown": "https://www.pystone.net/notes/unity-mono-il2cpp-dotnet-relationship.md",
  "context": "https://www.pystone.net/notes/unity-mono-il2cpp-dotnet-relationship.context.json",
  "knowledgeVersion": "224c990773de.5fa8af6e39fa",
  "build": {
    "siteCommit": "224c990773de166d23a886306577dd90379529ce",
    "notesCommit": "5fa8af6e39fa3891d1b9b4832bfa6c4e0ecaaf0a",
    "builtAt": "1970-01-01T00:00:00.000Z",
    "version": "224c990773de.5fa8af6e39fa"
  },
  "id": "note:unity-mono-il2cpp-dotnet-relationship",
  "slug": "unity-mono-il2cpp-dotnet-relationship",
  "title": "Unity、Mono、IL2CPP 与 .NET 的关系",
  "type": "note",
  "visibility": "public",
  "idStability": "rename-stable",
  "author": {
    "name": "Perrin Yong",
    "profile": "https://www.pystone.net/profile/"
  },
  "publisher": {
    "name": "Perrin Yong",
    "profile": "https://www.pystone.net/profile/"
  },
  "aliases": [],
  "summary": "Unity、Mono、IL2CPP 与 .NET 的关系",
  "contentRole": "unspecified",
  "isMoc": false,
  "mocRecognition": "none",
  "generated": false,
  "attribution": "unspecified",
  "domain": "10-计算机、信息技术与工程",
  "tags": [],
  "mocs": [],
  "contentHash": "11d6d41f82cb7ae9807bbc5e34a152db04a26e39bcccfb209df32d5b1a294695",
  "assets": [
    {
      "reference": "assets/Unity-Mono与NET-关系-01.png",
      "url": "/media/923654293893a7cf78a3.png",
      "mediaType": "image/png",
      "contentHash": "923654293893a7cf78a35b43c8e3552f2ddabdf80dd9627c1b067f1d656ce011",
      "byteLength": 74883,
      "width": 480,
      "height": 554
    },
    {
      "reference": "assets/Unity-Mono与NET-关系-02.png",
      "url": "/media/de4dba67a1bf3a63a36b.png",
      "mediaType": "image/png",
      "contentHash": "de4dba67a1bf3a63a36bb4250ea900d815d4a223eb71feadce4f888b64cd7890",
      "byteLength": 22369,
      "width": 603,
      "height": 446
    },
    {
      "reference": "assets/Unity-Mono与NET-关系-03.png",
      "url": "/media/bad4735cbcdbbba89933.png",
      "mediaType": "image/png",
      "contentHash": "bad4735cbcdbbba89933e9a80dee1395e6b1e94c5cf04843c070570ba1d3cc43",
      "byteLength": 36596,
      "width": 917,
      "height": 496
    }
  ],
  "headings": [
    {
      "depth": 1,
      "text": "Unity、Mono、IL2CPP 与 .NET 的关系",
      "anchor": "unitymonoil2cpp-与-net-的关系",
      "citation": "https://www.pystone.net/notes/unity-mono-il2cpp-dotnet-relationship/#unitymonoil2cpp-%E4%B8%8E-net-%E7%9A%84%E5%85%B3%E7%B3%BB"
    },
    {
      "depth": 2,
      "text": "先区分四个概念",
      "anchor": "先区分四个概念",
      "citation": "https://www.pystone.net/notes/unity-mono-il2cpp-dotnet-relationship/#%E5%85%88%E5%8C%BA%E5%88%86%E5%9B%9B%E4%B8%AA%E6%A6%82%E5%BF%B5"
    },
    {
      "depth": 2,
      "text": "编译与运行路径",
      "anchor": "编译与运行路径",
      "citation": "https://www.pystone.net/notes/unity-mono-il2cpp-dotnet-relationship/#%E7%BC%96%E8%AF%91%E4%B8%8E%E8%BF%90%E8%A1%8C%E8%B7%AF%E5%BE%84"
    },
    {
      "depth": 3,
      "text": "Mono 后端",
      "anchor": "mono-后端",
      "citation": "https://www.pystone.net/notes/unity-mono-il2cpp-dotnet-relationship/#mono-%E5%90%8E%E7%AB%AF"
    },
    {
      "depth": 3,
      "text": "IL2CPP 后端",
      "anchor": "il2cpp-后端",
      "citation": "https://www.pystone.net/notes/unity-mono-il2cpp-dotnet-relationship/#il2cpp-%E5%90%8E%E7%AB%AF"
    },
    {
      "depth": 2,
      "text": "三张历史示意图",
      "anchor": "三张历史示意图",
      "citation": "https://www.pystone.net/notes/unity-mono-il2cpp-dotnet-relationship/#%E4%B8%89%E5%BC%A0%E5%8E%86%E5%8F%B2%E7%A4%BA%E6%84%8F%E5%9B%BE"
    },
    {
      "depth": 2,
      "text": "实践中的判断顺序",
      "anchor": "实践中的判断顺序",
      "citation": "https://www.pystone.net/notes/unity-mono-il2cpp-dotnet-relationship/#%E5%AE%9E%E8%B7%B5%E4%B8%AD%E7%9A%84%E5%88%A4%E6%96%AD%E9%A1%BA%E5%BA%8F"
    },
    {
      "depth": 2,
      "text": "参考资料",
      "anchor": "参考资料",
      "citation": "https://www.pystone.net/notes/unity-mono-il2cpp-dotnet-relationship/#%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99"
    }
  ],
  "claims": [],
  "outgoing": [],
  "incoming": [
    {
      "id": "note:game-graphics-and-runtime",
      "title": "游戏图形与运行时",
      "url": "https://www.pystone.net/notes/game-graphics-and-runtime/",
      "atlas": "https://www.pystone.net/?node=game-graphics-and-runtime#knowledge-atlas",
      "label": "游戏图形与运行时",
      "origin": "explicit",
      "humanReviewed": true,
      "context": "Unity中的“Unity、Mono与.NET的关系”导航项",
      "citation": "https://www.pystone.net/notes/game-graphics-and-runtime/#unity"
    }
  ],
  "contentMarkdown": "# Unity、Mono、IL2CPP 与 .NET 的关系\n\n> [!note] 更新说明\n> 原笔记把 Unity 简化成“由 Mono 提供 C# 运行环境”。这对部分历史版本成立，但现代 Unity 还可使用 IL2CPP；可用的 .NET API 兼容级别也不等同于直接运行桌面 .NET SDK。\n\n## 先区分四个概念\n\n- **C#**：编程语言。\n- **.NET 标准库/API 兼容级别**：代码可调用的一组类型和接口约定。\n- **Mono**：开源 .NET 运行时实现之一。Unity 的 Mono 脚本后端通常把 C# 编译为 CIL，再由运行时执行，并提供垃圾回收等能力。\n- **IL2CPP**：Unity 的 AOT 脚本后端，把托管程序集中的 CIL 转换为 C++，再由目标平台原生编译器生成二进制；运行时仍需要 Unity 提供的托管语义、GC 与互操作支持。\n\n## 编译与运行路径\n\n### Mono 后端\n\n```text\nC# 源码 → C# 编译器 → CIL 程序集 → Mono/Unity 运行时执行\n```\n\nMono 后端通常便于快速迭代和调试，但具体 JIT/AOT 能力受目标平台限制。\n\n### IL2CPP 后端\n\n```text\nC# 源码 → CIL 程序集 → IL2CPP 转换为 C++ → 平台编译器 → 原生程序\n```\n\nIL2CPP 不是把任意 C# 语句直接“翻译成等价手写 C++”，而是 Unity 的构建链和运行时实现方案。AOT 环境会影响反射、泛型实例化、动态代码生成与代码裁剪，需要按 Unity 文档配置。\n\n## 三张历史示意图\n\n以下图片来自原笔记，用于理解早期 .NET/Mono 关系；其中版本与产品边界可能已过时。\n\n![早期 .NET 编译运行示意](assets/Unity-Mono与NET-关系-01.png)\n\n![Unity 与 Mono 的历史关系示意](assets/Unity-Mono与NET-关系-02.png)\n\n![CLI 与运行时历史示意](assets/Unity-Mono与NET-关系-03.png)\n\n## 实践中的判断顺序\n\n1. 确认 Unity 版本、目标平台和当前脚本后端。\n2. 确认 API Compatibility Level，而不是只看本机安装了哪个 .NET SDK。\n3. 检查第三方程序集是否支持 AOT、代码裁剪与目标 CPU 架构。\n4. 遇到内存问题时区分托管对象、Unity Native Object、原生插件和系统/驱动分配。\n5. 发布前分别在 Editor、Development Build 与真实发布构建验证，不能只依赖编辑器行为。\n\n## 参考资料\n\n- [Unity Manual：Scripting backends](https://docs.unity3d.com/ja/current/Manual/scripting-backends-intro.html)\n- [Unity Manual：IL2CPP overview](https://docs.unity3d.com/cn/6000.0/Manual/scripting-backends-il2cpp.html)\n- [Unity Manual：Managed memory introduction](https://docs.unity3d.com/jp/current/Manual/performance-managed-memory-introduction.html)\n"
}
