Tilemap, unique tiles.
I have made DoorTile script that changes tile sprite based on boolean.
using UnityEngine;
using UnityEngine.Tilemaps;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class DoorTile : Tile
{
public bool open = false;
public Sprite m_spriteOpen;
public Sprite m_spriteClosed;
public override void RefreshTile(Vector3Int location, ITilemap tilemap)
{
tilemap.RefreshTile(location);
}
public override void GetTileData(Vector3Int location, ITilemap tilemap, ref TileData tileData)
{
tileData.sprite = open ? m_spriteOpen : m_spriteClosed;
}
#if UNITY_EDITOR
[MenuItem("Assets/Create/DoorTile")]
public static void CreateDoorTile()
{
string path = EditorUtility.SaveFilePanelInProject("Save Door Tile", "Door_Tile", "Asset", "Save Door Tile", "Assets/Tiles");
if (path == "")
return;
AssetDatabase.CreateAsset(CreateInstance<DoorTile>(), path);
}
#endif
}
And I have made Door_Closed tile, put into my palette and then added 2 doors on my Tilemap. Here is how I'm Opening and Closing doors using Right-Click
private void Update()
{
mouseToGrid = worldGrid.WorldToCell(cam.ScreenToWorldPoint(Input.mousePosition) + Vector3Int.up);
mouseBlock.position = mouseToGrid;
if (Input.GetMouseButtonUp(1))
{
TileBase tile = blockTilemap.GetTile(mouseToGrid - Vector3Int.up);
if(tile is DoorTile door)
{
door.open = !door.open;
blockTilemap.RefreshTile(mouseToGrid - Vector3Int.up);
}
}
}
The problem is that if I press on a DoorTile, open
is changed for both DoorTile scripts. Not just for the one I pressed on. And even more, Tiles in my Assets are changed too. Why? Shouldn't tiles be unique? Is there any way to make it work?
Answer by CheekySparrow78 · Apr 09, 2021 at 06:28 AM
No, all same tile instances are references to the single tile, that is what allows tilemaps to be performant. If you want unique tiles you should write your own custom brush that paints clones of tiles (instead of tiles). This is how I managed to create separately opening door tiles for my tilemap. Mind you painting whole tilemap with it will seriously affect your tilemap's performance.