Generic component in Foreach lambda crashes Unity

Following code crashes unity because of invalid code gen in EntityQueryDesc.
Are generic types not supported anymore?

using Unity.Entities;
using Unity.Jobs;

[assembly: RegisterGenericComponentType(typeof(Timer<ECS_Test>))]

public struct Timer<T> : IComponentData
{
    public float CurrentTime;
    public float MaxTime;

    public bool IsFinished { get { return CurrentTime >= MaxTime; } }

}

public struct ECS_Test : IComponentData
{
}


[AlwaysSynchronizeSystem]
public partial class SomeSystem : JobComponentSystem
{

    protected override JobHandle OnUpdate(JobHandle inputDeps)
    {
        Entities
            .WithName("GenericJob")
            .WithoutBurst()
            .ForEach((
                ref Timer<ECS_Test> testTimer
            ) =>
            {
                testTimer.CurrentTime += 1;
            }).Run();

        return inputDeps;
    }

}

Generics are supported i use them with no problems.
You code does crash indeed which seems to be a bug.
If you create a job instead of using lambda expressions in the Entities.Foreach it will run just fine.

using Unity.Burst;
using Unity.Entities;
using Unity.Jobs;

[assembly: RegisterGenericComponentType(typeof(Timer<ECS_Test>))]

public struct Timer<T> : IComponentData {
  public float CurrentTime;
  public float MaxTime;

  public bool IsFinished { get { return CurrentTime >= MaxTime; } }

}

public struct ECS_Test : IComponentData {
}


[AlwaysSynchronizeSystem]
public partial class SomeSystem : JobComponentSystem {

  [BurstCompile]
  struct GenericJob : IJobForEach<Timer<ECS_Test>> {
    public void Execute(ref Timer<ECS_Test> testTimer) {
      testTimer.CurrentTime += 1;
    }
  }

  protected override JobHandle OnUpdate(JobHandle inputDeps) {
    inputDeps = new GenericJob()
      .Run(this, inputDeps);
    return inputDeps;
  }

}

I suggest you to submit a bug for that.

1 Like