A couple of weeks ago, we improved our UI and displayed more info on the buildings (with an info panel and healthbars).

Today, we’re going to continue on this topic and improve our selection mechanism — we’ll do three things:

  • display the list of currently selected units: in order to better remember what we’ve selected, even if it is off-screen, let’s list the units at the bottom of the screen as little blocks!
  • create selection groups: this is a nice way of switching between your units that many RTS provide
  • show up info on the active selected unit: it would be nice to have some data and specific actions for a unit when we select unit

Displaying the list of currently selected units

In a previous tutorial, we set up a basic selection system that allows to select units by either dragging a box around them on the screen or by clicking on them directly. We see which units are selected because they have a selection circle and a healthbar, but it would be nice to have a complete list of all the units currently selected somewhere on screen. In particular, this will be very useful when we start moving the camera and possibly have selected units that are no longer in the viewport.

To avoid having too many blocks in this list, we will group units per unit type; so for example if we select 1 House and 2 Towers, we will have 2 items: one for the House with a count of 1 and one for the Towers with a count of 2. Eventually we will use the unit icons — for now, we’ll simply add a text with the name of the unit type:

To implement this, we have to:

  • add some UI elements in our Canvas, in particular a parent panel with a GridLayout to easily stack the items and a prefab to display the selected units as small blocks
  • update our UIManager and UnitManager scripts to integrate this new feature to the current selection system

The first part is very similar to what we’ve done in previous tutorials and it’s quite dependent on the feel you want for your game, so I won’t give explicit details about this here. The important thing is just that my SelectedUnitDisplay prefab has two children with Text components on them: the “Count” and the “Name” to show respectively the current amount of selected instances and the unit type.

#games #development #unity #tutorial #csharp

Making a RTS game #8: Boosting our selection feature (Unity/C#)
5.85 GEEK