# Unity ML-Agents

**Unity ML-Agents**（[Unity](https://www.edgepedia.org/zh/unity-game-engine) 机器学习智能体工具包）是 [Unity Technologies](https://www.edgepedia.org/zh/unity-technologies) 的一个开源项目，它将用 Unity 引擎构建的游戏和仿真转化为环境，用于通过深度强化学习和模仿学习训练智能体。<sup>[1](https://github.com/Unity-Technologies/ml-agents)</sup> 它附带了最先进训练算法的 PyTorch 实现，使游戏开发者和爱好者能够为 2D、3D 和 VR/AR 游戏训练智能体行为，同时研究人员可以通过底层 API 从 Python 驱动 Unity 场景。<sup>[1](https://github.com/Unity-Technologies/ml-agents)</sup> Unity 于 2017 年 9 月 19 日将该工具包作为公开测试版发布。<sup>[2](https://unity.com/blog/engine-platform/introducing-unity-machine-learning-agents)</sup>

| 关键事实 | 详情 |
|---|---|
| 用途 | 将 Unity 场景转化为用于深度强化学习和模仿学习的学习环境<sup>[1](https://github.com/Unity-Technologies/ml-agents)</sup> |
| 首次发布 | 2017 年 9 月 19 日宣布公开测试版<sup>[2](https://unity.com/blog/engine-platform/introducing-unity-machine-learning-agents)</sup> |
| 训练算法 | 强化学习使用 PPO、SAC、MA-POCA 和自对弈；模仿学习使用 BC 和 GAIL<sup>[1](https://github.com/Unity-Technologies/ml-agents)</sup> |
| 组件 | Unity C# 包 `com.unity.ml-agents`；Python 包 `mlagents`（训练器）和 `mlagents_envs`（通信 API）<sup>[3](https://docs.unity.cn/Packages/com.unity.ml-agents%404.0/manual/Get-Started.html)</sup> |
| 示例环境 | 附带 17 个以上示例 Unity 环境<sup>[1](https://github.com/Unity-Technologies/ml-agents)</sup> |
| 仿真速度 | 训练期间游戏物理可加速至实时的 100 倍<sup>[4](https://docs.unity.cn/Packages/com.unity.ml-agents%401.0/manual/index.html)</sup> |
| 推理 | 训练好的模型通过 Unity 推理引擎（Sentis）在所有 Unity 运行时平台上运行<sup>[5](https://github.com/Unity-Technologies/ml-agents/blob/develop/com.unity.ml-agents/Documentation~/ML-Agents-Overview.md)</sup> |
| 社区规模 | GitHub 上约 19.7k 星标和 4.5k 复刻<sup>[1](https://github.com/Unity-Technologies/ml-agents)</sup> |

## 架构

该工具包分为 Unity 侧和 Python 侧。Unity 包 `com.unity.ml-agents` 包含集成到 Unity 项目中的 C# SDK；Python 包 `mlagents` 包含训练行为的机器学习算法，`mlagents_envs` 提供 Python 与 Unity 场景交换数据的 API，其中 `mlagents` 依赖于 `mlagents_envs`。<sup>[3](https://docs.unity.cn/Packages/com.unity.ml-agents%404.0/manual/Get-Started.html)</sup> 一个示例环境仓库展示了该工具包的功能。<sup>[3](https://docs.unity.cn/Packages/com.unity.ml-agents%404.0/manual/Get-Started.html)</sup>

Python 与 Unity 之间的通信通过 gRPC 协议使用 protobuf 消息进行。<sup>[6](https://arxiv.org/pdf/1809.02627)</sup> `mlagents_envs` 包公开了三个 Python API：单智能体 Gym API、类似 gym 的多智能体 [PettingZoo](https://www.edgepedia.org/zh/pettingzoo) API，以及供 `mlagents` 中训练器实现使用的底层 API（LLAPI）。<sup>[7](https://unity-technologies.github.io/ml-agents/ml-agents-envs/)</sup> Gym 和 PettingZoo 接口的封装器让研究人员能够将 Unity 环境接入围绕这些标准构建的现有训练系统。<sup>[5](https://github.com/Unity-Technologies/ml-agents/blob/develop/com.unity.ml-agents/Documentation~/ML-Agents-Overview.md)</sup>

## 设计智能体

ML-Agents 中的智能体是一个观察环境、决定动作并执行动作的实体。在 Unity 中，智能体通过扩展 `Agent` 类来创建；智能体将观察结果传递给其策略（Policy），策略返回一个动作，智能体代码必须执行该动作。<sup>[8](https://unity-technologies.github.io/ml-agents/Learning-Environment-Design-Agents/)</sup> 策略的行为取决于智能体的行为参数（Behavior Parameters）：设置为 `Heuristic Only` 时，智能体使用其 `Heuristic()` 方法进行手动控制或自定义逻辑；而使用模型文件则使策略使用训练好的神经网络。<sup>[8](https://unity-technologies.github.io/ml-agents/Learning-Environment-Design-Agents/)</sup>

对于强化学习，智能体必须在每次执行动作时计算奖励；PPO 算法优化选择以最大化随时间的累积奖励。奖励通过 `AddReward()` 或 `SetReward()` 方法分配，决策之间分配的奖励应保持在 [-1, 1] 范围内，因为超出此范围的值可能导致训练不稳定。<sup>[8](https://unity-technologies.github.io/ml-agents/Learning-Environment-Design-Agents/)</sup> 在使用训练好的模型进行推理或模仿学习期间不使用奖励。<sup>[8](https://unity-technologies.github.io/ml-agents/Learning-Environment-Design-Agents/)</sup> 该 API 提供内置传感器，用于视觉观察（CameraSensor）、射线投射观察（RayPerceptionSensor）和任意数据（VectorSensor）。<sup>[9](https://docs.unity.cn/Packages/com.unity.ml-agents%402.2/api/Unity.MLAgents.Agent.html)</sup>

## 训练场景与功能

该工具包通过其深度强化学习算法支持单智能体、多智能体合作和多智能体竞争训练，并支持通过模仿学习算法 BC（行为克隆）和 GAIL（生成对抗模仿学习）从演示中学习。<sup>[1](https://github.com/Unity-Technologies/ml-agents)</sup> [课程学习](https://www.edgepedia.org/zh/curriculum-learning)场景——即环境难度随训练进展而调整——可以直接定义，环境参数可以在训练期间随机采样（环境参数随机化，基于域随机化），以产生能够泛化到未见变体的智能体。<sup>[1](https://github.com/Unity-Technologies/ml-agents)</sup><sup> • </sup><sup>[5](https://github.com/Unity-Technologies/ml-agents/blob/develop/com.unity.ml-agents/Documentation~/ML-Agents-Overview.md)</sup> 训练可以使用多个并发的 Unity 环境实例，智能体可以按需做出决策，而不是以固定间隔决策。<sup>[1](https://github.com/Unity-Technologies/ml-agents)</sup>

对于视觉观察，ML-Agents 允许每个智能体使用多个摄像头，使智能体能够整合多个视觉流，并提供三种卷积网络编码器：简单的两层卷积编码器、Mnih 等人提出的三层架构，以及更大的 IMPALA Resnet，其具有三个堆叠层，每层包含两个残差块。<sup>[5](https://github.com/Unity-Technologies/ml-agents/blob/develop/com.unity.ml-agents/Documentation~/ML-Agents-Overview.md)</sup> 卷积核大小规定了各编码器所需的最小观察维度：`simple` 为 20x20，`nature_cnn` 为 36x36，`resnet` 为 15x15，`match3` 为 5x5。<sup>[10](https://unity-technologies.github.io/ml-agents/Training-Configuration-File/)</sup> 可变长度观察通过注意力网络处理，部分可观测性可以通过 LSTM 记忆来解决。<sup>[5](https://github.com/Unity-Technologies/ml-agents/blob/develop/com.unity.ml-agents/Documentation~/ML-Agents-Overview.md)</sup>

训练进度通过 TensorBoard 监控，训练器行为通过训练配置文件进行配置。<sup>[11](https://docs.unity.cn/Packages/com.unity.ml-agents%404.0/manual/Training.html)</sup> 训练结束后，每个智能体学到的策略被导出为模型文件并嵌入 Unity 应用程序；推理通过 Unity 推理引擎（Sentis）运行，支持所有 Unity 运行时平台。<sup>[5](https://github.com/Unity-Technologies/ml-agents/blob/develop/com.unity.ml-agents/Documentation~/ML-Agents-Overview.md)</sup>

## 性能与限制

训练需要 Unity 编辑器或 Windows、macOS 和 Linux 上使用 Mono 脚本后端的独立构建；训练不支持 IL2CPP 后端，当训练不受支持时环境会回退到推理模式。<sup>[4](https://docs.unity.cn/Packages/com.unity.ml-agents%401.0/manual/index.html)</sup> 游戏物理最多可加速至实时的 100 倍，而 Academy——驱动环境步进的单例——通过 `FixedUpdate()` 而非 `Update()` 推进，因此实现在 `Update()` 中的游戏逻辑可能与智能体决策失去同步。<sup>[4](https://docs.unity.cn/Packages/com.unity.ml-agents%401.0/manual/index.html)</sup> Python 接口通过本地主机端口交换数据，因此多个 `UnityEnvironment` 实例可能在端口上冲突，且通信不安全。<sup>[7](https://unity-technologies.github.io/ml-agents/ml-agents-envs/)</sup>

一项对强化学习工具包的比较研究发现，Unity ML-Agents 对十一项技术标准中的大部分提供了完整支持，在多任务学习和学习策略方面存在局限，而 [OpenAI Gym](https://www.edgepedia.org/zh/openai-gym) 及相关工具缺乏视觉观察支持和完整的多智能体支持。<sup>[12](https://www.scirp.org/pdf/jsea_2022123015380508.pdf)</sup> 在基准比较中，OpenAI Baselines 的 PPO2 得分比 Unity ML-Agents PPO 高 50%，但训练速度慢 14%，而 OpenAI Baselines 的 ACKTR 和 A2C 算法训练速度快 33%。<sup>[12](https://www.scirp.org/pdf/jsea_2022123015380508.pdf)</sup>

## 研究用途

该工具包是论文《Unity：智能体通用平台》（arXiv，2018 年 9 月 7 日首次发布，作者为 Arthur Juliani 等 11 位作者）的主题，该论文主张现代游戏引擎适合作为智能体研究的通用平台，并综述了 Unity 和 ML-Agents 所促成的工作。<sup>[6](https://arxiv.org/pdf/1809.02627)</sup> 基于该工具包，Marathon Environments 是一套在 Unity 引擎上进行运动控制的开源连续控制基准，在 AAAI-2019 人工智能游戏与仿真研讨会上展示，演示了从动作捕捉数据学习行走、奔跑和后空翻等任务。<sup>[13](https://arxiv.org/abs/1902.09097)</sup> 第三方框架也扩展了该工具包，例如 ROS2MLAgents，它将 ML-Agents 与 ROS2 桥接，将多机器人决策策略从仿真迁移到真实机器人。<sup>[14](https://icra2024-madgames.f1tenth.org/papers/rogers.pdf)</sup>

## 版本

Unity 包以 `com.unity.ml-agents` 分发；4.1.0 版本为 Unity 编辑器 6000.0 发布，4.0.x 和 3.0.0 也为该编辑器版本发布。<sup>[15](https://docs.unity.com/en-us/engine/6000.0/manual/packages-list/packages-all/pack-safe/com-unity-ml-agents)</sup> 各包的版本管理是解耦的：GitHub 发布使用整数编号，而 Unity 包和 Python 包（`mlagents`、`mlagents_envs`、`gym-unity`）带有各自的版本号，例如在第一个整数版本中 com.unity.ml-agents 1.0.0 与 ml-agents 0.16.0 并存。<sup>[16](https://unity-technologies.github.io/ml-agents/Versioning/)</sup> [文档](https://www.edgepedia.org/zh/documentation)以英文维护，并有中文和韩文的社区翻译，Docker、Anaconda 以及 AWS 和 Azure 云训练的旧指南作为已弃用文档保留。<sup>[17](https://unity-technologies.github.io/ml-agents/ML-Agents-Toolkit-Documentation/)</sup>

## References

1. [Unity-Technologies/ml-agents, GitHub repository](https://github.com/Unity-Technologies/ml-agents)
2. [Introducing Unity Machine Learning Agents Toolkit, Unity blog (September 19, 2017)](https://unity.com/blog/engine-platform/introducing-unity-machine-learning-agents)
3. [Get started, ML Agents 4.0.0 documentation](https://docs.unity.cn/Packages/com.unity.ml-agents%404.0/manual/Get-Started.html)
4. [About ML-Agents package, ML Agents 1.0 documentation](https://docs.unity.cn/Packages/com.unity.ml-agents%401.0/manual/index.html)
5. [ML-Agents Overview, ml-agents repository documentation](https://github.com/Unity-Technologies/ml-agents/blob/develop/com.unity.ml-agents/Documentation~/ML-Agents-Overview.md)
6. [Juliani, A., et al. Unity: A General Platform for Intelligent Agents, arXiv:1809.02627](https://arxiv.org/pdf/1809.02627)
7. [Unity ML-Agents Python Interface, ML-Agents Toolkit documentation](https://unity-technologies.github.io/ml-agents/ml-agents-envs/)
8. [Designing Agents, Unity ML-Agents Toolkit documentation](https://unity-technologies.github.io/ml-agents/Learning-Environment-Design-Agents/)
9. [Class Agent, ML Agents 2.2.1-exp.1 API reference](https://docs.unity.cn/Packages/com.unity.ml-agents%402.2/api/Unity.MLAgents.Agent.html)
10. [Training Configuration File, Unity ML-Agents Toolkit documentation](https://unity-technologies.github.io/ml-agents/Training-Configuration-File/)
11. [Training, ML Agents 4.0.0 documentation](https://docs.unity.cn/Packages/com.unity.ml-agents%404.0/manual/Training.html)
12. [Mekni, M., et al. Reinforcement Learning Toolkits for Gaming: A Comparative Qualitative Analysis, Journal of Software Engineering and Applications (2022)](https://www.scirp.org/pdf/jsea_2022123015380508.pdf)
13. [Booth, J., Booth, J. Marathon Environments: Multi-Agent Continuous Control Benchmarks in a Modern Video Game Engine, arXiv:1902.09097](https://arxiv.org/abs/1902.09097)
14. [ROS2MLAgents: A Framework for Enabling Seamless Transfer of Multi-Robot Decision-Making from Simulation to Reality](https://icra2024-madgames.f1tenth.org/papers/rogers.pdf)
15. [ML Agents package, Unity Editor 6000.0 documentation](https://docs.unity.com/en-us/engine/6000.0/manual/packages-list/packages-all/pack-safe/com-unity-ml-agents)
16. [ML-Agents Versioning, Unity ML-Agents Toolkit documentation](https://unity-technologies.github.io/ml-agents/Versioning/)
17. [Unity ML-Agents Toolkit Documentation](https://unity-technologies.github.io/ml-agents/ML-Agents-Toolkit-Documentation/)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 现代人工智能：基础模型、生成式人工智能与人工智能产业 › Foundation-model methods and training › Reinforcement learning and world models

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/unity-ml-agents

English original: https://www.edgepedia.org/unity-ml-agents
