Rect
The Rect datatype in Unity represents a rectangle defined by its position x, y, and size width, height. It's commonly used in 2D game development to manage areas, positions, and sizes within a game scene.
Properties
Retrieve or assign the x-coordinate of the Rect. The x-coordinate represents the horizontal position of the Rect.
local rect = Rect.new(0, 0, 100, 100)
rect.x = 10
print(rect.x) -- 10
Access or modify the y-coordinate of the Rect. The y-coordinate represents the vertical position of the Rect.
local rect = Rect.new(0, 0, 100, 100)
rect.y = 10
print(rect.y) -- 10
Get or set the position of the Rect as a Vector2. The position represents the x, y coordinates of the Rect.
local rect = Rect.new(0, 0, 100, 100)
rect.position = Vector2.new(10, 10)
print(rect.position.x) -- 10
print(rect.position.y) -- 10
Obtain or modify the center position of the Rect. The center represents the midpoint of the Rect.
local rect = Rect.new(0, 0, 100, 100)
rect.center = Vector2.new(50, 50)
print(rect.center.x) -- 50
print(rect.center.y) -- 50
Access the min point of the Rect. The min point represents the bottom-left corner of the Rect.
local rect = Rect.new(0, 0, 100, 100)
print(rect.min.x) -- 0
print(rect.min.y) -- 0
Get the max point of the Rect. The max point represents the top-right corner of the Rect.
local rect = Rect.new(0, 0, 100, 100)
print(rect.max.x) -- 100
print(rect.max.y) -- 100
Access or modify the width of the Rect. The width represents the horizontal size of the Rect.
local rect = Rect.new(0, 0, 100, 100)
rect.width = 50
print(rect.width) -- 50
Retrieve or set the height of the Rect. The height represents the vertical size of the Rect.
local rect = Rect.new(0, 0, 100, 100)
rect.height = 50
print(rect.height) -- 50
Get or set the size of the Rect as a Vector2. The size represents the width, height dimensions of the Rect.
local rect = Rect.new(0, 0, 100, 100)
rect.size = Vector2.new(50, 50)
print(rect.size.x) -- 50
print(rect.size.y) -- 50
Access the minimum x-coordinate of the Rect. The xMin represents the left edge of the Rect.
local rect = Rect.new(0, 0, 100, 100)
print(rect.xMin) -- 0
Access the minimum y-coordinate of the Rect. The yMin represents the bottom edge of the Rect.
local rect = Rect.new(0, 0, 100, 100)
print(rect.yMin) -- 0
Get the maximum x-coordinate of the Rect. The xMax represents the right edge of the Rect.
local rect = Rect.new(0, 0, 100, 100)
print(rect.xMax) -- 100
Access the maximum y-coordinate of the Rect. The yMax represents the top edge of the Rect.
local rect = Rect.new(0, 0, 100, 100)
print(rect.yMax) -- 100
Obtain a Rect instance positioned at the origin (0, 0) with zero width and height. This represents a point at the origin.
print(Rect.zero.x) -- 0
print(Rect.zero.y) -- 0
print(Rect.zero.width) -- 0
print(Rect.zero.height) -- 0
Methods
Set the position and size of the Rect. This method assigns the x, y position, width, and height of the Rect.
local rect = Rect.new(0, 0, 100, 100)
rect:Set(10, 10, 50, 50)
print(rect.x) -- 10
print(rect.y) -- 10
print(rect.width) -- 50
print(rect.height) -- 50
Parameters
x
y
width
height
Returns
Check if a point is within the Rect. This method determines if a point is inside the Rect.
local rect = Rect.new(0, 0, 100, 100)
local point = Vector2.new(50, 50)
print(rect:Contains(point)) -- true
Parameters
point
Vector2Returns
Determine if the Rect overlaps with another Rect. This method checks if the Rect overlaps with another Rect.
local rect1 = Rect.new(0, 0, 100, 100)
local rect2 = Rect.new(50, 50, 100, 100)
print(rect1:Overlaps(rect2)) -- true
Parameters
other
RectReturns
Create a new Rect from minimum and maximum coordinates. This method creates a Rect from the minimum and maximum coordinates.
local rect = Rect.MinMaxRect(0, 0, 100, 100)
print(rect.x) -- 0
print(rect.y) -- 0
print(rect.width) -- 100
print(rect.height) -- 100
Parameters
xmin
ymin
xmax
ymax
Returns
Convert normalized coordinates to a point within the Rect. This method converts normalized coordinates to a point within the Rect.
local rect = Rect.new(0, 0, 100, 100)
local normalized = Vector2.new(0.5, 0.5)
local point = rect:NormalizedToPoint(normalized)
print(point.x) -- 50
print(point.y) -- 50
Convert a point within the Rect to normalized coordinates. This method converts a point within the Rect to normalized coordinates.
local rect = Rect.new(0, 0, 100, 100)
local point = Vector2.new(50, 50)
local normalized = rect:PointToNormalized(point)
print(normalized.x) -- 0.5
print(normalized.y) -- 0.5