Legacy Documentation: Version 5.2
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Rect

struct in UnityEngine

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Sumbission failed

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Switch to Manual

Description

A 2D Rectangle defined by x, y position and width, height.

The Rect structure is mainly used for 2D operations. The UnityGUI system uses it extensively and it is also to set the onscreen position of a Camera's view.

The rectangle can be specified in two different ways. The first way involves supplying the corner coordinate (usually that's imagined as top-left) along with the width and height. This is done using the x, y, width and height properties.

The second way to specify the rectangle is to supply the X coordinates of its left and right sides and the Y coordinates of its top and bottom sides. These are denoted by the xMin, xMax, yMin and yMax properties.



Although the x and y properties may seem to be the same as xMin and xMax, their behaviour is actually slightly different. The x and y values are assumed to be used along with width and height. This means that if you change x or y without changing width or height then the rectangle will change position but stay the same size. On the other hand, if you change the values of xMin or yMin without changing xMax or yMax then the rectangle will change size and the top-left corner will also change.

See Also: GUI Scripting Guide, Camera.rect, Camera.pixelRect.

Variables

centerCenter coordinate of the rectangle.
heightHeight of the rectangle.
maxUpper right corner of the rectangle.
minLower left corner of the rectangle.
positionThe top left coordinates of the rectangle.
sizeThe size of the rectangle.
widthWidth of the rectangle.
xLeft coordinate of the rectangle.
xMaxRight coordinate of the rectangle.
xMinLeft coordinate of the rectangle.
yTop coordinate of the rectangle.
yMaxBottom coordinate of the rectangle.
yMinTop coordinate of the rectangle.

Constructors

RectCreates a new rectangle.

Public Functions

ContainsReturns 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.
OverlapsReturns 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.
SetSet components of an existing Rect.
ToStringReturns a nicely formatted string for this Rect.

Static Functions

MinMaxRectCreates a rectangle from min/max coordinate values.
NormalizedToPointReturns a point inside a rectangle, given normalized coordinates.
PointToNormalizedReturns the normalized coordinates cooresponding the the point.

Operators

operator !=Returns true if the rectangles are different.
operator ==Returns true if the rectangles are the same.