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);
}
}
}
}