Using PositionDb Service with Layout
The TilePositionDb is a Scriptable Runtime Scriptable Object SingletonService that you can load on your own or as part of the initialization of the Layout system. 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.
It’s important to note that the ‘Hashset’ is updated during the execution of the Tilemap callback. However, handling oversize sprites, while not conceptually difficult, takes time. Hence, these are cached, and the ‘Dictionary’ is updated during a TpLib update cycle: at EndOfFrame or PostLateUpdate, depending on how the TilePlus system is set up. See the Programmer’s Guide for more information.
This works well since typically you’d be checking this during a monobehaviour update: i.e., on the next frame. If you need to update this synchronously, use ForceUpdate. Note that the execution time is indeterminate and it depends on how many cached changes have occurred (one for each affected tile).
There are several query methods that can be used to determine if a position is occupied on one or more Tilemaps.