Hi guys, I have a few hundred tiles that I created from sprites in a palette. I'm trying to figure out how to load these at runtime and apply them to a TileMap using C# for a 2D map. So something like this codewise: Code (CSharp): for(x=0; x<200; x++) { for(y=0; y<200; y++) { map.SetTile(new Vector3Int(x, y, 0), GetTileForMap(world[x,y])); } } Tile GetTIleForMap(int tileType) { // fetch a tile asset named "tiles_" + tileType return tile; } The map is a reference to my TileMap in the scene. world is a 2D array holding the type of ground at that x, y location (grass, water, etc.) built from a spritesheet of tiles that was used to generate the tile palette. All tile assets (created when I dragged my spritesheet into the palette) are "tile_x.asset" files in a folder under Assets/Tiles. I thought I could use Resource.Load on this but since they're not in a resources folder I can't do that. Otherwise I could do something like Resource.Load<Tile>("tiles_" + tileType) right? (only loading it once so I don't load the same tile asset over and over again). That would require me to move all the tile.asset files into a resources folder. Or is there something I'm missing like a way to reference a tile asset in the palette using it's x, y value or index? Thanks
I solved this by creating a prefab that has a script that keeps references to all the different tiles I use on a given level. I just drag on the different tiles to the correct TileBase property in the Inspector for this prefab. This prefab gets instantiated on each level before I start call SetTile. Mine looks something like this. Code (CSharp): public class ThemeResources : MonoBehaviour { [Header("Slopes")] public TileBase slopeLeftA; public TileBase slopeLeftB; public TileBase slopeRightB; public TileBase slopeRightA; public TileBase angleLeft; public TileBase angleRight; [Header("Bounds")] public TileBase[] blocks; } You could just add a public function to this class to access the different tiles like this: Code (CSharp): TileBase GetTileForMap(int tileType) { return blocks[tileType]; } I found this gave me a lot of flexibility to write custom functions that for things like grabbing random blocks.