0

我正在使用 Unity 实体组件系统。当我尝试编写以下代码时:

Entities.WithAll<SomeComponentData, SomeComponentData2, SomeBufferElementData>()
        .ForEach((Entity entity,  ref SomeComponentData componentData, ref SomeComponentData2 componentData2, DynamicBuffer<SomeBufferElementData> buffer) => {

});

我收到以下错误error CS0315: The type 'Unity.Entities.Entity' cannot be used as type parameter 'T0' in the generic type or method 'EntityQueryBuilder.ForEach<T0>(EntityQueryBuilder.F_D<T0>)'. There is no boxing conversion from 'Unity.Entities.Entity' to 'Unity.Entities.IComponentData'.

当我只使用一个 ComponentData 和一个 BufferElementData 时,它编译得很好,即以下工作:

Entities.WithAll<SomeComponentData, SomeBufferElementData>()
        .ForEach((Entity entity,  ref SomeComponentData componentData, DynamicBuffer<SomeBufferElementData> buffer) => {

});

我查看了 ForEach 代码,它是生成的代码,其中包含许多组件和动态缓冲区的排列,但我找不到这个。 (请参阅答案 - 我只是看的不够努力!)在不支持 ComponentData 和 BufferElementData 的排列的情况下会发生什么?

4

1 回答 1

1

事实证明这种排列受支持的,但我不得不把 DynamicBuffer 放在第一位。那是:

Entities.WithAll<SomeBufferElementData, SomeComponentData, SomeComponentData2>()
        .ForEach((Entity entity,  DynamicBuffer<SomeBufferElementData> buffer, ref SomeComponentData componentData, ref SomeComponentData2 componentData2) => {

});

我通过查看IJobForEachWithEntity_XXX接口发现了这一点。我发现有一个IJobForEachWithEntity_EBCC接口,这让我知道缓冲区必须在组件之前。

希望这可以节省一些人的时间!

于 2020-06-08T13:40:37.443 回答