Hey folks! I’m having a hard time in Unity trying to create a new tile (not a tileset, but a single tile) system.
I created a function to read a .txt file which contains an abstract view of the stage in 0’s and 1’s. For example, a map with 4 tiles of width and 3 of height is written like this:
0010
0110
0111
The rule is simple: if 1, draws tile; if 0, draws nothing. In other words, the map may have very few tiles as much as it may have lots of it - all depends on what’s written in the .txt file. For that, I made this script:
void BuildMesh () {
StreamReader theReader = new StreamReader("test_stage.txt", Encoding.Default);
using (theReader)
{
string line;
// Gets the width of the map by number of tiles
int width_tiles = int.Parse (theReader.ReadLine());
Debug.Log ("Horizontal tiles: " + width_tiles);
// Gets the height of the map by number of tiles
int height_tiles = int.Parse (theReader.ReadLine());
Debug.Log ("Vertical tiles: " + height_tiles);
// Gets the total number of tiles in the map
int total_tiles = width_tiles * height_tiles;
Debug.Log ("Total tiles: " + total_tiles);
// Sets where the map will begin to be rendered (always zero)
int tileX = 0;
int tileY = 0;
for (int i = 0; i < height_tiles; i++){
line = theReader.ReadLine();
Debug.Log ("The first number is: " + line[0]);
if (line != null)
{
Debug.Log(line);
line = null;
}
}
// Done reading, close the reader and return true to broadcast success
theReader.Close();
}
Debug.Log ("Text file read succesfully");
// Generate the mesh data
Vector3[] vertices = new Vector3[4]; // To draw a square, there are 4 vertices
int[] triangles = new int[2 * 3]; // A square is composed by 2 triangles, each one with 3 vertices
Vector3[] normals = new Vector3[4];
vertices [0] = new Vector3 (0, 0, 10);
vertices [1] = new Vector3 (1, 0, 10);
vertices [2] = new Vector3 (0, -1, 10);
vertices [3] = new Vector3 (1, -1, 10);
triangles [0] = 0;
triangles [1] = 3;
triangles [2] = 2;
triangles [3] = 0;
triangles [4] = 1;
triangles [5] = 3;
normals [0] = Vector3.forward;
normals [1] = Vector3.forward;
normals [2] = Vector3.forward;
normals [3] = Vector3.forward;
// Create a new mesh and populate with the data
Mesh[] mesh = new Mesh[1];
mesh[0] = new Mesh (); // Calling Mesh constructor
mesh[0].vertices = vertices;
mesh[0].triangles = triangles;
mesh[0].normals = normals;
// Assign mesh to filter/renderer/collider
MeshFilter mesh_filter = GetComponent<MeshFilter> ();
MeshRenderer mesh_renderer = GetComponent<MeshRenderer> ();
MeshCollider mesh_collider = GetComponent<MeshCollider> ();
mesh_filter.mesh = mesh[0];
}
}
This script is working just fine! It’s right and it’s succesfully drawing a single tile on the screen, as shown on the image below:
Then, I tried to update it to load lots of tiles - in other words, by multidimensional array. The code is pasted below:
void BuildMesh () {
StreamReader theReader = new StreamReader("test_stage.txt", Encoding.Default);
using (theReader)
{
// Reads each line of the .txt file
string line;
// Gets the width of the map by number of tiles
int width_tiles = int.Parse (theReader.ReadLine());
Debug.Log ("Horizontal tiles: " + width_tiles);
// Gets the height of the map by number of tiles
int height_tiles = int.Parse (theReader.ReadLine());
Debug.Log ("Vertical tiles: " + height_tiles);
// Gets the total number of tiles in the map
int total_tiles = width_tiles * height_tiles;
Debug.Log ("Total tiles: " + total_tiles);
// Sets where the map will begin to be rendered (always zero)
int tileX = 0;
int tileY = 0;
// Creates multidimensional arrays of Mesh Filter, Renderer and Collider
MeshFilter[,] mesh_filter = new MeshFilter[width_tiles, height_tiles];
MeshRenderer[,] mesh_renderer = new MeshRenderer[width_tiles, height_tiles];
MeshCollider[,] mesh_collider = new MeshCollider[width_tiles, height_tiles];
// Creates a multidimensional array to store the tiles
Mesh[,] mesh_matrix = new Mesh[width_tiles, height_tiles];
// Begins to set the tiles of the map
for(int j = 0; j < height_tiles; j++){
// Reads lines from the .txt file
line = theReader.ReadLine();
for(int i = 0; i < width_tiles; i++){
// If the value got from .txt file is other than zero, draws a tile
if(line[i] != '0'){
Vector3[] vertices_tiles = new Vector3[4];
int[] triangles_tiles = new int[6];
Vector3[] normals_tiles = new Vector3[4];
vertices_tiles [0] = new Vector3 (tileX, tileY, 10);
vertices_tiles [1] = new Vector3 (tileX + 1, tileY, 10);
vertices_tiles [2] = new Vector3 (tileX, tileY - 1, 10);
vertices_tiles [3] = new Vector3 (tileX + 1, tileY - 1, 10);
triangles_tiles [0] = 0;
triangles_tiles [1] = 3;
triangles_tiles [2] = 2;
triangles_tiles [3] = 0;
triangles_tiles [4] = 1;
triangles_tiles [5] = 3;
normals_tiles [0] = Vector3.forward;
normals_tiles [1] = Vector3.forward;
normals_tiles [2] = Vector3.forward;
normals_tiles [3] = Vector3.forward;
// Creates new Mesh containing a tile
mesh_matrix[i,j] = new Mesh ();
mesh_matrix[i,j].vertices = vertices_tiles;
mesh_matrix[i,j].triangles = triangles_tiles;
mesh_matrix[i,j].normals = normals_tiles;
mesh_filter[i,j] = GetComponent<MeshFilter> ();
mesh_renderer[i,j] = GetComponent<MeshRenderer> ();
mesh_collider[i,j] = GetComponent<MeshCollider> ();
mesh_filter[i,j].mesh = mesh_matrix[i, j];
Debug.Log("Tile loaded succesfully");
}
tileX += 1;
}
tileY += 1;
line = null;
}
theReader.Close();
}
Debug.Log ("Text file read succesfully");
}
}
With this code, no tile is being drawn (not even the one that could be drawed before)! I already reviewed this code times and times but couldn’t yet find what’s wrong with it. There are no errors forbidding Unity to start, it just doesn’t draw any tile on the screen.
Can anybody please help me with this? I’m posting an aditional image below to see if it can help you (the GameObject where the script is stored).
Thanks in advance!
I can provide any more information if you want.