Using Unity Rule Tiles instead of regular tiles when importing from Tiled

Hello!

I’ve been using Tiled map editor for a while and really like it’s workflow. It makes way more sense to me to design my game’s map (a simple 2D top down maze) in Tiled and then just have it import into unity (using supertiled2unity). I like that workflow and it’s scalability for when I want to add more layers later.

The problem is that I need my tiles to update when they get removed, which happens a lot in my game. But when I import maps from Tiled, the prefabs it creates use regular tiles that don’t change when their neighbors do.

Is it possible to import my map from Tiled BUT instead of using the tileset I used to draw in Tiled, instead the prefab has Unity Rule Tiles everywhere?

This might be a dumb question so if you see a better solution, I’m all ears, but I need my map to update correctly in-game when sections of the walls get destroyed.

Thanks!

1 Like

hi @greg , did you ever figure this out? I have the same need, but I’m starting to think that Tiled is made for static map exporting. Not sure tho.

Hey there,

I kind of hacked a solution together. Not sure it’s very elegant, but it’s just an importer so doesn’t need to be very performant I guess.

Basically, SuperTiled2Unity has a very powerful Custom Importer option that lets you write your own importer. I leveraged that to do what I needed. The following steps should get you close:

  1. Copy your desired Rule Tile asset to a “Resources” folder in your project (under the Assets directory) - this is required to use Resources.Load() later on
  2. Create a custom importer class (this will NOT be a Monobehavior) it will extend CustomTmxImporter
  3. Override the TmxAssetImported(TmxAssetImportedArgs args) method with code to replace tiles with your Rule Tile (example below)
  4. Set up your imported asset to use the custom importer you just created.

Here’s my class. Note that I was acutally using an AdvancedRuleTile, but this can be changed to just RuleTile or whatever class your tile is.

public class RuleTileImporter : CustomTmxImporter {

    public override void TmxAssetImported(TmxAssetImportedArgs args) {

        // Find layer with desired tag
        var basicWalls = args.ImportedSuperMap
             .GetComponentsInChildren<SuperTileLayer>()
             .FirstOrDefault(layer => layer.name == "BasicWalls");
        
        
        if (basicWalls != null) {
            // get the Tilemap object from this layer
            var tilemap = basicWalls.GetComponentsInChildren<Tilemap>().First();
            
            // Iterate through all tiles in the tilemap
            foreach (var position in tilemap.cellBounds.allPositionsWithin) {
                var tile = tilemap.GetTile(position);
                if (tile != null) {
                    // Replace the tile with the RuleTile scriptable object
                    var newTile = Resources.Load<AdvancedRuleTile>("BasicWallsRuleTile");
        
                    if (newTile != null) {
                        tilemap.SetTile(position, newTile);
                    }
                }
            }
        }
    }
}

So once that’s created, if you go click on the prefab that SuperTiled2Unity creates, in the inspector, there should be a “Custom Importer Settings” section with a dropdown. That dropdown should now include your new class (mine is called RuleTileImporter). Select that and then reimport your asset.

Again, not the most elegant solution but it seems to work. I should mention that I’m not sure whether this retains any other tile info such as collisions and such since my tiled map has separate collision layers (that I’m still working on importing the way I want) so it’s not an issue for me. But if you have a bit of programming knowledge, the importer can be modified to an insane degree using the custom importer.

Let me know if it works for you! There’s not many examples of custom importers out there, so I’m happy to get mine out into the world.