Unit Selection
  • Welcome
    • πŸ‘‹Hello
    • πŸ—ΊοΈRoadmap
    • ⛑️Support & Community
    • πŸŽ“Changelog
  • Getting started
    • πŸ”ŒImport package
    • πŸ› οΈAdd selection to scene
    • 🦲Add selection to units
    • β›³Add visuals to units
  • How it works
    • 🎯Active Selections
    • 🫡Unit Selector
    • πŸ”’Quick Access Selector
    • ⬛Selection Area
      • πŸ”²2D Rectangle
      • 🧊3D Cube
    • πŸ’ Selectable Unit
      • πŸ“Selection Bounds
      • β­•Selection Indicator
    • Selectable Group Unit
    • ⌨️Input Controls
    • πŸ‘·β€β™€οΈUnit Management
Powered by GitBook
On this page
  • Overview
  • Configuration
  • Customisation
  1. How it works

Selectable Group Unit

More about the behaviour and implementation of a group as selectable unit.

PreviousSelection IndicatorNextInput Controls

Last updated 7 months ago

Overview

There are 2 components required to use the functionality for group selection without any coding. This would be the SelectableGroupUnit (for every moving unit) and SelectableGroup (for group of units). These components extend the Selectable Unit and provide the same functionality with few additions to achieve smooth group selection.

Both selectable group and selectable group unit support selection indicator visuals. Example would be for individual units to show selection circles and group itself to show floating text, health bar or other additional information about the selected or highlighted group unit.

Configuration

SelectableGroupUnit has all the properties already present on SelectableUnit. Additionally it implements ISelectableGroupUnit interface which provides a reference to the selectable group to which each individual unit belongs.

  • Group reference must be set for system to be able to match units and their groups together. This can be done within the Editor or dynamically using SetGroup method on group unit component.

  • As long as group reference is present, unit will automatically be added or removed from the group. There should be no need to manually modify it's list.

SelectableGroup has again all the properties already present on SelectableUnit. Additionally it implements ISelectableGroup and additionally provides a list of units within the group.

  • Group units is a dynamic list of units within the group.

  • Destroy group when emptied flag will specify if group itself should be destroyed when the last unit from it's list is removed. Group component will be disabled if this is set to false.

  • OnAllGroupUnitsRemoved is Unity Event invoked when all units were removed from the list.

  • Center selection indicator specifies if the indicator is updated every frame (when selected or highlighted) to the calculated center of its unit to be used as some floating GUI.

Customisation

Communication between units and selection system is done with ISelectable. The same as group components, these interfaces are the extension of ISelectable. ISelectableGroup and ISelectableGroupUnit can be used to completely customise behaviour with your own implementations that work with the selection system.

Group example:

class MyGroup : MonoBehaviour, ISelectableGroup // ISelectableGroup is key here
{
    public List<ISelectableGroupUnit> GroupUnits { get; private set; } = new();

    public bool IsSelected { get; private set; } = false;

    public bool IsHighlighted { get; private set; } = false;

    public void RemoveGroupUnit(ISelectableGroupUnit groupUnit)
        => GroupUnits.Remove(groupUnit);

    public void AddGroupUnit(ISelectableGroupUnit groupUnit)
        => GroupUnits.Add(groupUnit);

    public void Select()
    {
        // Update all units states and their visuals if selecting a group
        // should show selectors on all its units.
        IsSelected = true;
    }

    public void Deselect()
    {
        IsSelected = false;

        // Update...
    }

    public void Highlight()
    {
        IsHighlighted = true;

        // Update
    }

    public void Unhighlight()
    {
        IsHighlighted = false;

        // Update
    }
}

Group Unit example:

class MyGroupUnit : MonoBehaviour, ISelectableGroupUnit // ISelectableGroupUnit is key here
{
    public bool IsSelected { get; private set; } = false;

    public bool IsHighlighted { get; private set; } = false;

    // Manage this reference, it is used for matching unit to the group.
    public ISelectableGroup Group { get; private set; } = null;

    public void Select()
    {
        // Update all units states and their visuals if selecting a group
        // should show selectors on all its units.
        IsSelected = true;
    }

    public void Deselect()
    {
        IsSelected = false;

        // Update...
    }

    public void Highlight()
    {
        IsHighlighted = true;

        // Update
    }

    public void Unhighlight()
    {
        IsHighlighted = false;

        // Update
    }

    public void SetGroup(ISelectableGroup group)
    {
        Group = group;

        // Add unit to the group if it is not present yet.
    }
}

Group selection Demo scene
Selectable group unit component
Selectable group component