简体   繁体   中英

Get array from list without heap allocation

I have a list and I want to assign its array to a property.

public void BuildMesh(List<Vector3> list){
    mesh.vertices=list.ToArray();
}

Now the problems:

  • The project is game and is very tough on garbage collection so the default implementation of ToArray() is not an option as it creates a new array beside list's internal array.
  • The mesh object is from a closed source API and the vertices property is a Vector3[] so can't assign a pointer to it.

Do I have any option to prevent heap allocation?

EDIT: This is not a duplicate

Can't use IList<Vector3> . The mesh is from a closed source API and needs Vector3[] so I can't assign IList<Vector3> to it.

I think you're using Unity. If I'm right, try mesh.SetVertices(list) which accept a List<Vector3> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM