Skip to main content

Using PositionDb Service with Layout

The TilePositionDb is a Scriptable Runtime Service: it’s an optional feature that you can use to keep track of what positions are occupied on one or more Tilemaps. The intent is to make collision detection easier in certain situations; typically, turn-based games.

Read more about it here.

Basically:

TilePositionDb hooks into a Tilemap callback and creates two internal datasets:

  • A HashSet of occupied positions.
  • A Dictionary of Tilemaps with sprite sizes for each position where the sprite size is more than one unit.

The Dictionary is only used when you specify that oversize sprites should be detected. Why is this important? If you have tiles with sprites that are statically larger than 1x1 or if the sprites are changed at runtime, then this feature ensures that you can detect all possible positions that the sprite occupies.

For example, if you use the Tileplus Tweener, sprite sizes and positions are constantly changing. The PositionDb will automatically update its internal data as these values change.

One limitation may occur if you’re using the ‘Matrix’ tweening target, where you can change transform position, rotation, and scale in one tween. It’s possible to do all sorts of weird transformations and the sprite size calculations may not be able to handle every situation.