Selection Box
A visual indicator that automatically follows selected UI objects via EventSystem
Overview
Selection Box is an animated visual indicator that automatically tracks and highlights the currently selected UI element in your canvas. It responds to Unity's EventSystem selection changes, smoothly animating position, size, fade effects, and appearance with customizable curves and override settings.
The component handles automatic detection of selected objects, smooth animated transitions, fade in/out effects, per-object appearance customization via SelectionBoxOverride, and continuous tracking of moving UI elements.
Usage
Create a UI object in your canvas for the selection box
Add Selection Box component
Configure animation settings:
- Animation Duration for movement speed
- Animation Curve for easing
- Fade Duration for visibility transitions
Set default appearance:
- Default Offset for padding around selected objects
- Default Sprite for the box visual
- Default Color for tinting
Optionally enable Only Override Objects to restrict selection tracking
The selection box will automatically follow EventSystem selection changes
Properties
Animation Settings
| Field | Type | Description |
|---|---|---|
animationDuration | float | Duration of the movement and resize animation |
animationCurve | AnimationCurve | Animation curve for position and size transitions |
fadeDuration | float | Duration of the fade in/out animation |
Default Appearance
| Field | Type | Description |
|---|---|---|
defaultOffset | Vector2 | Default offset from the selected object's edges |
defaultSprite | Sprite | Default sprite/texture for the selection box |
defaultColor | Color | Default color for the selection box |
pixelsPerUnitMultiplier | float | Pixels per unit multiplier for sliced images |
Detection Settings
| Field | Type | Description |
|---|---|---|
onlyOverrideObjects | bool | If true, only objects with SelectionBoxOverride component will be selectable |
Public Methods
| Name | Parameters | Description |
|---|---|---|
SelectObject(RectTransform target) | RectTransform | Manually select a UI object and move the selection box to it |
Deselect() | None | Deselect and hide the selection box |
GetCurrentTarget() | None | Returns the currently selected RectTransform |
SetOnlyOverrideObjects(bool value) | bool | Set whether to only select objects with SelectionBoxOverride component |
Override Component
Add SelectionBoxOverride component to individual UI elements to customize how the selection box appears when they are selected.
Properties
| Field | Type | Description |
|---|---|---|
Offset | Vector2 | Custom offset for this object |
OverrideSprite | Sprite | Custom sprite to use when this object is selected |
OverrideColor | bool | Whether to override the color |
Color | Color | Custom color when overriding |
OverridePPU | bool | Whether to override pixels per unit multiplier |
PixelsPerUnitMultiplier | float | Custom PPU multiplier when overriding |
Code Example
using UnityEngine;
using Evo.UI;
public class SelectionBoxExample : MonoBehaviour
{
public SelectionBox selectionBox;
public RectTransform customButton;
void Start()
{
// Configure selection box behavior
selectionBox.SetOnlyOverrideObjects(false);
}
public void SelectCustomButton()
{
// Manually select a specific UI element
selectionBox.SelectObject(customButton);
}
public void ClearSelection()
{
// Hide the selection box
selectionBox.Deselect();
}
public void CheckCurrentSelection()
{
// Get the currently selected target
RectTransform current = selectionBox.GetCurrentTarget();
if (current != null)
{
Debug.Log("Currently selected: " + current.name);
}
else
{
Debug.Log("No object selected");
}
}
void Update()
{
// Example: Press Tab to clear selection
if (Input.GetKeyDown(KeyCode.Tab))
{
selectionBox.Deselect();
}
}
}