Is there any way to directly reference a tile palette in code and access the tiles by index?

I want to have use a defined tile palette as I loop over the tilemap and place tiles at “random”.
Something like this?

System.Random rand = new System.Random();
for (int y = 0; y <= maxY; y++)
{
    for (int x = 0; x <= maxX; x++)
    {
        this.map.SetTile(new Vector3Int(x, y, 0), this.palette[(rand.Next(0,75)]);
    }
}

Is there any way to access the tile palette in this way? pallette[<index>]

The palette won’t exist at runtime… Internally the palette is just a scene with a tile map loaded. You could probably read it from the palette asset file. an alternative option is to populate your scene tilemap in the editor before run.

There might be, but I highly suspect you’re wasting your time even trying.
Somewhere, sometime, you’re going to need to say what tile means what. At that point you’ll need to create a reference link between the tile itself and the properties, so you might as well just do it now. Create a master tile list and use that instead. A bit of grunt work to be sure, but ultimately you’ll have to do it anyway.

1 Like