Job System, Unity | Native Array of struct containing different types. Error InvalidOperationException

Viewed 1986

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. My main task is to pass triangles, vertices and normals to Job to generate a mesh. 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.

1 Answers

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;
    }
Related