百度做网站电话多少钱,生物做实验的网站,网站推广只能使用在线手段进行,建立网站纯文字版本ECS是一种软件架构模式#xff0c;就像MVC一样。ECS最早在游戏《守望先锋》中提及到的相关链接。ECS具体是指实体#xff08;entity#xff09;、 组件#xff08;component#xff09;和系统#xff08;system#xff09;#xff1a;
实体#xff1a;实体是一个ID就像MVC一样。ECS最早在游戏《守望先锋》中提及到的相关链接。ECS具体是指实体entity、 组件component和系统system
实体实体是一个ID它是一个唯一的标识符用于标识一个对象它本身不包含任何数据只是一个ID它的作用是用于标识一个对象它的数据是由组件来提供的。
组件组件是一个数据结构它包含了一些数据用于描述一个对象的属性组件是没有任何行为的它只是一个数据结构。
系统主要用户逻辑处理进行状态迁移。系统中不保留数据且是无状态的。
三者之间的关系如下图所示
ECS在unity中出现的原因
在使用unity开发的时候monobehaviour是我们最常用的然而monobehaviour中的update是基于gameobject去更新的且无序的这样很容易造成memory cache miss。而ECS是基于数据的它将具有相同Component组合的Entity放在一起这样可以更好的利用cache提高性能。
archetype
通过上面的图和介绍我们知道通过将不同的Component组合和Entity一起组成了GameOjbectComponent不同组合的唯一性在ECS中被称为archetype。在ECS中archetype是一个很重要的概念它是用来管理内存的每个archetype都有一个chunk列表chunk是一种内存管理方式。 上图中EntityA和EntityB具有相同的Component组合所以它们属于同一个Archetype M而EntityC具有不同的Component组合所以它们属于不同的Archetype N。
下图是一个chunk的内存排布
component在chunk中的排布. #mermaid-svg-dOLGdj5zBc8nNxlZ {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-dOLGdj5zBc8nNxlZ .error-icon{fill:#552222;}#mermaid-svg-dOLGdj5zBc8nNxlZ .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-dOLGdj5zBc8nNxlZ .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-dOLGdj5zBc8nNxlZ .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-dOLGdj5zBc8nNxlZ .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-dOLGdj5zBc8nNxlZ .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-dOLGdj5zBc8nNxlZ .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-dOLGdj5zBc8nNxlZ .marker{fill:#333333;stroke:#333333;}#mermaid-svg-dOLGdj5zBc8nNxlZ .marker.cross{stroke:#333333;}#mermaid-svg-dOLGdj5zBc8nNxlZ svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-dOLGdj5zBc8nNxlZ g.classGroup text{fill:#9370DB;fill:#131300;stroke:none;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-dOLGdj5zBc8nNxlZ g.classGroup text .title{font-weight:bolder;}#mermaid-svg-dOLGdj5zBc8nNxlZ .nodeLabel,#mermaid-svg-dOLGdj5zBc8nNxlZ .edgeLabel{color:#131300;}#mermaid-svg-dOLGdj5zBc8nNxlZ .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-dOLGdj5zBc8nNxlZ .label text{fill:#131300;}#mermaid-svg-dOLGdj5zBc8nNxlZ .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-dOLGdj5zBc8nNxlZ .classTitle{font-weight:bolder;}#mermaid-svg-dOLGdj5zBc8nNxlZ .node rect,#mermaid-svg-dOLGdj5zBc8nNxlZ .node circle,#mermaid-svg-dOLGdj5zBc8nNxlZ .node ellipse,#mermaid-svg-dOLGdj5zBc8nNxlZ .node polygon,#mermaid-svg-dOLGdj5zBc8nNxlZ .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-dOLGdj5zBc8nNxlZ .divider{stroke:#9370DB;stroke:1;}#mermaid-svg-dOLGdj5zBc8nNxlZ g.clickable{cursor:pointer;}#mermaid-svg-dOLGdj5zBc8nNxlZ g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-dOLGdj5zBc8nNxlZ g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-dOLGdj5zBc8nNxlZ .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-dOLGdj5zBc8nNxlZ .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-dOLGdj5zBc8nNxlZ .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-dOLGdj5zBc8nNxlZ .dashed-line{stroke-dasharray:3;}#mermaid-svg-dOLGdj5zBc8nNxlZ #compositionStart,#mermaid-svg-dOLGdj5zBc8nNxlZ .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-dOLGdj5zBc8nNxlZ #compositionEnd,#mermaid-svg-dOLGdj5zBc8nNxlZ .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-dOLGdj5zBc8nNxlZ #dependencyStart,#mermaid-svg-dOLGdj5zBc8nNxlZ .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-dOLGdj5zBc8nNxlZ #dependencyStart,#mermaid-svg-dOLGdj5zBc8nNxlZ .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-dOLGdj5zBc8nNxlZ #extensionStart,#mermaid-svg-dOLGdj5zBc8nNxlZ .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-dOLGdj5zBc8nNxlZ #extensionEnd,#mermaid-svg-dOLGdj5zBc8nNxlZ .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-dOLGdj5zBc8nNxlZ #aggregationStart,#mermaid-svg-dOLGdj5zBc8nNxlZ .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-dOLGdj5zBc8nNxlZ #aggregationEnd,#mermaid-svg-dOLGdj5zBc8nNxlZ .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-dOLGdj5zBc8nNxlZ .edgeTerminals{font-size:11px;}#mermaid-svg-dOLGdj5zBc8nNxlZ :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Archetype ArchetypeChunkData Chunks; ComponentTypeInArchetype* Types; ArchetypeChunkData Chunk** p; Chunk 执行System unity会自己实例化system,system里的方法默认都在主线程执行可以通过一下几种方式来实现多线程
Entities.ForEachJob.WithCodeIJobEntityIJobEntityBatchC# Job System
各部分的创建
下面是一个简单的例子创建一个CustomSystem它会在每一帧中更新CustomData的值。ComponentData不一定要和MonoBehaviour一起使用它可以单独使用像一些CPU密集性的计算可以使用ComponentData来实现。下面的示例重点是为了说明各部分的创建过程从LaunchECS中可以发现仅仅使用了World中的EntityManager来创建了一个Entity系统的创建被注视掉了。这是因为unity会自己实例化system所以我们不需要手动创建system。
public class Custom : MonoBehaviour
{private void Awake(){LaunchECS();}private void LaunchECS(){var world World.DefaultGameObjectInjectionWorld;world.EntityManager.CreateEntity(typeof(CustomData));// world.CreateSystemCustomSystem();}
}public struct CustomData : IComponentData
{public int Value;
}public partial class CustomSystem : SystemBase
{[BurstCompile]protected override void OnUpdate(){var timeElapsedTime World.Time.ElapsedTime;Entities.ForEach((ref CustomData customData) {customData.Value (int)(100 * math.sin(timeElapsedTime));}).ScheduleParallel();}
}游戏制作
ECS的大致情况我们已经了解了那如何在项目中应用呢。下面是一个简单的例子我们创建一个Cube然后通过ECS来控制它的移动。
将下面的代码放到名叫MoveMono的cs文件中然后在场景中添加一个sub scene在sub scene中添加一个Cube然后添加一个MoveMono组件并设置Velocity的值。 运行后我们会发现Cube会按照我们设置的Velocity的值进行移动。
[GenerateAuthoringComponent]
public struct MoveData : IComponentData
{public float3 Velocity;
}public partial class MoveSystem : SystemBase
{[BurstCompile]protected override void OnUpdate(){var deltaTime World.Time.DeltaTime;Entities.ForEach((ref Translation translation, in MoveData moveData) {translation.Value moveData.Velocity * deltaTime;}).ScheduleParallel();}
}上面代码中使用了GenerateAuthoringComponent这个属性将会自动生成一个名为MoveMono的MonoBehaviour所有虽然我们并没有显示的创建MoveMono但是我们可以给GameObject添加组件是可以看到MoveMono.
除了GenerateAuthoringComponent属性以外还有其他方式去将ComponentData和MonoBehaviour关联起来还可以有其他方式。我们将在后面的gameplay文章中介绍。