---
description: Learn more about rectangle selection area
---

# 2D Rectangle

## Overview

Uses mouse drag start and current screen positions to calculate rectangle on screen. Then uses configurations to render the rectangle on screen in the `OnGUI` method.&#x20;

**Note:** Prefab is named <mark style="color:green;">**2D Rectangle Selection Area**</mark>.

## Configuration

Customise the appearance and behaviour of the selection rectangle to suit your game's needs.

<figure><img src="https://425343605-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fml7vS0D5xN2Q8jQMEmBv%2Fuploads%2Fbvm0rQ30P9Aie9mBe8TF%2Fimage.png?alt=media&#x26;token=986d0ec8-c62f-4da1-951e-e1b10f331c01" alt="" width="563"><figcaption><p>RectangleSelectionArea component</p></figcaption></figure>

### Visuals

**Custom Textures**

* **Custom Fill and Border Textures**: Customise the textures used for the fill and border of the selection area. By default, a plain white texture is employed, which serves as a neutral base. If you use custom textures featuring distinct colors or transparent pixels, consider setting the fill and border colors to white. This ensures your textures appear without any unintended color tinting.

**Color Settings**

* **Fill and Border Colors**: Define the colors for rendering the fill and border of your selection area. This allows for a high degree of visual customization, enabling the selection area to match or contrast with the game environment effectively.

**Border Properties**

* **Border Thickness**: Specify the thickness of the border around the selection area. This border is rendered externally and does not influence the behavior or visibility of the selection components inside it.

### Detection Type

The rectangle selection area supports various detection methods, which are crucial for determining whether units fall within the specified selection boundaries. Each method varies in how it accounts for unit positions and their boundaries:

1. **ScreenPosition**
   * **Description**: Transforms units' world positions to screen positions and checks if these positions are within the selection area. This method is effective for direct visual correspondence with the user's view.
2. **WorldPosition**
   * **Description**: Creates a frustum for the selection area at maximum distance and checks if units' world positions fall within it. This method is useful for selecting units based on their actual positions in the game world, regardless of the camera's perspective. This produces the same result as **ScreenPosition**, but different technique is used.
3. **RendererBounds**
   * **Description**: Utilizes Axis-Aligned Bounding Box (AABB) tests for renderer bounds. This method does not consider rotation, making it suitable when rotation does not significantly affect the selection relevance.
   * **Consideration**: Rotation is ignored in this method. If units significantly differ in dimensions along the X and Z axes, and rotation may affect the detection, **Screen** or **World Position** detection should be considered.
   * **Optimisation:** Renderer is retrieved using `GameObject.GetComponentInChildren<Renderer>()` method, which can lead to two things; performance impact and selecting the wrong renderer if game object has more than one. Using <mark style="color:blue;">**RendererSelectionBounds**</mark> component with specified renderer and **CustomBounds** detection type will optimise this detection process. &#x20;
4. **CustomBounds**
   * **Description**: Similar to **RendererBounds**, this method creates a frustum for the selection area at maximum distance and performs AABB tests for custom selection bounds. Like **RendererBounds**, rotation is not considered.
   * **Consideration**: This method is ideal for custom-defined bounds that do not follow standard rendering bounds. It shares the same limitations regarding rotation as RendererBounds.
   * **Requirement:** This detection type requires for selectable units to have a component that implements <mark style="color:blue;">**ISelectableBounds**</mark>. There are few available out of the box, you can find them here [selection-bounds](../selectable-unit/selection-bounds "mention").

#### Conclusion

When choosing a detection type, consider the geometric properties of the units and how they are likely to interact with the selection area. For units that vary greatly in dimensions along the X and Z axes and where rotation could alter the selection outcome, **WorldPosition** or **ScreenPosition** may provide more accurate results.
