1
\$\begingroup\$

I have made a map via TilePallete tool.

Now I want to store information in certain tile. How I can do this?

ForEx:

I'm hittin a certain tile and I need it to have info about health remaining. For first hit I change a tile. For second I set it to null.

\$\endgroup\$

3 Answers 3

2
\$\begingroup\$

One approach could be to do that with scriptable tiles.

In order to do that, you need to create a new C# class which inherits from TileBase. You can now implement any functionality you need in this class, like having a health property. A useful method to override is the method bool GetTileData(Vector3Int location, ITilemap tilemap, ref TileData tileData). If you want to change the appearance of the tile based on something, then you can do that by changing the tileData structure passed to the class, specifically the field sprite which you could set depending on the current health.

Unfortunately it is not that straight-forward to create a tile asset which uses that script. You have to do that through an editor script which calls AssetDatabase.CreateAsset(ScriptableObject.CreateInstance<YourScriptableTileClass>(), path);. This official example shows how that can look (starts at #if UNITY_EDITOR).

But now you have another problem: Detecting that the tile is hit. How to do that depends on what "hitting a tile" actually means in the context of your game, but I assume that it would somehow involve having a game object with a Trigger Collider and an attached script which implements OnTriggerEnter and carries a reference to the tile it is supposed to change.

\$\endgroup\$
1
  • \$\begingroup\$ Unfortunately every instance of that tile on the map will have exactly the same value for health as it's based on the tile type not instances of it on the map making it useless for most cases. \$\endgroup\$
    – John Stock
    Apr 4, 2023 at 16:57
2
\$\begingroup\$

Unity have that tool in their 2d-extras Github repository. The tool is called GridInformation. It's basically a dictionary that can hold Integer, String, Float, Double, UnityObject or Color values and uses Vector3Int(i.e. cell position) as a key.

\$\endgroup\$
1
\$\begingroup\$

What you could do, which is think is the best solution for tiles & information is to have a dictionary containing a class thats connected to the tile index. This will behave like a lookup table in the "general terms".

some psudo code.

int key = TilePositionY * TileMapWidth + TilePositionX;
if( TileLookupDictionary.ContainsKey( key ) == true )
{
   MyTileDataClass = TileLookupDictionary[key];
}
\$\endgroup\$
1
  • \$\begingroup\$ Bad solution. The whole point of using a map editor is that it's GUI driven. This would mean you have to progmatically associate the data to the tile. Solution above is much better \$\endgroup\$
    – John Stock
    Apr 4, 2023 at 12:35

You must log in to answer this question.

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