ECS的简单入门(三):Component

Component概念

ECS中的Component和我们以往MonoBehaviour的Component不同,ECS的Component中,只用于存放数据,并不会有行为。操纵数据的行为我们交给了System来处理。

在ECS中,当一个Struct实现了以下接口之一,就可以称之为Component

  • IComponentData
  • IBufferElementData
  • ISharedComponentData
  • ISystemStateComponentData
  • ISharedSystemStateComponentData

 

IComponentData

IComponentData是用于一般用途的组件接口,我们可以看下Unity自带的用于设置Entity位置信息的Translation Component的内容:

using System;
using Unity.Entities;
using Unity.Mathematics;

namespace Unity.Transforms
{
    [Serializable]
    [WriteGroup(typeof(LocalToWorld))]
    [WriteGroup(typeof(LocalToParent))]
    public struct Translation : IComponentData
    {
        public float3 Value;
    }
}

可以看见里面只是纪录了一个float3的数据,用于表示座标的x,y,z信息,简单明了。因此若我们想改变一个Entity的位置信息,只需要添加Translation Component,并且修改其Value值,并不需要像GameObject那样有一个冗长的Transform组件来处理了。

IComponentData 结构体中不能包含引用类型的对象,因此不会产生GC,从而提高性能。

Unity.Mathematics

我们可以看见Translation中,用到的是float3,而不是Vector3。float3是Unity提供的数学库Unity.Mathematics中的类型。使用它的好处是可以直接映射到硬件的SIMD寄存器,可以提高我们的游戏性能,这是 Burst 相关知识,在这就不详细介绍了。我们只需知道在我们自己编写Component的时候,需要使用Unity.Mathematics中的类型。

 

ISharedComponentData

shared component是一种比较特殊的component。前面我们讲到不同的component组合为一个archetype,相同archetype的entity会被连续的分配在一个chunk中。然而若entity中含有shared component,那么会根据shared component中的值来进行区分entity,将值不相同的entity分配在不同的chunk中,但是不会导致archetype的改变。

举个例子,假设我们有两个component,C1,C2,其中C2为shared component,我们的entity都包含C1和C2两个component,且值全相同。此时只有一个archetype,也就是C1,C2的组合,这些entity都会被连续的分配在一个chunk中(除非这个chunk装满了)。若我们改变C1的值,在内存分配上并不会产生什么变化,但是如果我们改变shared component也就是C2的值,那么这些entity就会被分配到一个个新的chunk中,C2值相同的entity会在同一个chunk中。

因此我们不能滥用shared component,因为可能导致我们chunk的利用率变低,cache miss概率变高,从而性能降低。同样的,我们自己编写shared component的时候也要避免一些不是必须的字段,降低值的多样性。

同时shared component中我们可以使用引用类型的对象。Hybrid.rendering package中的Rendering.RenderMesh就是shared component,如下

[System.Serializable]
public struct RenderMesh : ISharedComponentData, IEquatable<RenderMesh>
{
    public Mesh                 mesh;
    public Material             material;
    public int                  subMesh;
    [LayerField] public int                  layer;
    public ShadowCastingMode    castShadows;
    public bool                 receiveShadows;
    public bool Equals(RenderMesh other)
    {
        return
            mesh == other.mesh &&
            material == other.material &&
            subMesh == other.subMesh &&
            layer == other.layer &&
            castShadows == other.castShadows &&
            receiveShadows == other.receiveShadows;
    }
    public override int GetHashCode()
    {
        int hash = 0;
        if (!ReferenceEquals(mesh, null)) hash ^= mesh.GetHashCode();
        if (!ReferenceEquals(material, null)) hash ^= material.GetHashCode();
        hash ^= subMesh.GetHashCode();
        hash ^= layer.GetHashCode();
        hash ^= castShadows.GetHashCode();
        hash ^= receiveShadows.GetHashCode();
        return hash;
    }
}

注:编写shared component除了需要实现ISharedComponentData接口,同时还要实现IEquatable<T>接口和重写GetHashCode方法。

使用RenderMesh,EntityManager会把组件相同但是mesh或者material等不同entity就会被分在不同的类别中,这样可以提高我们的渲染效率(将相同值的物体放在一起渲染是最有效的)

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章