Managing Hidden Area In Tilemap Unity

Started by
2 comments, last by Shaarigan 4 years, 3 months ago

So I am trying to figure out the best way to handle hidden areas in a tilemap and how to manage them in the Unity editor.

Lets say I have a tilemap where I want a bunch of hidden areas that are only visible / accessible after certain events or conditions have been met (my map size is 512x512 so there could be a lot of these). My first idea was to just draw the main tile map with all the hidden areas exposed and then draw a tilemap per hidden area that basically covers it up. Then when the event / condition triggers to reveal the hidden area, I just remove that tilemap. This works but when I get to the point where I have 30, 40, 50+ tilemaps in this one scene, the performance get kinda bad (even if these extra tilemap maps are sized to just be big enough to cover the area needed and not all 512x512).

The only other option I can think of is for each tile that is part of a hidden area, store some meta data (like the id to indicates the hidden area and the tile id of what the tile should be when the hidden area is revealed) however this pattern makes managing of the hidden areas kinda of a pain.

I am just wondering if there are any patterns I might not be thinking of to make managing something like this easier before I go down the route of manually managing it (and given the amount I intend to use this, is kinda toes the line of probably being more effort in creating the custom editor vs managing it manually but not sure right now).

Advertisement

You can make a kind of an editor. For example it will be empty tilemap with HiddenAreas.cs script attached where you draw black rectangles on area then you press Right Mouse Button on tilemap game object and there will be a method from your HiddenAreas class that exports black rectangles to some structure.

class HiddenAreas
{
	[System.Serializable]
	public struct ExportedArea
	{
		public string name;
		public Vector2Int[] points; // can be list as well
	}

	public ExportedArea[] areas; // can be list as well

	[ContextMenu("Export Current Zone")]
	public void ExportCurrentArea()
	{
		// add new array item to areas and fill new array element with points and clear tiles if needed
		// add name by hand after inspector was populated by a new element
	}
}

then you can easily read back all data when game load to single tilemap overlay and show/hide any area. You must mark prefab as dirty after adding new elements from script to properly serialize and save all data to prefab file or scene.

You could also use a shader for this just blending anything to black that shouldn't be visible. I think to achieve something like “fog of war” there will be more work to do but simply erasing content to black should fit just fine

This topic is closed to new replies.

Advertisement