unity-game-enginespriteunity3d-2dtools

Connecting Rule-Tiles with different textures in Unity (2D)


I am creating a 2D platformer in Unity and I am using ruletiles from the "2D Extras" add-on. I am currently trying to let two ruletiles connect with each other even if they have a different texture. Here you can see an example for the Ruletiles which I want to connect. I want to connect the lighter and darker grass tiles. They are in two seperate ruletile files.

How can I manage this?


Solution

  • There are several ways you can approach this, dependent mostly on how you visually want to connect the tiles in question. The way I generically approached this problem was by altering the ruletile so that it makes a distinction between:

    I believe the implementation is fairly trivial and can be done by editing the original ruletile.cs in the followingplaces:

    public enum Neighbor { DontCare, This, NotThis, Empty }
    

    And most importantly here:

     // When this returns true, that means that the rule does not match
        // IE this is testing if any of the rules are broken
        // If the rule matches that means that the sprite associated to the rule will be the new main sprite
        // These are the rules which are being checked if they are broken
        // This -    should only be triggered for the same tile type,
        //           which is not the case when the tile is not this
        // NotThis - should only be triggered for another tile, which is not empty
        //           IE broken when it is this or null(empty)
        // Empty -   should only be triggered when there is no tile
        //           IE broken when it is this or not null(empty)
        private bool RuleBroken(TilingRule rule, int index, TileBase tile)
        {
            return (rule.m_Neighbors[index] == TilingRule.Neighbor.This && tile != this)
                || (rule.m_Neighbors[index] == TilingRule.Neighbor.NotThis && (tile == this || tile == null))
                || (rule.m_Neighbors[index] == TilingRule.Neighbor.Empty && (tile == this || tile != null))
                ;
        }
    

    What remains is to make a change into the editor file so that you can actually toggle between 4 and not 3 enum states and then draw a graphic of choice in the grid.

    If you want to use my changed version you can find the ruletile.cs and ruletileeditor.cs in this gist.

    Note with this implementation your ruletile will need quite a lot more rules to describe each tile.