void OnGUI() { GUILayout.Label("2D Grid Options", EditorStyles.boldLabel); Columns = EditorGUILayout.IntField("Number Of Columns", Columns); Rows = EditorGUILayout.IntField("Number Of Rows", Rows); SeperationX = EditorGUILayout.FloatField("Seperation in X", SeperationX); SeperationZ = EditorGUILayout.FloatField("Seperation in Z", SeperationZ); chosenPrimitive = (GridItemsType)EditorGUILayout.EnumPopup("Grid Item", chosenPrimitive); if (chosenPrimitive == GridItemsType.Custom) { theNewObject = EditorGUILayout.ObjectField("Custom Prefab", theNewObject, typeof(Object), true); } if (GUILayout.Button("Create Grid")) { createTheGridItems(chosenPrimitive); } }
void createTheGridItems(GridItemsType theType) { GameObject GridHandle = new GameObject("3D Grid Parent"); GridHandle.transform.position = Vector3.zero; GridHandle.transform.rotation = Quaternion.identity; switch (theType) { case GridItemsType.Cube: for (int k = 0; k < stacks; k++) { for (int i = 0; i < Columns; i++) { for (int j = 0; j < Rows; j++) { GameObject OBJ = GameObject.CreatePrimitive(PrimitiveType.Cube); OBJ.transform.position = new Vector3(i + tempx, tempy, j + tempz); OBJ.transform.SetParent(GridHandle.transform); tempz += SeperationZ; } tempx += SeperationX; tempz = 0; } tempy += SeperationY; tempx = 0; } break; case GridItemsType.Sphere: for (int k = 0; k < stacks; k++) { for (int i = 0; i < Columns; i++) { for (int j = 0; j < Rows; j++) { GameObject OBJ = GameObject.CreatePrimitive(PrimitiveType.Sphere); OBJ.transform.position = new Vector3(i + tempx, tempy, j + tempz); OBJ.transform.SetParent(GridHandle.transform); tempz += SeperationZ; } tempx += SeperationX; tempz = 0; } tempy += SeperationY; tempx = 0; } break; case GridItemsType.Capsule: for (int k = 0; k < stacks; k++) { for (int i = 0; i < Columns; i++) { for (int j = 0; j < Rows; j++) { GameObject OBJ = GameObject.CreatePrimitive(PrimitiveType.Capsule); OBJ.transform.position = new Vector3(i + tempx, tempy, j + tempz); OBJ.transform.SetParent(GridHandle.transform); tempz += SeperationZ; } tempx += SeperationX; tempz = 0; } tempy += SeperationY; tempx = 0; } break; case GridItemsType.Cylinder: for (int k = 0; k < stacks; k++) { for (int i = 0; i < Columns; i++) { for (int j = 0; j < Rows; j++) { GameObject OBJ = GameObject.CreatePrimitive(PrimitiveType.Cylinder); OBJ.transform.position = new Vector3(i + tempx, tempy, j + tempz); OBJ.transform.SetParent(GridHandle.transform); tempz += SeperationZ; } tempx += SeperationX; tempz = 0; } tempy += SeperationY; tempx = 0; } break; case GridItemsType.Plane: for (int k = 0; k < stacks; k++) { for (int i = 0; i < Columns; i++) { for (int j = 0; j < Rows; j++) { GameObject OBJ = GameObject.CreatePrimitive(PrimitiveType.Plane); OBJ.transform.position = new Vector3(i + tempx, tempy, j + tempz); OBJ.transform.SetParent(GridHandle.transform); tempz += SeperationZ; } tempx += SeperationX; tempz = 0; } tempy += SeperationY; tempx = 0; } break; case GridItemsType.Quad: for (int k = 0; k < stacks; k++) { for (int i = 0; i < Columns; i++) { for (int j = 0; j < Rows; j++) { GameObject Quad = GameObject.CreatePrimitive(PrimitiveType.Quad); Quad.transform.position = new Vector3(i + tempx, tempy, j + tempz); Quad.transform.eulerAngles = new Vector3(90.0f, 0, 0); Quad.transform.SetParent(GridHandle.transform); tempz += SeperationZ; } tempx += SeperationX; tempz = 0; } tempy += SeperationY; tempx = 0; } break; case GridItemsType.Custom: if (theNewObject != null) { for (int k = 0; k < stacks; k++) { for (int i = 0; i < Columns; i++) { for (int j = 0; j < Rows; j++) { GameObject OBJ = Instantiate(theNewObject, new Vector3(i + tempx, tempy, j + tempz), Quaternion.identity) as GameObject; OBJ.transform.SetParent(GridHandle.transform); tempz += SeperationZ; } tempx += SeperationX; tempz = 0; } tempy += SeperationY; tempx = 0; } } else { Debug.LogError("Kindly Assign a Prefab to the custom Prefab field"); } break; } tempx = 0; tempz = 0; tempy = 0; }