U3d the relationship between screen coordinates, world coordinates, pixel coordinates

Source: Internet
Author: User
Tags constant
u3d The relationship between screen coordinates, world coordinates, pixel coordinates

In u3d, the screen coordinates and the world coordinate units are the direct one by one correspondence between the two, unaffected by screen resolution. By default, the bottom-left coordinate of the screen space canvas is the World origin (0,0,0), in which case the point of World space (1920,1080, any value) corresponds to the point on the screen (1920,1080,0).

The size of the sprite is determined by the pixel of the actual picture, such as 512x512 's picture on the sprite, the size of the sprite is 5.12x5.12. That is: 1=100 pixels in 3D space by default, this can be set in each picture import settings.

As a result, the screen resolution represents the range of world space that the game window can see. 1024x768, for example, says that the game window can see 1024 units long in world space and 768 units wide in range.

While the proportional resolution is different, such as 4:3 mode, take the system desktop resolution high as the game screen width, length = width X4/3.

Therefore, the world coordinates of the UI and the 3D coordinate properties of any object in 3D space are the same. The position of the UI minus the position in the lower-left corner of the canvas gets the absolute screen coordinates of the UI. For the screen space UI, its world coordinate position is the coordinates of the screen space relative to the lower-left corner of the screen (0,0,0).

When the resolution is changed, the UI coordinates will generally change. In addition to a situation where all parent node coordinates in the hierarchy of a UI are relative to the lower-left corner of the parent node, the UI must also be relative to the lower-left corner of its parent node. The principle is that when the resolution is changed just increase the size of the canvas, and the bottom left corner of the canvas is always fixed at the origin (screen space u).

In the screenshot, lower:

The above-mentioned situation is in the case of the canvas for screen space Overlay,canvas scaler for the const pixel size. This is one of the simplest cases.

Assuming that the game resolution is now set to 1920x1080 and the orthographic camera cam is set to Orthgraphicsize = 4.5, the camera viewport height is 9 and the width is 16.

Three Modes of canvas:

1,screen Space Overlay: The range of UI coordinates is (0,0) ~ (1920,1080), because at this point the lower left corner of the canvas is at the origin

The 2,screen space Camera,ui camera is set to the above cam, assuming that the bottom left corner of the canvas is at the origin, when the UI coordinate range is (0, 0) ~ (16, 9), which means that it is no longer one by one corresponding to the screen coordinates, but rather scaled.

3,world space, world spaces, when the UI is equivalent to a 3D object, you must use Camera.worldtoscreenpoint to convert to screen coordinates.

Canvasscaler of three modes:

1,constant pixel size: fixed pixel size, no matter how the game window size is changed (in the Unity Editor, pull the game window border) Screen.width, Screen.height are 1920,1080

If you specify the screen aspect ratio at this point, such as 4:3, the system device resolution is 1920x1080, then screen.width,screen.heght is the System device pixel (window size) framed by the game window

At this point, the position and size of the UI are measured in this pixel,

The document says "Using the Constant Pixel Size mode, positions and sizes of UI elements is specified in pixels on the screen."

2,scale with screen size: Customizing a canvas resolution in this way, the UI is drawn to the canvas first, and then the canvas is scaled to the game window, in a way that is more flexible than 1, essentially the same.

If the canvas aspect ratio is not the same as the width-to-height ratio, then how to fit the two is determined by the screen match mode, which is used to always take the width or height of the canvas, and then calculates the other party based on the screen width-to-height ratio.

For example: Canvas set X, y 1080, screen resolution of 4:3 mode, then actually screen.width = 1920x1080, screen.height = 1920 * 3/4


Original link: http://www.cnblogs.com/timeObjserver/p/7074683.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.