6
\$\begingroup\$

I am using Unity's RuleTiles to automatically change sprites of adjacent similar blocks.

For example, when a Dirt tile notices there are two Dirt tiles to the left and the right of it, it will change texture, like this:

enter image description here

I would also like to do this with walls. However, I plan to have many types of walls (Wood, Stone, Metal). How would I make different types of walls still affect each other?

Imagine that Dirt is now a Wood wall. If I place a Stone wall to the left, and a Metal wall to the right, I'd still like for it to apply a texture change.

Is it possible to make different types of rule tiles interact with each other?

\$\endgroup\$
2
  • \$\begingroup\$ is it possible to use inheritance. So you can have a base class Wall then the Wood, Stone, Metal etc can extend that class. Then you just check if it is type Wall \$\endgroup\$ Jun 8, 2021 at 15:43
  • \$\begingroup\$ Perhaps but this seems like it would require coding an entire system. Unity RuleTiles already do this, I'm just wondering if it's possible to extend that functionality to connect different RuleTiles. \$\endgroup\$
    – caleidon
    Jun 8, 2021 at 15:56

1 Answer 1

2
\$\begingroup\$

For my purposes, this example from Unity's Github worked flawlessly (Source):

public class ExampleSiblingRuleTile : RuleTile
{

    public enum SiblingGroup
    {
        Poles,
        Terrain,
    }
    public SiblingGroup siblingGroup;

    public override bool RuleMatch(int neighbor, TileBase other)
    {
        if (other is RuleOverrideTile)
            other = (other as RuleOverrideTile).m_InstanceTile;

        switch (neighbor)
        {
            case TilingRule.Neighbor.This:
                {
                    return other is ExampleSiblingRuleTile
                        && (other as ExampleSiblingRuleTile).siblingGroup == this.siblingGroup;
                }
            case TilingRule.Neighbor.NotThis:
                {
                    return !(other is ExampleSiblingRuleTile
                        && (other as ExampleSiblingRuleTile).siblingGroup == this.siblingGroup);
                }
        }

        return base.RuleMatch(neighbor, other);
    }
}


In my case, I'd have a single value in the SiblingGroup enum, Wall. Then I'd create multiple instances of the ExampleSiblingRuleTile, and assign the Wall value to them. Now they will all react to neighbor tiles and update their textures accordingly.

\$\endgroup\$
1
  • \$\begingroup\$ It's still a good idea to include the source link for credit and further reading, so you don't have to delete that from your answer. The goal is to just not rely solely on links without any explanation in the text of the answer. \$\endgroup\$
    – DMGregory
    Jun 25, 2021 at 11:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .