Using probuilder functions through script at runtime

So I’m building a mesh in my game procedurally and my goal is to strip the duplicate vertices for performance.

I can do this in probuilder with the collapsesharedvertices method.

    public static void CollapseSharedVertices(Mesh mesh, Vertex[] vertices = null)
    {
        bool flag = vertices != null;
        if (vertices == null)
        {
            vertices = mesh.GetVertices();
        }

        int subMeshCount = mesh.subMeshCount;
        List<Dictionary<Vertex, int>> list = new List<Dictionary<Vertex, int>>();
        int[][] array = new int[subMeshCount][];
        int num = 0;
        for (int i = 0; i < subMeshCount; i++)
        {
            array[i] = mesh.GetTriangles(i);
            Dictionary<Vertex, int> dictionary = new Dictionary<Vertex, int>();
            for (int j = 0; j < array[i].Length; j++)
            {
                Vertex key = vertices[array[i][j]];
                if (dictionary.TryGetValue(key, out var value))
                {
                    array[i][j] = value;
                    continue;
                }

                array[i][j] = num;
                dictionary.Add(key, num);
                num++;
            }

            list.Add(dictionary);
            Debug.Log(i);
        }

        Vertex[] array2 = list.SelectMany((Dictionary<Vertex, int> x) => x.Keys).ToArray();
        if (flag | (array2.Length != vertices.Length))
        {
            Vertex.SetMesh(mesh, array2);
            mesh.subMeshCount = subMeshCount;
            for (int k = 0; k < subMeshCount; k++)
            {
                mesh.SetTriangles(array[k], k);
            }
        }
    }

But that function is designed to work with probuildermeshes. Does anyone know of a way to convert that function so I can use it in final build? It looks like the only thing that function uses is “Vertex” so if I can figure out how to convert that it might work. Or if there is another way to go about it?

Not sure if this helps, but Probuilder strips all probuilder scripts when you enter playmode by default. So I assume anything runtime tying into that is going to have problems. Not sure if PB is best tool for what you want to do. There are quite few procedural mesh generation libraries out there with MIT licenses. Mght be worth checking some of those out. I can’t personally recommend a specific one as I just started messing with the concept myself and it’s more of a point of interest for me rather than a need.

Also, depending on the target platform, there might not be much of a gain in doing this as modern gpu’s handle vertices much better than they used to. If you have a big level this might have the opposite of the desired effect when scaled beyond a small basic scene.