Unity Plane mesh creation and editing within unity editor

I am making a tool for unity to speed up development for my game, as creating each cell by hand in blender is just not going to work.I need help creating a plane (Cell) in unity. I need each cell to be 128128 faces (129129) vertices. I need to be able to raise and lower each vert and if a certain bool is triggered I need to be able to edit each vertices x and z coordinates as well. I want a box in the tool bar call the section “Terrain” and make it drop down and reveal “Create Cell” and when you click create cell make it have 4 fields asking for certain values of the cell, organized like below.

[String Name]
[Int]…[Int]
…[Button]

The string name will be for the name of the cell, the two int values will be for the x and y global coordinates of the cells position, I need these to grey out the 4th Button if the x and y coordinates already have a cell in them.

I need some sort of window with brushes and it will also house the bool for editing each vertices x and y coordinates as I mentioned above. Each brush will affect the raising and lowering of vertices and the area around the mouse they are affected. I need a window for different materials to be able to be selected as well so I can paint each triangle on the cell. as well as with certain slopes and certain heights to be able to automatically be painted.

I would love it if there is a way to be able to create a new global grid for the editor window, so that it displays a new grid with each square on the grid representing a 128*128 cell, though if the cell does not exist I want each square to display plus that if clicked generates a cell in that position. I think that would be really cool if it is possible.

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

public class CellWindow : EditorWindow
{
    public static string cellName = "Wilderness";
    public static int xPosition = 0;
    public static int yPosition = 0;
    private static string inputIntx = "";
    private static string inputInty = "";

    [MenuItem("Heightmap Creation/Editor")]
    static void Init()
    {
        CellWindow cellWindow = (CellWindow)GetWindow(typeof(CellWindow));
        cellWindow.Show();
    }

    void OnGUI()
    {
        EditorGUILayout.BeginHorizontal();
        GUILayout.Label("Name : ");
        cellName = GUILayout.TextField(cellName);
        EditorGUILayout.EndHorizontal();
        EditorGUILayout.BeginHorizontal();
        GUILayout.Label("xPosition : ");
        inputIntx = GUILayout.TextField(inputIntx, 5);
        GUILayout.Label("yPosition : ");
        inputInty = GUILayout.TextField(inputInty, 5);
        EditorGUILayout.EndHorizontal();

        GUI.backgroundColor = Color.grey;
        if (GUILayout.Button("Spawn Cell", GUILayout.Width(250), GUILayout.Height(30)))
        {
            xPosition = (int.Parse(inputIntx));
            yPosition = (int.Parse(inputInty));
            Cell.add(cellName, xPosition, yPosition);
        }
    }
}

public static class Cell
{
    public static void add(string cellName,int xPosition,int yPosition)
    {
        //Declare Variables.
        Mesh cell = new Mesh();

        //Clear the cell to avoid out of bound data.
        cell.Clear();

        // create point data, [3] for 3 verticies of a simple triangle
        List<Vector3> vertices = new List<Vector3>();
        Vector3[] verts = new Vector3[3];
        Vector3[] norms = new Vector3[3];
        Vector2[] uvs = new Vector2[3];
        int[] tris = new int[3];

        int cellSize = 64;

        //Plot points.
        for (int x = -cellSize; x <= cellSize; x++)
        {
            for (int z = -cellSize; z <= cellSize; z++)
            {
                //Consturct a Vector and add the point to a list.
                Vector3 vert = new Vector3(x, 0, z);
                vertices.Add(vert);
            }
        }

        //Foreach vertex connect other vertices.
        foreach (Vector3 vert in vertices)
        {
            //Create 3 points using each Vector3 in the list.
            Vector3 pointA = new Vector3(vert.x, vert.y, vert.z);
            Vector3 pointB = new Vector3(vert.x, vert.y, vert.z + 1);
            Vector3 pointC = new Vector3(vert.x + 1, vert.y, vert.z);

            //Determine if the points exist so unity knows not to add triangles past teh edge of the cell.
            if (vertices.Contains(pointB) && vertices.Contains(pointC))
            {
                //Add the points to form a triangle.
                verts[0] = pointA;
                verts[1] = pointB;
                verts[2] = pointC;
                //Set the normals.
                norms[0] = pointA;
                norms[1] = pointB;
                norms[2] = pointC;
                //Set the UVs.
                uvs[0] = new Vector2(pointA.x, pointA.z);
                uvs[1] = new Vector2(pointB.x, pointB.z);
                uvs[2] = new Vector2(pointC.x, pointC.z);
                //create the triangle.
                tris[0] = 0;
                tris[1] = 1;
                tris[2] = 2;

                //set the mesh up
                cell.vertices = verts;
                cell.normals = norms;
                cell.uv = uvs;
                cell.triangles = tris;
            }
        }

        //Create the cell
        cell.name = cellName + " (" + xPosition + (", ") + yPosition + (")");
        cell.SetVertices(vertices);
    }
}

Here is the code I have so far for the editor grid. I am not sure if it possible but right now I need help getting what I have to run in the editor.

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

public class Grid
{
    public static bool True = false;
}

public class WorldCellGrid : MonoBehaviour {
    int gridX = 4096;
    int gridY = 4096;

    List<Vector3> Vertices = new List<Vector3>();

    [ExecuteInEditMode]
    void Update()
    {
        for (int x = -gridX; x >= gridX; x++)
        {
            for (int y = -gridY; y >= gridY; y++)
            {
                Vector3 gridPosition = new Vector3(x * 128, 0, y * 128);
                Vertices.Add(gridPosition);
            }
        }

        foreach (Vector3 point in Vertices)
        {
            Vector3 NextPointX = new Vector3(point.x + 128, point.y, point.z);
            if (Vertices.Contains(NextPointX))
            {
                Gizmos.DrawLine(point, NextPointX);
            }

            Vector3 NextPointY = new Vector3(point.x, point.y, point.z + 128);
            if (Vertices.Contains(NextPointX))
            {
                Gizmos.DrawLine(point, NextPointY);
            }
        }
    }
}

this is more from my code.

I’m creating a 20 x 20 grid of cubes. it’s a start. The cube is the position of each vert. So know it’s time to think about how to use these verts to build a plane.

2879645–211336–YourKing.unitypackage (13.6 KB)

Update the CellWindow class with this to get the buttons

public class CellWindow : EditorWindow
{
    public static string cellName = "Wilderness";
    public static int xPosition = 0;
    public static int yPosition = 0;
    private static string inputIntx = "";
    private static string inputInty = "";

    [MenuItem("Heightmap Creation/Editor")]
    static void Init()
    {
        CellWindow cellWindow = (CellWindow)GetWindow(typeof(CellWindow));
        cellWindow.Show();
    }

    void OnGUI()
    {
        EditorGUILayout.BeginHorizontal();
        GUILayout.Label("Name : ");
        cellName = GUILayout.TextField(cellName);
        EditorGUILayout.EndHorizontal();
        EditorGUILayout.BeginHorizontal();
        GUILayout.Label("xPosition : ");
        inputIntx = GUILayout.TextField(inputIntx, 5);
        GUILayout.Label("yPosition : ");
        inputInty = GUILayout.TextField(inputInty, 5);
        EditorGUILayout.EndHorizontal();

        GUI.backgroundColor = Color.red;
        if (GUILayout.Button("Spawn Cell", GUILayout.Width(250), GUILayout.Height(30)))
        {
            xPosition = (int.Parse(inputIntx));
            yPosition = (int.Parse(inputInty));
            Cell.add(cellName, xPosition, yPosition);
        }
    }
}

plane generation system

2880994–211518–YourKing_GridSystem_v001.unitypackage (14.9 KB)

Did some work, I need to figure out how to combine the mesh before instantiating it, I also am going to need each individual triangle be able to flip corners as well as each individual triangle to be able to get paints a separate material if needed, for now lets just work on combining the meshes but just keep in mind the painting and flipping thing is looking like it will give us issues in the future.

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

public class CellWindow : EditorWindow
{
    //Declare Variables for CellWindow.
    public static string cellName = "Wilderness";
    public static int xPosition = 0;
    public static int yPosition = 0;
    private static string inputIntx = "";
    private static string inputInty = "";

    [MenuItem("Heightmap/Create Cell %#c")]
    static void Init()
    {
        //Used to open the cell creation window..
        CellWindow cellWindow = (CellWindow)GetWindow(typeof(CellWindow));
        cellWindow.Show();
    }

    void OnGUI()
    {
        //Set up the cell Creation window.
        EditorGUILayout.BeginHorizontal();
        GUILayout.Label("Cell Name");
        cellName = GUILayout.TextField(cellName);
        EditorGUILayout.EndHorizontal();

        EditorGUILayout.BeginHorizontal();
        GUILayout.Label("X");
        inputIntx = GUILayout.TextField(inputIntx, 5);
        GUILayout.Label("Y");
        inputInty = GUILayout.TextField(inputInty, 5);
        EditorGUILayout.EndHorizontal();

        GUI.backgroundColor = Color.blue;
        if (GUILayout.Button("Create Cell", GUILayout.Width(250), GUILayout.Height(30)))
        {
            xPosition = (int.Parse(inputIntx) * 128);
            yPosition = (int.Parse(inputInty) * 128);
            Cell.add(cellName, xPosition, yPosition);
        }
    }
}

public static class Cell
{
    public static void add(string cellName, int xPosition, int yPosition)
    {
        //Declare Variables for Cell.
        Mesh cell = new Mesh();
        List<Mesh> tempCells = new List<Mesh>();
        int cellSize = 128;

        List<Vector3> Vertices = new List<Vector3>();

        cell.Clear();

        //Create a grid to add vertices to the mesh.
        for (int x = -(cellSize / 2); x < (cellSize / 2); x++)
        {
            for (int z = -(cellSize / 2); z < (cellSize / 2); z++)
            {
                //Add each point to a list.
                Vector3 gridPosition = new Vector3(x, 0, z);
                Vertices.Add(gridPosition);
            }
        }

        //Use each vertex in the cell to create 2 triangles that form a square face.
        foreach (Vector3 vertex in Vertices)
        {
            //Add vertices to the mesh.
            Vector3[] tempVertices = new Vector3[4];
            Mesh tempCell = new Mesh();

            tempCell.Clear();

            tempVertices[0] = new Vector3(vertex.x, vertex.y, vertex.z);
            tempVertices[1] = new Vector3(vertex.x + 1, vertex.y, vertex.z);
            tempVertices[2] = new Vector3(vertex.x + 1, vertex.y, vertex.z + 1);
            tempVertices[3] = new Vector3(vertex.x, vertex.y, vertex.z + 1);

            tempCell.vertices = tempVertices;

            //Add triangles to the mesh.
            int[] tri = new int[6];

            tri[0] = 0;
            tri[1] = 2;
            tri[2] = 1;

            tri[3] = 2;
            tri[4] = 3;
            tri[5] = 1;

            tempCell.triangles = tri;

            //Add normals to the mesh.
            Vector3[] normals = new Vector3[4];

            normals[0] = -Vector3.forward;
            normals[1] = -Vector3.forward;
            normals[2] = -Vector3.forward;
            normals[3] = -Vector3.forward;

            tempCell.normals = normals;

            //Add uv maps to the mesh.
            Vector2[] uv = new Vector2[4];

            uv[0] = new Vector2(0, 0);
            uv[1] = new Vector2(1, 0);
            uv[2] = new Vector2(0, 1);
            uv[3] = new Vector2(1, 1);

            tempCell.uv = uv;

            //Add the cell into a list to keep track of all the new objects.
            tempCells.Add(tempCell);
        }

        //Combine all the meshes into one.
      
    }
}