Version: 2019.3
LanguageEnglish
  • C#

Rect Constructor

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

Submission failed

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

Close

Cancel

public Rect(float x, float y, float width, float height);

Parameters

xThe X value the rect is measured from.
yThe Y value the rect is measured from.
widthThe width of the rectangle.
heightThe height of the rectangle.

Description

Creates a new rectangle.

using UnityEngine;

public class RectExample : MonoBehaviour { void Start() { Rect rect = new Rect(0, 0, 10, 10);

// prints: (x:0, y:0, width:10, height:10) Debug.Log(rect); } }

Note: Rect represents an abstract rectangle, and can be used in a variety of situations. As such, Rects don't have an explicit top, bottom, left or right. For example, Y values in Camera space are measured from the bottom of the screen, but Y values in Editor GUI space are measured from the top of the window, therefore whether the Y value of a Rect is its "top" or "bottom" will vary depending on where you use the Rect value. You can refer to the corners by using Rect.min and Rect.max.


public Rect(Vector2 position, Vector2 size);

Parameters

positionThe position of the minimum corner of the rect.
sizeThe width and height of the rect.

Description

Creates a rectangle given a size and position.

This form of the constructor is convenient when you are already working with Vector2 values.

Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961