# Rect

> A 2D Rectangle defined by X and Y position, width and height.

## Definition

* **Type:** Struct
* **Namespace:** [UnityEngine](/engine/6000.7/script-reference/unityengine.md)
* **Assembly:** UnityEngine.CoreModule
* **Implements:** [IEquatable\<Rect>](https://learn.microsoft.com/dotnet/api/system.iequatable-1), [IFormattable](https://learn.microsoft.com/dotnet/api/system.iformattable)

```csharp
public struct Rect : IEquatable<Rect>, IFormattable
```

## Remarks

Unity uses a number of 2D coordinate spaces, most of which define X as increasing to the right, and Y increasing upwards. The one exception is in the GUI and GUILayout classes, where Y increases downwards.

The following examples are illustrated in GUI space, where (0,0) represents the top-left corner and Y increases downwards.

Rectangles can be specified in two different ways. The first is with an [Rect.x](/engine/6000.7/script-reference/unityengine/rect/x.md) and [Rect.y](/engine/6000.7/script-reference/unityengine/rect/y.md) position and a [Rect.width](/engine/6000.7/script-reference/unityengine/rect/width.md) and [Rect.height](/engine/6000.7/script-reference/unityengine/rect/height.md):

![RectXY (Rect)](/api/media?file=/engine/6000.7/media/images/RectXY.svg)

The previous image illustrates a rectangle formed by defining `x` and `y` and then extending `x` by `width` and `y` by `height` to form the rectangle's edges.

The other way is with the X and Y coordinates of each of its edges. These are called [Rect.xMin](/engine/6000.7/script-reference/unityengine/rect/xmin.md), [Rect.xMax](/engine/6000.7/script-reference/unityengine/rect/xmax.md), [Rect.yMin](/engine/6000.7/script-reference/unityengine/rect/ymin.md) and [Rect.yMax](/engine/6000.7/script-reference/unityengine/rect/ymax.md):

![RectXMinYMin (Rect)](/api/media?file=/engine/6000.7/media/images/RectXMinYMin.svg)

The previous image illustrates a rectangle formed by defining `xMin`, `xMax`, `yMin` and `yMax` as the extreme points of the rectangle's edges.

Note that although `x` and `y` have the same values as `xMin` and `yMin`, they behave differently when you set them. Setting `x` or `y` changes the position of the rectangle, but preserves its size:

![RectSetX (Rect)](/api/media?file=/engine/6000.7/media/images/RectSetX.svg)

In the previous image a rectangle remains the same size but shifts right when its `x` property increases.

Setting any of `xMin`, `xMax`, `yMin` and `yMax` will resize the rectangle, but preserve the position of the opposite edge:

![RectSetXMin (Rect)](/api/media?file=/engine/6000.7/media/images/RectSetXMin.svg)

In the previous image, the value of `xMin` increases while `xMax` remains unchanged, which resizes the rectangle by reducing its width.

Additional Resources: [GUI Scripting Guide](/engine/6000.7/manual/uitoolkits/ui-imgui/gui-basics.md), [Camera.rect](/engine/6000.7/script-reference/unityengine/camera/rect.md), [Camera.pixelRect](/engine/6000.7/script-reference/unityengine/camera/pixelrect.md).

## Constructors

| Constructor                                                                                                                                               | Description                                    |
| --------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------- |
| [Rect(System.Single,System.Single,System.Single,System.Single)](/engine/6000.7/script-reference/unityengine/rect/ctor.md#rect\(float-float-float-float\)) | Creates a new rectangle.                       |
| [Rect(UnityEngine.Vector2,UnityEngine.Vector2)](/engine/6000.7/script-reference/unityengine/rect/ctor.md#rect\(vector2-vector2\))                         | Creates a rectangle given a size and position. |
| [Rect(UnityEngine.Vector2@,UnityEngine.Vector2@)](/engine/6000.7/script-reference/unityengine/rect/ctor.md#rect\(vector2-vector2\))                       | Creates a rectangle given a size and position. |

## Properties

| Property                                                                 | Description                                                |
| ------------------------------------------------------------------------ | ---------------------------------------------------------- |
| [center](/engine/6000.7/script-reference/unityengine/rect/center.md)     | The position of the center of the rectangle.               |
| [height](/engine/6000.7/script-reference/unityengine/rect/height.md)     | The height of the rectangle, measured from the Y position. |
| [max](/engine/6000.7/script-reference/unityengine/rect/max.md)           | The position of the maximum corner of the rectangle.       |
| [min](/engine/6000.7/script-reference/unityengine/rect/min.md)           | The position of the minimum corner of the rectangle.       |
| [position](/engine/6000.7/script-reference/unityengine/rect/position.md) | The X and Y position of the rectangle.                     |
| [size](/engine/6000.7/script-reference/unityengine/rect/size.md)         | The width and height of the rectangle.                     |
| [width](/engine/6000.7/script-reference/unityengine/rect/width.md)       | The width of the rectangle, measured from the X position.  |
| [x](/engine/6000.7/script-reference/unityengine/rect/x.md)               | The X coordinate of the rectangle.                         |
| [xMax](/engine/6000.7/script-reference/unityengine/rect/xmax.md)         | The maximum X coordinate of the rectangle.                 |
| [xMin](/engine/6000.7/script-reference/unityengine/rect/xmin.md)         | The minimum X coordinate of the rectangle.                 |
| [y](/engine/6000.7/script-reference/unityengine/rect/y.md)               | The Y coordinate of the rectangle.                         |
| [yMax](/engine/6000.7/script-reference/unityengine/rect/ymax.md)         | The maximum Y coordinate of the rectangle.                 |
| [yMin](/engine/6000.7/script-reference/unityengine/rect/ymin.md)         | The minimum Y coordinate of the rectangle.                 |

## Static Properties

| Property                                                         | Description                                |
| ---------------------------------------------------------------- | ------------------------------------------ |
| [zero](/engine/6000.7/script-reference/unityengine/rect/zero.md) | Shorthand for writing `new Rect(0,0,0,0)`. |

## Methods

| Method                                                                   | Description                                                                                                                                                                                                                                                                    |
| ------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| [Contains](/engine/6000.7/script-reference/unityengine/rect/contains.md) | Returns true if the `x` and `y` components of `point` is a point inside this rectangle. If `allowInverse` is present and true, the width and height of the Rect are allowed to take negative values (ie, the min value is greater than the max), and the test will still work. |
| [Overlaps](/engine/6000.7/script-reference/unityengine/rect/overlaps.md) | Returns true if the other rectangle overlaps this one. If `allowInverse` is present and true, the widths and heights of the Rects are allowed to take negative values (ie, the min value is greater than the max), and the test will still work.                               |
| [Set](/engine/6000.7/script-reference/unityengine/rect/set.md)           | Set components of an existing Rect.                                                                                                                                                                                                                                            |
| [ToString](/engine/6000.7/script-reference/unityengine/rect/tostring.md) | Returns a formatted string for this `Rect`.                                                                                                                                                                                                                                    |

## Static Methods

| Method                                                                                     | Description                                                       |
| ------------------------------------------------------------------------------------------ | ----------------------------------------------------------------- |
| [MinMaxRect](/engine/6000.7/script-reference/unityengine/rect/minmaxrect.md)               | Creates a rectangle from min/max coordinate values.               |
| [NormalizedToPoint](/engine/6000.7/script-reference/unityengine/rect/normalizedtopoint.md) | Returns a point inside a rectangle, given normalized coordinates. |
| [PointToNormalized](/engine/6000.7/script-reference/unityengine/rect/pointtonormalized.md) | Returns the normalized coordinates corresponding to the point.    |

## Operators

| Operator                                                                       | Description                                  |
| ------------------------------------------------------------------------------ | -------------------------------------------- |
| [operator ==](/engine/6000.7/script-reference/unityengine/rect/op-equality.md) | Returns true if the rectangles are the same. |
