I want to be able to change the visibility of a Unity tilemap programmatically in C#, while keeping the tilemap enabled.
Looking at the documentation of TilemapRenderer, there is a property isVisible
, but it is read-only, so I cannot do something like the following:
Transform myTilemap = GameObject.Find("My Tilemap").transform;
myTilemap.GetComponent<TilemapRenderer>().isVisible = false;
It also does not have a Color
property, unlike for example a SpriteRenderer, so I cannot set its alpha value to zero like one could do with a sprite.
I do not want to make the tilemap no longer enabled.
How may I do this?
enabled
on the TilemapRenderer
is the property you’re looking for, the main Tilemap
object will still be enabled:
Transform myTilemap = GameObject.Find("My Tilemap").transform;
myTilemap.GetComponent<TilemapRenderer>().enabled = false;
This works for any other Renderer
, or even any other component.
I do not want to make the tilemap [or tilemap renderer] no longer enabled.
Alternatives:
Tilemap
has acolor
property, notTilemapRenderer
. You could also set that tonew Color(0, 0, 0, 0);
- Change the tilemap’s GameObject’s layer to one that is excluded by the camera’s culling mask
- Set the parent
Grid
‘senabled
property to false:tilemap.GetComponentInParent<Grid>().enabled = false
- Set
TilemapRenderer.maskInteraction
toSpriteMaskInteraction.VisibleInsideMask
(assuming you don’t use sprite masks)
3