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就會被分在不同的類別中,這樣可以提高我們的渲染效率(將相同值的物體放在一起渲染是最有效的)

 

 

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