RTS Interlude #3: Showing our updated minimaps in the menu (Unity/C#)

Let’s continue our RTS and get up-to-date minimaps for our game session saves!

This article is also available on Medium.

In the last two episodes of the Unity RTS tutorial, we saw how to use binary serialisation to save and reload our game scene data. We can now create new game sessions, play for a while, save the current game state and then restore it later. We’ve even added a new panel in our main menu to list all available game saves and reload a previous session.

But, for now, those game saves don’t show the proper minimap! At the moment, we are simply using the base minimap from the reference map itself, but it doesn’t take into account the game context: we don’t see the units, or the fog of war…

Luckily, we’ve already prepared a tool to quickly get a capture of our minimap a while ago – so we just need to call it at the right time, save a picture to the disk along with the rest of the game data and then reload it in the main menu’s load panel 🙂

Upgrading the capture tool

My minimap capture tool is almost ready, but it is still has a little limitation: it only saves images to one given directory that I hardcoded (the “Resources/MapCaptures” folder inside my application’s main data directory).

The problem is that, now, I want to store my minimaps in their respective game folders (i.e. the various directories I created based on my game session UIDs). So I have to update my MinimapCapture class to accept an optional folderPath:

This way, if I pass a folderPath manually, it will override the default behaviour and save the image in this folder!

Saving our minimap with the rest of the data

Step 2 is to actually use our updated tool to take a screenshot of the game’s minimap when we save the data 🙂

The idea is basically to modify our SaveGameData() method (in the DataHandler class) so that it takes a screenshot – but first, we’ll need to get an easy reference to the game’s folder which we’ll pass as our custom folderPath. I’ll compute it in my GameData class and even re-use it in my previous _GetFilePath() function:

Then, I’ll improve my SaveGameData() method to make use of it, too:

Note: I arbitrarily chose a 512×512 image size and the JPG format because this creates quite light files, but that should be large enough for my UI.

You can now try this out: if you create or load a game session and click the “Save” button, you’ll get a new image named “minimap.jpg” in the session’s folder. There is just a little issue: it has the minimap indicator stamped on it!

This is pretty useless outside the context of the game scene, so let’s remove it 😉

To do this, we’ll just add a static variable to our MinimapManager and check if it’s enabled before doing or post-rendering. If it’s disabled, then we’ll disable the post-rendering altogether.

Finally, in my SaveGameData() function, I’ll toggle this static flag off and on around my minimap capture call:

And there you have it: we know have a clean image of the game’s minimap in its current state, safely stored inside the game session’s folder…

Loading back the image in the main menu

The final thing we need to do is actually show this image in our main menu, in the load panel, instead of the map’s base minimap. This means that we have to:

  1. load the image file from the disk (into a Texture2D)
  2. create a Sprite from the image
  3. assign this Sprite to the list item in the main menu load panel

To load my image into a Texture2D and generate the matching Sprite, I’ll add a new function in my Utils class – this static method will load the data as a byte array thanks to the LoadImage() Unity function:

And now, all that’s left to do is update my MainMenuManager class to call this new tool:

And we’re done! We can now run our game and, in the main menu, open the load panel to check out our little minimaps 🙂

Conclusion

In the past few episodes, we’ve worked on our menu-to-game workflow and we’ve seen how to make our game sessions reloadable. Now, we can even check out the minimap of the game in its current state in our main menu! 🙂

Next week, we’ll do a global review of our project, refactor or improve some mechanics and fix a few bugs I or other readers discovered along the way…

Leave a Reply

Your email address will not be published.