How to find the vertices of each edge on Mesh

hi guys i am trying to get all of the mesh edges(sides- the light blue lines that appear when you select a mesh, the wire frame) each edge is a pair of vertices that form that edge(line, side) and are conected, i saw this:

and try to use the sample code in the link that its from unity procedural example,
i try it with unity default plane and cube it works fine but it only retruns the edges on the borders the horizontal and vertical external lines of a plane but no the diagonal or internal lines, like this:


it only return me the red lines but not the green ones, some maybe plz can help me with this

This is a method that takes about 20 lines but it’s very simple…


Get the array of mesh.triangles


Run through the entire list in a loop, and return 3 sets of edges, made of 2 vertices, for each triangle and put them in a new array of called mesh.edges, preferably, keep it orderly so that the point with the smallest X, Y, Z is the 1st points of the pair in the list for each edge, that means you have to compare XY and Z for each edge prior to storing it in the array.
The array of mesh.edges is going to be the same style the mesh.triangles, except it references the points in the mesh.vertex array in pairs.

If you’re not worried about having duplicate edges, they need to compare them and order them, just convert the mesh triangles into a new array of edges with 6 vertices per triangle.

By this time you have about 15 lines. mostly the compare XYZ logic in the loop. The rest is 5 lines.


After that you can delete duplicates because they are ordered so that the 1st point is always the lowest XYZ, so you can easily compare the 1st and 2nd points to all the other ones in the array. there should be a lot of duplicates because triangles share edges.

It’s a good learning project I have done stuff seminar. use Debug.DrawLine() if you want, draw the edges using one of the points and the Vector3 the 2nd one.

Remember that some models don’t share vertices among triangles and 3 times more vertices in the array, duplicates. It doesn’t really matter but it’s good to know, please vote at my answer if it’s useful my feedback is very low, my last profile was the thousand! I can’t give people, karma.

why not just texture the objects?
it would make it a lot more graphically sound, would it not?

Firstly you need to create a list of edges (edge could be a struct). Then you have to iterate through mesh.triangles, taking 3 elements (vertices) at a time, and then check if any pair of these vertices should be added to your list.

Sample code below, but please note that it’s just a very basic solution, not optimized at all. Its performance degrades significantly with triangles count increase.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public class EdgesTest : MonoBehaviour
    public struct Edge
        public Vector3 v1;
        public Vector3 v2;

        public Edge(Vector3 v1, Vector3 v2)
            if (v1.x < v2.x || (v1.x == v2.x && (v1.y < v2.y || (v1.y == v2.y && v1.z <= v2.z))))
                this.v1 = v1;
                this.v2 = v2;
                this.v1 = v2;
                this.v2 = v1;

    void Start()
        var mesh = this.GetComponent<MeshFilter>().mesh;

        var edges = GetMeshEdges(mesh);
        //for (int i = 0; i < edges.Length; i++)
        //    print(i + ": " + edges_.v1 + ", " + edges*.v2);*_


private Edge[] GetMeshEdges(Mesh mesh)
HashSet edges = new HashSet();

for (int i = 0; i < mesh.triangles.Length; i += 3)
var v1 = mesh.vertices[mesh.triangles*];*
var v2 = mesh.vertices[mesh.triangles[i + 1]];
var v3 = mesh.vertices[mesh.triangles[i + 2]];
edges.Add(new Edge(v1, v2));
edges.Add(new Edge(v1, v3));
edges.Add(new Edge(v2, v3));

return edges.ToArray();

Yes this question is from 2013, but here is a very short solution:
Copy all vertices of ur Mesh to a List.
Now remove all duplicate Vector3

The RemoveFunction is from here:

using System.Linq;
public class Vertices : MonoBehaviour {

    void Start()
        mesh = GetComponent<MeshFilter>().mesh;
        vertices = mesh.vertices;
        List<Vector3> VertexList = new List<Vector3>();
        foreach(Vector3 vertex in vertices)

        VertexList = VertexList.Distinct().ToList();