I am trying to spawn a cell on my game grid and I keep running into errors, I finally got some code that should work done and I just do not know how to fix any of the errors now, I need help. I finally got past the hundreds of syntax errors hoping this would work but I am just not that good with generating mesh.
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.
MeshFilter cell = new MeshFilter();
List<Mesh> tempCells = new List<Mesh>();
CombineInstance[] meshToCombine = new CombineInstance[tempCells.Count];
int cellSize = 128;
List<Vector3> Vertices = new List<Vector3>();
int i = 0;
//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];
MeshFilter tempCell = new MeshFilter();
tempCell.mesh.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.mesh.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.mesh.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.mesh.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.mesh.uv = uv;
//Add the cell into a list to keep track of all the new objects.
tempCells.Add(tempCell.mesh);
//Combine all the meshes into one.
i = tempCells.Count;
meshToCombine[i].mesh = tempCell.sharedMesh;
meshToCombine[i].transform = tempCell.transform.localToWorldMatrix;
}
//finalize the new gameobject and add it to the hierarchy.
cell.mesh.Clear();
cell.transform.localPosition = new Vector3(xPosition, 0, yPosition);
cell.name = (cellName + " (" + xPosition + ", " + yPosition + ")");
cell.mesh.CombineMeshes(meshToCombine);
MeshFilter newCell = Object.Instantiate(cell);
newCell.name = cell.name;
newCell.transform.localPosition = cell.transform.localPosition;
}
}
there’s no reason you can’t do something because it’s in a static method… it’s just that you need to know the implication of it being in a static method.
A static method has no instance anywhere to access unless given one.
Now as for your code… what’s wrong? What’s not happening that you expect?
One thing I notice is that you create a Mesh, but you do nothing with it. You don’t return it from the static method, you just create it and… that’s it. You don’t attach it to a MeshRenderer on a GameObject. It just gets created, and exists, and that’s that.
I don’t know what you expect to do with it though… so I can’t give any further guidance there.
Finally got past teh syntax errors that were pissing me off and this should work now but I am getting various null reference errors. could you take another look at the code for me?
I created the mesh so I could create smaller chunks and combine them into that one later on, now that the code is all there it shoul;d be a little more obvious what I was going for.
I tried it this way and I am getting errors because I cannot attach this to anything in the hierarchy because it is an editor script and I cannot call resources.load the way I am and am having problems getting that to load correctly.
using UnityEngine;
using UnityEditor;
using System.Collections;
public class CreateCell : EditorWindow {
//Declare Variables for the Cell Creation Window.
public GameObject cell;
public int cellSize = 64;
public string cellName = "Wilderness";
public int xPosition = 0;
public int yPosition = 0;
public string xString = "0";
public string yString = "0";
[MenuItem("Heightmap/Create Cell %#c")]
static void Init()
{
//Used to open the cell creation window.
CreateCell createCell = (CreateCell)GetWindow(typeof(CreateCell));
createCell.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");
xString = GUILayout.TextField(xString, 5);
GUILayout.Label("Y");
yString = GUILayout.TextField(yString, 5);
EditorGUILayout.EndHorizontal();
GUI.backgroundColor = Color.blue;
if (GUILayout.Button("Create Cell", GUILayout.Width(250), GUILayout.Height(30)))
{
OnEnable();
}
}
void OnEnable()
{
xPosition = (int.Parse(xString) * cellSize);
yPosition = (int.Parse(yString) * cellSize);
cell = Resources.Load("Environment/Cells/Cell", typeof(GameObject)) as GameObject;
GameObject newCell = Instantiate(cell);
newCell.name = cell.name;
newCell.transform.localPosition = new Vector3(xPosition, 0, yPosition);
}
}
I have re-written my script to spawn a gameobject, the gameobject will be a blank cell and then will be editable, now I need help adding each cell to a list so you cannot create a cell that is in the same position as another cell. that way cells do not overlap, as soon as that list is done we can move onto editing.
using UnityEngine;
using UnityEditor;
using System.Collections;
public class CreateCell : EditorWindow {
//Declare Variables for the Cell Creation Window.
public GameObject cell;
public int cellSize = 64;
public string cellName = "Wilderness";
public int xPosition = 0;
public int yPosition = 0;
public string xString = "0";
public string yString = "0";
[MenuItem("Heightmap/Create Cell %#c")]
static void Init()
{
//Used to open the cell creation window.
CreateCell createCell = (CreateCell)GetWindow(typeof(CreateCell));
createCell.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");
xString = GUILayout.TextField(xString, 5);
GUILayout.Label("Y");
yString = GUILayout.TextField(yString, 5);
EditorGUILayout.EndHorizontal();
GUI.backgroundColor = Color.blue;
if (GUILayout.Button("Create Cell", GUILayout.Width(250), GUILayout.Height(30)))
{
AddCell();
}
}
void AddCell()
{
xPosition = (int.Parse(xString) * cellSize);
yPosition = (int.Parse(yString) * cellSize);
GameObject newCell = Instantiate(cell);
newCell.name = (cellName + " (" + (xPosition / cellSize) + ", " + (yPosition / cellSize) + ")");
newCell.transform.localPosition = new Vector3(xPosition, 0, yPosition);
}
}