Entitas--ECS框架插件

ECS

Entity、Component、System

  • Entity
  • Component
  • System

模块解耦

守望先锋 https://gameinstitute.qq.com/community/detail/114516react

从描述的状态 Component 上,不一样的观察者会看见不一样的行为,拆分不一样System出来分别实现。缓存

内存连续

经过合理组织数据利用 CPU 的缓存机制来加快内存访问数据。ide

举个例子,当在 Unity 中实时更新若干个 Monster 和 Player 的坐标时,每每是在 Update 中实现 transform.position = new Vector(1,2,3),即须要加载数据是 Position,可是却把整个 Transform 数据加载进内存,更高概率形成 Cache Miss;此外,对应 Monster 和 Player 的 Update 生命周期更是不可控,致使加载的数据不久后就失效,必定几率形成 Cache Miss。函数

上面例子中对数据的处理方式对 Cache 不友好,也对 CPU 访问内存的时间局部性不友好。性能


Entitas

Component

  • Unity Component 包括数据和行为。线程

  • ECS Component 单纯包括数据。3d


System

Entitas 中的 System 等价于方法。component

System 存在如下四种类型:orm

System 类型 实现功能 Unity 类似函数
Initialize System 初始化功能 Unity Awake
IExecute System 每帧更新功能 Unity Update
Reactive System 触发式更新功能 Unity OnCollider 等
TearDown System 析构功能 Unity OnDestory

其中 Reactive System 比较特别,主要实现是经过 Group + Collector 组成,经过监听特定 Component 来处理对应的 Entity,而且在以后清除对应的 Entity,避免须要每帧处理大量数据。对象

Entitas 跟 Unity 交互

Unity 经过脚本构建对应的 Entity,并添加对应的 Entity Component 来传递消息,通过 Entitas 处理数据后经过监听函数返回 Unity。



录像回放机制

因为 Entitas 自己是严格时序控制,调用方在保证浮点数偏差,随机数等一致的状况下,对于同一个输入源,其输出结果也是一致的。

所以记录输入源便可模拟整个运算过程,有些相似魔兽争霸3的录像机制。

性能

经过图片能够看出,Entitas 在性能上要优于 Unity。

  • reactive 标签表示经过开启 Reactive System 处理。
  • non reactive 标签表示不经过 Reactive System 处理。
  • Job System 标签表示经过开始线程处理。

关闭 Reactive System

Entitas 中若是某个 Component 经过 ReplaceXXX 函数赋值,则 Reactive System 会收集到对应的 Entity,若是经过 XXX 直接赋值,则 Reactive System 不会收集到对应的 Entity,从而节省 CPU 处理消耗,可是在实际应用中,不会专门绕开 Reactive System。

对象池

Entitas 经过尽量复用全部对象来提升性能,减小内存分配时间和碎片。

  • 复用 Entity
  • 复用 Component
  • 索引 Components
  • 索引 ComponentValue( EntityIndex )
  • 复用 Groups

ECS 编写模式为在 System 中处理一批相同 Component 类型的 Entity,致使每次处理某个特定值时,须要检查所有相同类型 Component 的 Entity,所以引入 EntityIndex 以高效地定位拥有某个具体 Component 值的 Entity。

Job System

Entitas Job System 跟 Unity Job System 实现方式不一致,其中Entitas Job System 是经过手动开辟新的 Thread。

相关文章
相关标签/搜索