How to optimise mesh data

How to optimise mesh data

So i have some procedurally generated mesh data and i want to reduce it down to its minimum number of verts. In case it matters this is a unity project. 
Working on the basis of a simple example, lets assume a typical flat surface of points 2 by 3.
The point / vertex at [1,1] is used in many triangles.
I've generated mesh for a voxel type engine that adds verts to a list based on face visiblility and now I want to remove all the duplicates.
Can anyone come up with an efficient way of doing this because what i have is sooo bad its not even funny (and i don't even think it's logically correct) ...
private void Optimize()
    Vector3 v;
    Vector3 v2;
    for (int i = 0; i < Vertices.Count; i++)
        v = Vertices[i];
        for (int j = i+1; j < Vertices.Count; j++)
            v2 = Vertices[j];
            if (v.x == v2.x && v.y == v2.y && v.z == v2.z)
                for (int ind = 0; ind < Indices.Count; ind++)
                    if (Indices[ind] == j)
                        Indices[ind] = i;
                    else if (Indices[ind] > j && Indices[ind] > 0)


Ok i managed to get this (code sample above updated) to render an "optimised" set of verts but the UV data is all wrong now, which would make sense because i'm basically just removing any UV Vector that represents a UV coord for a removed vert and not actually considering what I need to do to "fix the tri" so to speak.
The code now seemingly does work but its quite time consuming, still looking to further optimise.


Answer 1:

Unity’s Mesh class has an Optimize method which should give you what you want.

From the wiki page:

You should use it if you generate a mesh from scratch procedurally and
you want to trade better runtime performance against higher load time.

Answer 2:

I think you will have to spend time running the optimization and there isn’t much way around that. Maybe the implementation itself could be faster but still.

Try loading chunks of mesh at a time, its a common solution, or decreasing the number of optimizations that have to be done.

Another solution could be optimizing the mesh itself (the assets) rather than doing it at runtime