Create a custom rule tile
Create a custom rule tile that changes what happens when a tile checks its neighbors.
Create a custom rule tile class
Follow these steps:
Create an empty C# script that inherits from
UnityEngine.Tilemaps.Create a class that inherits from
RuleTile.Add
[CreateAssetMenu]above the class to add an item in the main menu that instantiates the tile.For example:
using UnityEngine; using UnityEngine.Tilemaps; [CreateAssetMenu] public class CustomRuleTile : RuleTile { }
Add a custom property
Add a custom property to the top of the RuleTile class. The property appears in the Inspector window of the rule tile.
For example:
public class MyTile : RuleTile {
public bool isWater;
}
Create a custom matching rule
Create a new type of rule for the grid in the Rule Tile Inspector window.
Inherit
RuleTile<CustomRuleTile.Neighbor>instead ofRuleTile.Create a subclass that inherits from
RuleTile.TilingRule, and add a new rule number. Unity uses 0, 1, and 2 for the default rules, so custom rules start at 3. For example:public class Neighbor : RuleTile.TilingRule.Neighbor { public const int tileIsNull = 3; }When you create an instance of the new rule tile, select 3 as the rule when you click the grid in the Tiling Rules section of the Inspector window.
Override the
RuleMatchmethod to define the new rule.RuleMatchruns every time Unity checks the neighbor of a tile. Use aswitchstatement to returntrueorfalsedepending on a condition. For example:public override bool RuleMatch(int neighbor, TileBase tile) { switch (neighbor) { case 3: if (tile == null) return true; else return false; } return base.RuleMatch(neighbor, tile); }
For a full example, from the main menu, select Assets > Create > 2D > Tiles > Custom Rule Tile Script.
Check for tiles from a list
To check for tiles from a list, follow these steps:
In your custom tile class, create a
ListofTileBaseobjects that contains the tiles you want to check for. For example:public List<TileBase> tilesToCheckFor = new List<TileBase>();When you define the rule, use
Containsto check if the neighbor tile is in the list. For example:public override bool RuleMatch(int neighbor, TileBase tile) { switch (neighbor) { case Neighbor.IsInList: return tilesToCheckFor.Contains(tile); } return base.RuleMatch(neighbor, tile); }After you create an instance of the custom rule tile, add tiles to the list in the Inspector window.
Additional resources
- RuleTile API