Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Distributing Mesh Creation Over Multiple Frames in UnityJob

Discussion in 'Getting Started' started by vampir26, May 18, 2024.

  1. vampir26

    vampir26

    Joined:
    Mar 29, 2010
    Posts:
    110
    Hi,

    I am generating voxel terrain that consists of many different individual chunks. I am doing this in a UnityJob, so it runs smoothly without any stuttering.

    However, after the UnityJob is finished and I create the mesh, I experience a frame drop.

    So far, I haven't found a way to do this without stuttering. I thought about doing this in a coroutine and distributing the mesh creation over multiple frames.

    But I only know how to create the mesh all at once with all the data at once:
    Code (CSharp):
    1.  
    2. NativeArray<Vector3> verticesArray = chunkJob.Vertices.AsArray();
    3. NativeArray<int> trianglesArray = chunkJob.Triangles.AsArray();
    4. NativeArray<Vector2> uvsArray = chunkJob.UVs.AsArray();
    5.  
    6. Mesh mesh = new Mesh();
    7. mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
    8. mesh.SetVertices(verticesArray);
    9. mesh.SetTriangles(trianglesArray.ToArray(), 0);
    10. mesh.SetUVs(0, uvsArray);
    11.  
    12. mesh.RecalculateNormals();
    13. mesh.RecalculateBounds();
    14.  
    15. meshFilter.mesh = mesh;
    16. meshCollider.sharedMesh = mesh;
    17.  
    Does anyone know how I can distribute this over multiple frames?
    Thanks and have a nice day