[Resolved] Native Array of struct containing different types. Error InvalidOperationException
I am just starting to work with Job System and Burst Compiler(1.4.0) and I have a problem.
I have a structure with arrays of different types. When I try to create a NativeArray out of it, I get an error on startup "InvalidOperationException: 'Data' used in NativeArray<> must be unmanaged (contain no managed types) and cannot itself be a native container type." I don't know what to do, I tried to use native arrays instead of unity arrays, but I still get the same error. The structure is used in another class to return the required parameters
Class:
public class Model
{
public List<GameObject> gameObjects;
public Vector3[] vertices;
public int[] triangles;
public Vector2[] uvs;
public Vector3[] normals;
public Data GetData()
{
return new Data(vertices, triangles, uvs, normals, gameObjects.Count);
}
}
Structure:
public struct Data
{
public readonly int gameObjectsCount;
public readonly Vector3[] vertices;
public readonly int[] triangles;
public readonly Vector2[] uvs;
public readonly Vector3[] normals;
public Data(Vector3[] _vertices, int[] _triangles, Vector2[] _uvs, Vector3[] _normals, int _gameObjectsCount)
{
vertices = _vertices;
triangles = _triangles;
uvs = _uvs;
normals = _normals;
gameObjectsCount = _gameObjectsCount;
}
}
The line that cause the error:
int length = 10;
NativeArray<Data> _data = new NativeArray<Data>(length, Allocator.Persistent);
Does anyone have an idea of how to get around this? Thanks in advance.
Answer by Alex-Naronov · Oct 15, 2020 at 03:51 PM
I gave up on the idea of using NativeArray, instead I used NativeStream. Maybe it will be useful for someone
private void AddToStream(int index, Data data)
{
writer.BeginForEachIndex(index);
writer.Write(data.elementsCount);
writer.Write(data.vertices.Length);
foreach (float3 vertex in Extestions.Vector3ToFloat3(data.vertices))
{
writer.Write(vertex);
}
writer.Write(data.triangles.Length);
foreach (int triangle in data.triangles)
{
writer.Write(triangle);
}
writer.Write(data.uvs.Length);
foreach (float2 vertex in Extestions.Vector2ToFloat2(data.uvs))
{
writer.Write(vertex);
}
writer.Write(data.normals.Length);
foreach (float3 normal in Extestions.Vector3ToFloat3(data.normals))
{
writer.Write(normal);
}
writer.EndForEachIndex();
}
public Data GetLookUpFromStream(int index)
{
NativeStream.Reader reader = stream.AsReader();
reader.BeginForEachIndex(index);
var vertices = new List<Vector3>();
var triangles = new List<int>();
var uvs = new List<Vector2>();
var normals = new List<Vector3>();
var elementsCount = reader.Read<int>();
var verticesLength = reader.Read<int>();
for (int i = 0;i < verticesLength; i++)
{
vertices.Add(reader.Read<float3>());
}
var trianglesLength = reader.Read<int>();
for (int i = 0; i < trianglesLength; i++)
{
triangles.Add(reader.Read<int>());
}
var uvsLength = reader.Read<int>();
for (int i = 0; i < uvsLength; i++)
{
uvs.Add(reader.Read<float2>());
}
var normalsLength = reader.Read<int>();
for (int i = 0; i < normalsLength; i++)
{
normals.Add(reader.Read<float3>());
}
reader.EndForEachIndex();
Data data = new Data(vertices.ToArray(), triangles.ToArray(),uvs.ToArray(),normals.ToArray(), elementsCount);
data.Print();
return data;
}
Answer by ga5p0d3 · Oct 15, 2020 at 01:31 PM
Caveat: I'm only just starting out with the Jobs system, but... My take on this is that you are passing managed arrays (non-blittable types) into the structure (by reference) which could be manipulated outside the job effectively circumventing the thread safety checks the job system tries to maintain to protect against race conditions. Your job could be reading the vertices while your class is changing their values. The readonly
qualifier only means the array reference can't be changed, not the properties or contents of it. My guess is that you need to set up NativeArray versions and copy the data into the structure. If you aren't finding this works, there is probably something different going on that still triggers the error.
Answer by insoluzioni · Mar 24, 2021 at 02:52 AM
@Alex-Naronov, I suppose your problem has to do with using Vector3 in your Data struct. I have some issues and found out about unmanaged types (didn't heard it before) : https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/unmanaged-types
Your struct should only have unmanaged types in order to make this work.
I turned Vector3 into a float array with 3 elements, for x, y and z. But it didn't work! Arrays are not supported as well. So you will have to create independent variables...
Your answer
Follow this Question
Related Questions
List of Structs variable values not changing 1 Answer
storing multiple data types in a list 0 Answers
float value is reset after every update 1 Answer
Strange behavior using structs with HashSet 0 Answers
How to use SyncListStruct properly? 0 Answers