Unity-Technologies / 2d-extras

Fun 2D Stuff that we'd like to share!
Other
1.55k stars 343 forks source link

Can you update a rule tile appearance in a script? #311

Closed jatkinson16 closed 3 years ago

jatkinson16 commented 3 years ago

I've just started using rule tiles in Unity 2019.4 and I'm working on a procedurally generated tilemap that uses rule tiles. It use the standard tilemaps.SetTile(new Vector3Int(x, y, 0), ruleTile); to assign the tiles to tilemap positions but when the game runs the tiles don't adapt to those around them.

Is it possible to manually force a rule tile to recheck the tiles around it and update its sprite accordingly? I'm planning on having tiles get removed from tilemaps due to player actions in the future so that would also benefit from the answer.

ChuanXin-Unity commented 3 years ago

Hi, when you call SetTile with the RuleTile, the RuleTile should update all the positions around it, as defined in RuleTile.RefreshTile.

Could you share an example of where the RuleTile does not work and what Tiles are placed around the RuleTile?

Is it possible to manually force a rule tile to recheck the tiles around it and update its sprite accordingly? I'm planning on having tiles get removed from tilemaps due to player actions in the future so that would also benefit from the answer.

You can call Tilemap.RefreshTile with the given positions to update those positions if you need to. However, the RuleTile system should update the Tiles automatically for you when you call SetTile.

jatkinson16 commented 3 years ago

The rule tile works as normal in the tile palette image

But this is the outcome when the tiles are placed in a script(with the script looping from 0,0 to 56,32) image

I can assume that initially the tiles won't appear as they should when the script is still going through each coordinate placing tiles but their appearance ends up as the top corner tile regardless. image

That bottom left tile in the runtime image surely fulfils the condition for this rule tile, not the one it appears as image

ChuanXin-Unity commented 3 years ago

The RuleTile should update itself whenever the neighbour is placed next to it, just like what you see in the Tile Palette.

Would it be possible to share the full configuration for the RuleTile as well as the script for placing the Tiles?

If you could, could you try importing the Dungeon RuleTile sample from the Unity Package Manager (or copy the assets from Samples~/DungeonRuleTile to your project) and see if you get the same behaviour when using the script for placing the Tiles with the Dungeon RuleTile? image

jatkinson16 commented 3 years ago

I managed to figure it out thanks to checking with the sample rule tile. It was an issue on my end with how I set up different spritesheets, I removed the method call and the rule tiles were drawn properly. So it was just my fault in the end. Thanks for the help regardless, this package is a great tool for 2D games! image