由 X 和 Y 位置、宽度以及高度定义的 2D 矩形。
Unity 使用了一些 2D 坐标空间,其中大部分将 X 定义为向右增加,而 Y 向上增加。
一个例外是 GUI 和 GUILayout 类,其中 Y 向下增加。
以下示例在 GUI 空间中进行展示,其中 (0,0) 表示左上角,而 Y 向下增加。
矩形可以采用两种不同方式进行指定。第一种是使用 x 和 y 位置以及 width 和 height:
另一种方式是使用其每个边缘的 X 和 Y 坐标。这些坐标称为 xMin、xMax、yMin 和 yMax:
请注意,虽然 x
和 y
的值与 xMin
和 yMin
相同,但是它们在进行设置时具有不同的行为方式。设置 x
或 y
会改变矩形的位置,而大小不变:
设置 yMin
、yMin
、yMin
和 yMax
中的任何值会调整矩形大小,但是相对边缘的位置保持不变:
另请参阅:GUI 脚本指南、Camera.rect、Camera.pixelRect。
zero | 用于编写 new Rect(0,0,0,0) 的简便方法。 |
center | 矩形中心的位置。 |
height | 矩形的高度(从 Y 位置进行测量)。 |
max | 矩形最大角的位置。 |
min | 矩形最小角的位置。 |
position | 矩形的 X 和 Y 位置。 |
size | 矩形的宽度和高度。 |
width | 矩形的宽度(从 X 位置进行测量)。 |
x | 矩形的 X 坐标。 |
xMax | 矩形的最大 X 坐标。 |
xMin | 矩形的最小 X 坐标。 |
y | 矩形的 Y 坐标。 |
yMax | 矩形的最大 Y 坐标。 |
yMin | 矩形的最小 Y 坐标。 |
Rect | 创建一个新的矩形。 |
Contains | 如果 point 的 x 和 y 分量是此矩形内部的点,则返回 true。如果 allowInverse 存在并且为 true,则允许 Rect 的宽度和高度取负值(即,最小值大于最大值),测试仍然有效。 |
Overlaps | 如果另一个矩形与此矩形重叠,则返回 true。如果 allowInverse 存在并且为 true,则允许 Rect 的宽度和高度取负值(即,最小值大于最大值),测试仍然有效。 |
Set | 设置现有 Rect 的分量。 |
ToString | 对于此 Rect,返回整齐格式化的字符串。 |
MinMaxRect | 通过最小/最大坐标值创建矩形。 |
NormalizedToPoint | 根据给定的标准化坐标,返回矩形内的点。 |
PointToNormalized | 返回与点对应的标准化坐标。 |
operator == | 如果矩形相同,则返回 true。 |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.