how do i implement marching cubes to voxel engine?

so i have this voxel engine that i want to implement marching cubes to. i cant seem to find anything that will help, so heres the code

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

[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class VoxelCube : MonoBehaviour
{
    public Vector3 pos;
    public List<Vector3> Cubes = new List<Vector3>();

    public Mesh mesh;

    public List<Vector3> vertices = new List<Vector3>();
    public List<int> triangles = new List<int>();
    public List<Vector2> uvs = new List<Vector2>();

    private int LastVertex;

    private void Start()
    {
        mesh = new Mesh();
        for (int x = 0; x<20;x++)
        {
          for (int y = 0; y<20;y++)
        {
            for (int z = 0; z<20;z++)
        {
          SetCube(x,y,z);
        }
        }
        }
       


        for (int i = 0; i < Cubes.Count; i++)
        {
            pos = Cubes[i];
            DrawCube();
        }

        mesh.vertices = vertices.ToArray();
        mesh.triangles = triangles.ToArray();
        mesh.SetUVs(0,uvs.ToArray());
        mesh.RecalculateNormals();
        mesh.Optimize();
        GetComponent<MeshFilter>().mesh = mesh;
    }

    void SetCube(int x, int y, int z)
    {
        Cubes.Add(new Vector3(x,y,z));
    }

    void DrawCube()
    {
        if(!Cubes.Contains(new Vector3(pos.x,pos.y,pos.z+1)))
        {
          Front_GenerateFace();  
        }
      if(!Cubes.Contains(new Vector3(pos.x,pos.y,pos.z-1)))
      {
       Back_GenerateFace();  
      }
      
      if(!Cubes.Contains(new Vector3(pos.x-1,pos.y,pos.z)))
      {
       Left_GenerateFace();  
      }
      if(!Cubes.Contains(new Vector3(pos.x+1,pos.y,pos.z)))
      {
       Right_GenerateFace();  
      }
      if(!Cubes.Contains(new Vector3(pos.x,pos.y+1,pos.z)))
      {
       Top_GenerateFace();  
      }
      if(!Cubes.Contains(new Vector3(pos.x,pos.y-1,pos.z)))
      {
       Bottom_GenerateFace();  
      }
     
    }

    void Front_GenerateFace()
    {
        LastVertex = vertices.Count;
       vertices.Add(pos + Vector3.forward);
       vertices.Add(pos + Vector3.forward + Vector3.up);
       vertices.Add(pos + Vector3.forward + Vector3.up + Vector3.right);
       vertices.Add(pos + Vector3.forward + Vector3.right);

       triangles.Add(LastVertex+2);
       triangles.Add(LastVertex+1);
       triangles.Add(LastVertex);
       triangles.Add(LastVertex);
       triangles.Add(LastVertex+3);
       triangles.Add(LastVertex+2);   
    }
      void Back_GenerateFace()
    {
        LastVertex = vertices.Count;
       vertices.Add(pos + Vector3.right);
       vertices.Add(pos + Vector3.up + Vector3.right);
       vertices.Add(pos + Vector3.up);
       vertices.Add(pos);

       triangles.Add(LastVertex + 2);
       triangles.Add(LastVertex + 1);
       triangles.Add(LastVertex);
       triangles.Add(LastVertex);
       triangles.Add(LastVertex + 3);
       triangles.Add(LastVertex + 2);   
    }
      void Left_GenerateFace()
    {
        LastVertex = vertices.Count;
       vertices.Add(pos);
       vertices.Add(pos + Vector3.up);
       vertices.Add(pos + Vector3.forward + Vector3.up);
       vertices.Add(pos + Vector3.forward);

       triangles.Add(LastVertex+2);
       triangles.Add(LastVertex+1);
       triangles.Add(LastVertex);
       triangles.Add(LastVertex);
       triangles.Add(LastVertex+3);
       triangles.Add(LastVertex+2);   
    }
       void Right_GenerateFace()
    {
        LastVertex = vertices.Count;
       vertices.Add(pos + Vector3.right + Vector3.forward);
       vertices.Add(pos + Vector3.one);
       vertices.Add(pos + Vector3.right + Vector3.up);
       vertices.Add(pos + Vector3.right);

       triangles.Add(LastVertex+2);
       triangles.Add(LastVertex+1);
       triangles.Add(LastVertex);
       triangles.Add(LastVertex);
       triangles.Add(LastVertex+3);
       triangles.Add(LastVertex+2);   
    }
           void Top_GenerateFace()
    {
        LastVertex = vertices.Count;
       vertices.Add(pos + Vector3.up + Vector3.right);
       vertices.Add(pos + Vector3.one);
       vertices.Add(pos + Vector3.forward + Vector3.up);
       vertices.Add(pos + Vector3.up);

       triangles.Add(LastVertex+2);
       triangles.Add(LastVertex+1);
       triangles.Add(LastVertex);
       triangles.Add(LastVertex);
       triangles.Add(LastVertex+3);
       triangles.Add(LastVertex+2);   
    }
       void Bottom_GenerateFace()
    {
        LastVertex = vertices.Count;
       vertices.Add(pos);
       vertices.Add(pos + Vector3.forward);
       vertices.Add(pos + Vector3.forward + Vector3.right);
       vertices.Add(pos + Vector3.right);

       triangles.Add(LastVertex+2);
       triangles.Add(LastVertex+1);
       triangles.Add(LastVertex);
       triangles.Add(LastVertex);
       triangles.Add(LastVertex+3);
       triangles.Add(LastVertex+2);   
    }
}

(any optimizations would be nice too)

You sure?

You didn’t tell us what isn’t working with this code, so there’s not much we can work with. In general, having tons of extremely similar functions (like that Top_GenerateFace() etc) makes code very difficult to debug, so anything you can do to make all of that functionality share as much code as possible will help you more easily zero in on issues.

One easy win: when you initialize your Lists, put a number in there. Take a broad guess as to what their final array size will be, and plug in that number. Anytime a List has to resize its capacity it’s an ordeal efficiency-wise (gotta allocate more memory and copy over all the existing elements), and by default it initializes to a small capacity and doubles it each time you .Add beyond its capacity. So if you’re adding hundreds or thousands, it has to do that dozens of times, unless you initialize it with a big capacity to start with.

there is nothing wrong with the code i want to know how i would implement marching cubes