Get mesh from sprite without allocations

Hello Unity community!

I’m experimenting with ecs now and Im trying to draw multiple sprites using their meshes as they defined in sprite importer. I’m asking my question here instead of experimental forum section because it is not directly related with Ecs, it is more about sprites and meshes vertex/index buffers.

So far I’ve managed to extract sprite vertices and uvs, generate mesh, and push it to appopriate component, and it is drawn on screen. Everything works, no problem here, except I need to double copy vertex buffer because Sprite class retruns Vector2[ ] for vertices and Mesh class wants Vector3[ ].

I discovered what that vertex buffer is a copy of sprite data. Therefore, my code instantiates 2 copies of vertex buffer and again copies it into Mesh instance. I see it as very poor design. I’d like to know if I able to extract mesh from sprite importer settings without allocations at all in runtime? Okay, I agree to copy my buffer to mesh is required, but is there anything to avoid creating two copies of vertext buffer and one copy of index buffer and uv buffer? Maybe Im missing something obvious?
Thank you!

Since there’s no answer, i post some code to illustrate the problem

   public class SpriteMeshExtractor
   {
      private readonly List<Vector3> _vertices = new List<Vector3>();
    
      public Mesh meshFromSprite(Sprite sprite)
      {
         var vertices = sprite.vertices; // first copy of vertex buffer occurs here, when it is marshalled from unmanaged to managed memory
         var mesh = new Mesh();
    
         _vertices.Clear();  // this temporary buffer allows me to avoid one of those allocation, but i still do copy of data because Mesh.SetVertices can't accept Vector2[]

         for (var i = 0; i < vertices.Length; i++)
         {
            _vertices.Add(vertices[i]);
         }
    
         mesh.SetVertices(_vertices);  // here's the third copy of vertex buffer is created and marshalled back to unmanaged memory
         mesh.SetTriangles(sprite.triangles, 0);
         mesh.SetUVs(0, sprite.uv);
         mesh.RecalculateBounds();

         return mesh;
      }
   }

Is there any way to avoid those pointless copying? For my demo game I need to create up to 4 * 9 * 11 = 396 sprites on scene load, not including characters. This leads to 3 * 396 = 1188 redundant copy operations for super simple 2d game.

1 Like

Bump. No way to do this?

Still no way to do this