I wanted to use interface in my job, I know that only unmanaged types are allowed in jobs so I have tried something like this:
[BurstCompile]
public struct JobWithInterfaces<TGenericA,TGenericB>:IJobParallelFor
where TGenericA:struct,IMyInterface
where TGenericB:struct,IMyInterface
{
[ReadOnly] public TGenericA A;
public TGenericA A;
public void Execute(int index)
{
//...
}
}
But burst is complaining that A is a managed type of IMyInterface. Constraining it to struct is not taken into account. Is it possible to do something like that or I just have to get unmanaged elements from the interface manually and assigned them to fields of the job?
Oddly I have done it again because I have changed my approach thinking that it is not possible and now I do not get this error. But I do get an error that I am writing to [ReadOnly] native array
InvalidOperationException: The Unity.Collections.NativeArray`1[System.Single] has been declared as [ReadOnly] in the job, but you are writing to it.
I get this native array using Property of en interface:
public struct AddMapAToMapBJobGeneric<T> : IJobParallelForBatch where T:struct, IMapIterface
{
public T MapA;
public T MapB;
public void Execute(int startIndex, int count)
{
var mapB = MapB.Data;
for (int j = startIndex, end = startIndex + count; j < end; j++)
{
var d = MapA.Data[j];
mapB[j] += d; // Exception is thrown here
}
}
}
I have a new approach to my code so I do not need a Generic job, but it would be nice to know if what I am doing is OK for the future
public struct TestMap : IMapInterface
{
public int Width { get; }
public NativeArray<float> Data { get; }
public TestMap(int width, Allocator allocator = Allocator.TempJob)
{
Width = width;
Data = new NativeArray<float>(width*width, allocator);
}
public void Dispose()
{
Data.Dispose();
}
}