【问题标题】:mark struct as unmanaged in C# - Unity ECS Baker在 C# 中将结构标记为非托管 - Unity ECS Baker
【发布时间】:2022-12-04 21:36:34
【问题描述】:

我正在处理新的 ECS 包 (com.unity.entities) 并在我的 Monobehavior 中包含以下代码:

public class LevelBaker : Baker<LevelMono>
{
    public override void Bake(LevelMono authoring)
    {
        AddComponent(new LevelProperties
        {
            SpawnDimensions = authoring.SpawnDimensions,
            NeutralSpawnCount = authoring.NeutralSpawnCount,
            NeutralActorPrefab = GetEntity(authoring.NeutralActorPrefab)
        });
        AddComponent(new LevelRandom
        {
            Value = Random.CreateFromIndex(authoring.RandomSeed)
        });
    }
}

代码运行正常,但 Rider 突出显示了 AddComponent 方法

“ComponentsAndTags.LevelProperties”类型必须是有效的非托管 类型(简单数字、'bool'、'char'、'void'、枚举类型或 具有非托管类型的所有字段的非泛型结构类型 嵌套级别)以便将其用作“T”的类型参数 范围

错误,因为它有这样的定义:

public void AddComponent<T>(in T component) where T : unmanaged, IComponentData

LevelProperties 和 LevelRandom 是简单的结构,只包含非托管类型,但 Rider 似乎不知道。这是 LevelProperties 的代码:

public struct LevelProperties : IComponentData
    {
        public float2 SpawnDimensions;
        public int NeutralSpawnCount;
        public Entity NeutralActorPrefab;
    }

我如何才能将 Level Properties 结构“标记”为非托管结构,以便 Rider 停止将其突出显示为错误?
我使用的是最新版本的 Rider 和 Unity 2022.2.0b16。代码编译运行,只有 Rider 显示错误。

【问题讨论】:

    标签: c# unity3d struct unmanaged rider


    【解决方案1】:

    当结构的字段和属性也是非托管时,结构也是非托管的。使用 Entity 作为类型可能是它被认为是托管的原因。

    https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/unmanaged-types

    【讨论】:

    • 但是 Entity 是一种非托管类型,它是 ECS 的一部分,它的唯一目的是成为 ECS 使用的非托管结构的非托管部分
    猜你喜欢
    • 2012-04-08
    • 2013-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-26
    • 2011-02-24
    • 1970-01-01
    相关资源
    最近更新 更多