π«΅Unit Selector
Learn more about unit selector component.
Overview
The UnitSelector is the main component required for the selection system to operate effectively. It is responsible for:
Monitoring selection inputs.
Updating the selection area based on user actions.
Performing physics Raycasts for Click and Hover features.
Managing active selections through the required ActiveSelections component.
Interacting with the unit manager to access selectable units.
This component is implemented as a singleton and can be accessed using:
Key Properties
IsSelectionEnabled: Indicates whether the selection system is currently active.
Unit Manager: The selection system relies on the unit manager to provide a list of selectable units. Without a valid unit manager or if the list of selectables is empty, only mouse click and hover interactions will be functional. See Unit Managementfor more details.
Selection Area: The system requires a defined selection area to operate. If this is not set, enabling drag selection will result in errors. Disabling the selection and highlight features for drag interactions will prevent these errors. See #sSelection Areafor more details.
Configuration
The configuration of the UnitSelector system is crucial for tailoring its behaviour to the specific needs of your game. Below are detailed descriptions of each configuration property available:
Selectable Layer Mask: This property defines the layer mask used to detect game objects for selection purposes. Setting this correctly ensures that only the intended objects are selectable within the game.
Drag Selection Enabled: Determines whether units are selected when the mouse is released after dragging. Enabling this feature allows users to select multiple units quickly by dragging over them.
Drag Highlight Enabled: Specifies whether units should be highlighted during a drag operation, before the mouse button is released. This provides visual feedback on which units are about to be selected.
Modifying Current Selection Enabled: Allows for the addition to or removal from the current selection using mouse clicks or by dragging. This option gives players greater control over their unit selections during gameplay.
Double Click Selection Enabled: When enabled, double-clicking on an object will trigger the selection of all visible selectable objects. This is a quick way for players to select all units within their view.
Ignore When Over UI: This setting ensures that clicks or touches over UI components are ignored, preventing the selection system from responding when UI elements are interacted with. It is vital for games where the UI overlaps the gameplay area. For this to work Unity's EventSystem component must be present in the scene.
Sort Selection by Distance: If enabled, this will sort the selected units by their distance from the drag start position. This feature can impact performance and should be used only when needed. For example when active selection has a limit or when sorted units are desired output of selection process.
Raycast Type & Max. Raycast Hits: You can specify if click and hover features use single raycast (which ever is returned by Unity Physics system) or finding all potential hits (limited by max hits) and picking the furthest or the nearest hit. Impact on performance is relatively small, depending on max. selection distance and max. number of raycast hits.
Max Selection Distance: Defines the maximum allowable distance for selection actions, including clicks, hovers, or drags. This limit helps prevent players from selecting units that are too far away to be relevant to current gameplay situations.
Mouse Drag Threshold: Specifies precision, the minimum amount of mouse movement required to trigger a drag selection. This prevents unintentional drags when players intend only to click.
Highlight On Mouse Hover: Enables the highlighting of units when the mouse hovers over them. This feature is useful for games that require clear identification of units under the cursor, providing immediate visual feedback to the player.
These configuration settings allow you to finely tune how selection behaves in your game, ensuring both responsiveness and intuitiveness in player interactions. Adjusting these settings will help match the selection dynamics to the specific gameplay style and mechanics of your game.
Public Interface
SetInputControl method sets the input reference in case it is not already referenced from Editor or attached to the game object
SetCamera method updates the camera selector and its components use
CancelSelection method interrupts currently active selection and deselects and selected units.
SetSelectionEnabled method enables or disables the functionality of the selection system
Last updated