public Tilemap(string csvFile, string textureName)
{
string mapBody = File.ReadAllText(csvFile).TrimEnd(new char[] { '\n' });
string[] rows = mapBody.Split('\n');
int index = 0;
height = rows.Length;
foreach (string row in rows)
{
string[] cols = row.Split(',');
width = cols.Length;
if (this.map == null)
{
this.map = new int[cols.Length * rows.Length];
}
foreach (string col in cols)
{
this.map[index] = int.Parse(col);
index++;
}
}
this.mapMesh = new Mesh();
List<float> vertices = new List<float>();
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
vertices.Add(x * 70);
vertices.Add(y * 70);
vertices.Add(x * 70);
vertices.Add((y + 1) * 70);
vertices.Add((x + 1) * 70);
vertices.Add(y * 70);
vertices.Add((x + 1) * 70);
vertices.Add(y * 70);
vertices.Add((x + 1) * 70);
vertices.Add((y + 1) * 70);
vertices.Add(x * 70);
vertices.Add((y + 1) * 70);
}
}
this.mapMesh.v = vertices.ToArray();
this.mapMesh.uv = new float[this.mapMesh.v.Length];
this.mapMesh.Update();
this.tileSheet = new Texture(textureName);
// use nearest mode for sampling
this.tileSheet.SetNearest();
}