# 实体组件系统

实体组件系统（ECS）是一种软件架构模式，主要用于游戏引擎和大规模仿真，它将程序分解为三类元素：实体，即区分不同事物的标识符；组件，即附加在实体上的纯数据记录；以及系统，即读写这些数据的逻辑。它是一种面向数据的替代方案，用以取代面向对象架构——后者将数据与行为封装在一起，而前者将二者分离。<sup>[1](https://github.com/SanderMertens/ecs-faq)</sup><sup> • </sup><sup>[2](https://docs.unity.cn/Packages/com.unity.entities%401.3/manual/concepts-intro.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 核心划分 | 身份（实体）、状态（组件）与行为（系统）相互分离，以支持面向数据的执行<sup>[3](https://doi.org/10.1145/3828534.3828535)</sup> |
| 实体 | 一个不含代码的唯一标识符，充当与各个组件相关联的ID；与GameObject不同，实体不包含任何逻辑<sup>[2](https://docs.unity.cn/Packages/com.unity.entities%401.3/manual/concepts-intro.html)</sup> |
| 组合优于继承 | 继承在OOP中是一等公民，组合在ECS中是一等公民；ECS鼓励暴露的朴素数据组件，而OOP鼓励封装<sup>[1](https://github.com/SanderMertens/ecs-faq)</sup> |
| 原型存储 | 组件集合相同的实体被归入原型，即稠密的列式表，每一列是一种组件类型，每一行是一个实体，从而提升缓存效率<sup>[4](https://boyang.cs.uwm.edu/publication/sac2026_ECS.pdf)</sup> |
| 实测性能 | 在Unity 6的生成-销毁工作负载中，ECS在约10,000个实体以内可维持60帧/秒的帧预算，而OOP为6,000个，ECS可承载的实体数量为OOP的1.67倍<sup>[5](https://papers.ssrn.com/sol3/papers.cfm?abstract_id=6854635)</sup> |
| 引擎采用 | Unity DOTS、Bevy、Flecs以及虚幻引擎的MassEntity均为基于原型的ECS框架<sup>[4](https://boyang.cs.uwm.edu/publication/sac2026_ECS.pdf)</sup><sup> • </sup><sup>[6](https://dev.epicgames.com/documentation/unreal-engine/overview-of-mass-entity-in-unreal-engine?lang=en-US)</sup> |
| 早期范例 | 《地牢围攻》（2002）使用了一套类似ECS的“数据驱动游戏对象系统”，当时该术语尚未被提出<sup>[7](https://trepo.tuni.fi/bitstream/handle/123456789/27593/H%C3%A4rk%C3%B6nen.pdf?sequence=4)</sup> |

## 三大支柱

实体是一个唯一标识符，一个自身不含任何行为的轻量句柄。在Unity的Entities包中，实体被描述为GameObject的轻量非托管替代品，一个与持有其数据的组件相关联的ID。<sup>[2](https://docs.unity.cn/Packages/com.unity.entities%401.3/manual/concepts-intro.html)</sup> 在虚幻的MassEntity框架中，实体是一组片段的集合实例，片段是诸如Transform、Velocity或LOD Index之类的原子数据块，而片段与实体都是明确仅含数据、不含逻辑的元素。<sup>[6](https://dev.epicgames.com/documentation/unreal-engine/overview-of-mass-entity-in-unreal-engine?lang=en-US)</sup>

组件存储状态。组件是一条纯数据记录，一个实体具备何种能力，取决于它在某一时刻携带哪些组件；组合可以在运行时改变，因此实体可以在程序运行过程中获得一个Velocity片段或失去一个[Paralysis](https://www.edgepedia.org/zh/paralysis)组件。<sup>[6](https://dev.epicgames.com/documentation/unreal-engine/overview-of-mass-entity-in-unreal-engine?lang=en-US)</sup><sup> • </sup><sup>[1](https://github.com/SanderMertens/ecs-faq)</sup> 完全不携带任何数据的简单组件——在MassEntity中称为标签——以其存在或不存在本身作为数据。<sup>[6](https://dev.epicgames.com/documentation/unreal-engine/overview-of-mass-entity-in-unreal-engine?lang=en-US)</sup>

系统提供逻辑，将组件数据从当前状态变换到下一状态，例如根据速度乘以经过的时间来更新所有移动实体的位置。<sup>[8](https://docs.unity.cn/Packages/com.unity.entities%400.8/manual/ecs_systems)</sup> 系统查询具有特定组件集合的实体并对其进行迭代：一个外部物理系统遍历所有Velocity组件并更新Position，而一个输入系统遍历所有KeyboardControlled组件并更新Velocity。<sup>[9](https://softwareengineering.stackexchange.com/questions/211708/is-the-entity-component-system-architecture-object-oriented-by-definition?rq=1)</sup> 由于系统的数据需求是显式声明的，系统可以独立于其所处理的实体进行调度和排序。<sup>[4](https://boyang.cs.uwm.edu/publication/sac2026_ECS.pdf)</sup>

## ECS与面向对象设计

ECS常被描述为面向对象编程的替代方案，尽管二者有所重叠。由软件工程师桑德·默滕斯（Sander Mertens）维护的ecs-faq列出了实际差异——他因Flecs ECS框架而知名：继承在OOP中是一等公民，而组合在ECS中是一等公民；OOP鼓励数据封装，而ECS鼓励暴露的朴素数据对象；OOP将数据与行为放在一起，而ECS将二者分离；OOP对象实例只有单一静态类型，而ECS实体可以拥有多个动态变化的组件。<sup>[1](https://github.com/SanderMertens/ecs-faq)</sup>

其动机是务实的。在基于继承的设计中，添加一项新能力往往迫使共享代码上移到公共根类，最终根类几乎持有全部功能，而每个子类自身提供的功能寥寥无几，产生出复杂且难以维护的代码。<sup>[10](https://developer.apple.com/library/archive/documentation/General/Conceptual/GameplayKit_Guide/EntityComponent.html)</sup> 在基于组合的设计中，一个射击型敌人是通过将移动组件和开火组件添加到同一个实体上来创建的，而不是通过重新安排类层次结构。<sup>[10](https://developer.apple.com/library/archive/documentation/General/Conceptual/GameplayKit_Guide/EntityComponent.html)</sup> 有人主张ECS符合面向对象设计的特征，应被视为其子集，但在实践中设计过程差异足够大，将其视为一种独立方法是有益的。<sup>[1](https://github.com/SanderMertens/ecs-faq)</sup>

## 原型存储与性能

**数据布局是ECS性能主张背后的机制。** 现代基于原型的框架遵循数组结构（SoA）原则：组件集合相同的实体被归入原型，即稠密的列式表，每一列代表一种组件类型，每一行代表一个实体。<sup>[4](https://boyang.cs.uwm.edu/publication/sac2026_ECS.pdf)</sup> 虚幻的MassEntity出于同样的原因将原型的实体组织在内存块中，因此为同一原型的实体获取片段很快。<sup>[6](https://dev.epicgames.com/documentation/unreal-engine/overview-of-mass-entity-in-unreal-engine?lang=en-US)</sup> 迭代一列同质数据能使CPU缓存行保持满载，而这正是面向对象布局所缺乏的特性——在后者中，每个对象将其字段分散在堆上。

基准测试在附带条件的情况下支持这一优势。肖恩·M·哈里斯（Shawn M. Harris）2022年在加州理工州立大学的硕士论文构建了一种实体组件系统架构，并与面向对象架构进行了基准对比，结果提示其CPU缓存性能更优。<sup>[11](https://digitalcommons.calpoly.edu/theses/2389/)</sup> 一项针对Unity 6在2D生成-销毁工作负载中的对照研究，每种条件进行30次独立试验，实体数量从100到20,000不等，发现ECS在约10,000个实体以内可维持60帧/秒的帧预算，而OOP为6,000个，ECS可承载的实体数量为OOP的1.67倍；同一研究还发现，在20,000个实体时垃圾回收差异变得不显著，此时ECS批量销毁产生的内存压力与OOP相当，这限定了ECS无条件免疫垃圾回收的假设。<sup>[5](https://papers.ssrn.com/sol3/papers.cfm?abstract_id=6854635)</sup> 一项使用塔防仿真的形式化研究在相同条件下比较了原型ECS与其他设计，发现其帧率更高、帧稳定性更好，归因于缓存效率的提升和实体访问的一致性。<sup>[12](https://arxiv.org/abs/2606.14919)</sup>

**结构性变更是代价高昂的情形。** 添加或移除组件会改变实体的组合，并将其从一个原型移动到另一个原型，因此框架会推迟此类变更。MassEntity处理器通过MassCommandBuffer请求组合变更，在当前处理批次结束时进行批处理，以避免在实体被处理期间将其在原型之间移动。<sup>[6](https://dev.epicgames.com/documentation/unreal-engine/overview-of-mass-entity-in-unreal-engine?lang=en-US)</sup> Unity的EntityCommandBufferSystem起到同样的作用，将结构性变更分组，从而减少一帧中的同步点。<sup>[8](https://docs.unity.cn/Packages/com.unity.entities%400.8/manual/ecs_systems)</sup>

## 实现

该模式以不同的纯粹程度出现在商业引擎和开源引擎中。Unity的面向数据技术栈（DOTS）在Entities包中实现了ECS，其中子场景中的GameObject由烘焙器转换为实体和ECS组件，系统被自动发现并组织成嵌套的ComponentSystemGroup以驱动更新顺序。<sup>[13](https://docs.unity.cn/Packages/com.unity.entities%401.0/manual/ecs-workflow-intro.html)</sup><sup> • </sup><sup>[8](https://docs.unity.cn/Packages/com.unity.entities%400.8/manual/ecs_systems)</sup> 截至Unity 6.6开发周期，ECS包是与引擎版本同步开发的核心包，EntityId标识符类型正在GameObject与Entities之间对齐，EntityId已从4字节迁移到8字节，提供更大的地址空间。<sup>[14](https://discussions.unity.com/t/ecs-development-status-for-unity-6-6/1724480)</sup>

[虚幻引擎5](https://www.edgepedia.org/zh/unreal-engine-5)提供MassEntity，这是一个面向数据计算的框架，其中处理器——无状态类——提供处理逻辑，EntityQuery指定它们需要哪些片段类型。<sup>[6](https://dev.epicgames.com/documentation/unreal-engine/overview-of-mass-entity-in-unreal-engine?lang=en-US)</sup> 苹果的GameplayKit提供了该模式更轻量、带有对象色彩的版本：GKEntity是GKComponent对象的通用容器，GKComponentSystem管理指定类的所有组件的周期性更新消息，无论它们附加在哪些实体上。<sup>[10](https://developer.apple.com/library/archive/documentation/General/Conceptual/GameplayKit_Guide/EntityComponent.html)</sup> Bevy（Rust）和Flecs（C）是大型商业引擎之外的基于原型的框架。<sup>[4](https://boyang.cs.uwm.edu/publication/sac2026_ECS.pdf)</sup>

## 历史与形式化

该模式早于其名称。2002年的游戏《[地牢围攻](https://www.edgepedia.org/zh/dungeon-siege)》呈现了一个无需加载画面的无缝世界，这得益于其“数据驱动游戏对象系统”，该引擎与ECS高度相似，尽管当时该术语尚未被正式提出。<sup>[7](https://trepo.tuni.fi/bitstream/handle/123456789/27593/H%C3%A4rk%C3%B6nen.pdf?sequence=4)</sup> 斯科特·比拉斯（Scott Bilas）2002年在Gas Powered Games任职期间所作的讲座“数据驱动游戏对象系统”，据现任职于[Unity Technologies](https://www.edgepedia.org/zh/unity-technologies)的比拉斯本人所述，直接衍生了Unity的基于组件的游戏对象架构。<sup>[15](https://github.com/Adelost/entity-component-systems-study)</sup>

近期的学术工作将以往实现中非正式的部分形式化了。2026年，威斯康星大学密尔沃基分校计算机科学系的阿尼莎·塔斯尼姆（Anisha Tasnim）和田昭（Tian Zhao）发表了一篇论文，对原型ECS进行了形式化，将实体创建、组件组合、系统执行和原型迁移刻画为组合式状态转移，并指出原型ECS的语义一直是非正式的且依赖于实现，限制了对确定性、系统调度和结构性变更的严格推理。<sup>[12](https://arxiv.org/abs/2606.14919)</sup><sup> • </sup><sup>[3](https://doi.org/10.1145/3828534.3828535)</sup> 一个名为Core ECS的形式化模型由帕特里克·雷德蒙德（Patrick Redmond）、乔纳森·卡斯特罗（Jonathan Castello）、何塞·曼努埃尔·卡尔德隆·特里利亚（José Manuel Calderón Trilla）和林赛·库珀（Lindsey Kuper）开发，它识别出一类无论调度如何都表现确定性的ECS程序，使该模式成为一种构造即确定性的并发编程模型；作者们对照该模型调查了现实世界的框架，发现它们都留下了未被利用的确定性并发机会。<sup>[16](https://arxiv.org/abs/2508.15264)</sup>

## References

1. [ECS FAQ, Sander Mertens, GitHub](https://github.com/SanderMertens/ecs-faq)
2. [Entity Component System concepts, Unity Entities 1.3 documentation](https://docs.unity.cn/Packages/com.unity.entities%401.3/manual/concepts-intro.html)
3. [The Semantics of Entity Component System, ACM SIGAPP Applied Computing Review](https://doi.org/10.1145/3828534.3828535)
4. [The Essence of Entity Component System, Tasnim and Zhao](https://boyang.cs.uwm.edu/publication/sac2026_ECS.pdf)
5. [Empirical Scalability Analysis of Data-Oriented and Object-Oriented Architectures in Controlled 2D Game Scenarios, SSRN](https://papers.ssrn.com/sol3/papers.cfm?abstract_id=6854635)
6. [MassEntity Overview, Unreal Engine 5.8 Documentation](https://dev.epicgames.com/documentation/unreal-engine/overview-of-mass-entity-in-unreal-engine?lang=en-US)
7. [ENTITY-COMPONENT-SYSTEM, Härmälä (Tampere University thesis)](https://trepo.tuni.fi/bitstream/handle/123456789/27593/H%C3%A4rk%C3%B6nen.pdf?sequence=4)
8. [Systems, Unity Entities 0.8 documentation](https://docs.unity.cn/Packages/com.unity.entities%400.8/manual/ecs_systems)
9. [Is the Entity Component System architecture object oriented by definition?, Software Engineering Stack Exchange](https://softwareengineering.stackexchange.com/questions/211708/is-the-entity-component-system-architecture-object-oriented-by-definition?rq=1)
10. [Entities and Components, GameplayKit Programming Guide, Apple](https://developer.apple.com/library/archive/documentation/General/Conceptual/GameplayKit_Guide/EntityComponent.html)
11. [Implementation and Analysis of the Entity Component System Architecture, Shawn M. Harris, Cal Poly](https://digitalcommons.calpoly.edu/theses/2389/)
12. [The Essence of Entity Component System, arXiv](https://arxiv.org/abs/2606.14919)
13. [Understand the ECS workflow, Unity Entities 1.0 documentation](https://docs.unity.cn/Packages/com.unity.entities%401.0/manual/ecs-workflow-intro.html)
14. [ECS Development Status for Unity 6.6, Unity Discussions](https://discussions.unity.com/t/ecs-development-status-for-unity-6-6/1724480)
15. [Entity Component Systems study, Adelost, GitHub](https://github.com/Adelost/entity-component-systems-study)
16. [Exploring the Theory and Practice of Concurrency in the Entity-Component-System Pattern, arXiv](https://arxiv.org/abs/2508.15264)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 17, 2026; Oct 21, 2026 · Edited: Sep 17, 2026; Oct 21, 2026 · Last review: Oct 21, 2026*

Topic: 体育、游戏与休闲 › 电子游戏与数字游戏 › 游戏产业 › Development and technology › Game programming

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/entity-component-system

English original: https://www.edgepedia.org/entity-component-system
