I am trying to create a tile map by using a script. I know there are different other methods to do it but its for learning purposes. It would really be cool if it works, so I can put it in my guitar hero game or rpg.
The script created my tilemap too big. It spawned extra tiles, which made my tile map too big. If I put one different tile on the tile map, it made like 6 extra of that tile. My tile map layer is 128x128 px and the script made it like 10x times as big. My default tiles are 32x32px.
This script basically reads the color per unit of your map layer png file. It spawns a tile based on the color to whatever you input it.
I recommend you to save your project before trying this out though. It might break unity if you mistype something.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class LevelManager : MonoBehaviour
{
[SerializeField]
private Transform map;
[SerializeField]
private Texture2D[] mapData;
[SerializeField]
private MapElement[] mapElements;
[SerializeField]
private Sprite defaultTile;
private Vector3 WorldStartPos
{
get
{
return Camera.main.ScreenToWorldPoint(new Vector3(0, 0));
}
}
// Start is called before the first frame update
void Start()
{
GenerateMap();
}
private void GenerateMap()
{
int height = mapData[0].height;
int width = mapData[0].width;
for (int i = 0; i < mapData.Length; i++)
{
for (int x = 0; x < mapData[i].width; x++)
{
for (int y = 0; y < mapData[i].height; y++)
{
Color c = mapData[i].GetPixel(x, y); //Gets the color of the current pixel
MapElement newElement = Array.Find(mapElements, e => e.MyColor == c);
if (newElement != null)
{
float xPos = WorldStartPos.x + (defaultTile.bounds.size.x * x);
float yPos = WorldStartPos.y + (defaultTile.bounds.size.y * y);
GameObject go = Instantiate(newElement.MyElementPrefab);
go.transform.position = new Vector2(xPos, yPos);
if (newElement.MyTileTag == "Tree")
{
go.GetComponent<SpriteRenderer>().sortingOrder = height*2 - y*2;
}
go.transform.parent = map;
}
}
}
}
}
}
[Serializable]
public class MapElement
{
[SerializeField]
private string tileTag;
[SerializeField]
private Color color;
[SerializeField]
private GameObject elementPrefab;
public GameObject MyElementPrefab
{
get
{
return elementPrefab;
}
}
public Color MyColor
{
get
{
return color;
}
}
public string MyTileTag
{
get
{
return tileTag;
}
}
}