I’m making a tile-based game. My intention is to create map layouts in Photoshop, where each pixel represents a tile. The color of each pixel in the map image determines which tile goes in each spot in the game. I’m currently trying to write the code to import the map images and use them to populate the game board.
In the code below, “MapColor” represents the pixel index of its tile type - a solid red pixel in the map png will correspond with a wall tile on the game board, for example.
Here’s the code snippet I’m having trouble with:
List<Tile>TileTypes;
protected class Tile
{
public static Dictionary<string, int> typeDic = new Dictionary<string, int>()
{
{"Grass", 0 },
{"Sand", 1 },
{"Mountain", 2 },
{"Water", 3 },
{"Wall", 4 }
};
public bool Is_Walkable { get; set; }
public bool Is_Swimmable { get; set; }
public bool Is_Flyable { get; set; }
public Color32 MapColor { get; set; }
}
void InitTiles()
{
//Create blank tile types up to the number of available tile types.
Tile blank = new Tile();
for(int i = 0; i<tileCount;i++)
{
TileTypes.Add(blank);
}
//Populate tile variables
TileTypes[Tile.typeDic["Water"]].Is_Walkable = false;
TileTypes[Tile.typeDic["Water"]].Is_Swimmable = true;
TileTypes[Tile.typeDic["Water"]].Is_Flyable = true;
TileTypes[Tile.typeDic["Water"]].MapColor = new Color32(0,0,255,255);
TileTypes[Tile.typeDic["Sand"]].Is_Walkable = true;
TileTypes[Tile.typeDic["Sand"]].Is_Swimmable = false;
TileTypes[Tile.typeDic["Sand"]].Is_Flyable = true;
TileTypes[Tile.typeDic["Sand"]].MapColor = new Color32(255, 255, 0, 255);
TileTypes[Tile.typeDic["Grass"]].Is_Walkable = true;
TileTypes[Tile.typeDic["Grass"]].Is_Swimmable = false;
TileTypes[Tile.typeDic["Grass"]].Is_Flyable = true;
TileTypes[Tile.typeDic["Grass"]].MapColor = new Color32(0, 255, 0, 255);
TileTypes[Tile.typeDic["Mountain"]].Is_Walkable = true;
TileTypes[Tile.typeDic["Mountain"]].Is_Swimmable = false;
TileTypes[Tile.typeDic["Mountain"]].Is_Flyable = true;
TileTypes[Tile.typeDic["Mountain"]].MapColor = new Color32(255, 255, 255, 255);
TileTypes[Tile.typeDic["Wall"]].Is_Walkable = false;
TileTypes[Tile.typeDic["Wall"]].Is_Swimmable = false;
TileTypes[Tile.typeDic["Wall"]].Is_Flyable = false;
TileTypes[Tile.typeDic["Wall"]].MapColor = new Color32(255, 0, 0, 255);
}
Later on, I’ve set up a function which performs:
for (int k = 0; k < TileTypes.Count; k++)
{
Debug.Log(TileTypes[k].MapColor.ToString());
}
The result in the debugger is:
RGBA(255, 0, 0, 255)
UnityEngine.Debug:Log(Object)
RGBA(255, 0, 0, 255)
UnityEngine.Debug:Log(Object)
RGBA(255, 0, 0, 255)
UnityEngine.Debug:Log(Object)
RGBA(255, 0, 0, 255)
UnityEngine.Debug:Log(Object)
RGBA(255, 0, 0, 255)
UnityEngine.Debug:Log(Object)
In essence, every tile thinks that it’s represented by a solid red pixel, even though I tried to explicitly set them each up to be represented by a different pixel color. For this reason, the only tiles which correctly populate on my game map are the ones which correspond with wall tiles.
I’ve been looking at this for over an hour, and have no idea where I’ve gone wrong. I could use some help.
It probably won’t help any, but I’ve attached the map image I’m trying to import for posterity.
Thanks in advance.
![]()