Unity3d GUI element Interface coordinate system summary (with formula)

Source: Internet
Author: User

Hello everybody, I am Sun Guangdong, I am making a game recently, the UI is using the system provided GUI. Encounter a lot of problems. I believe that people often use Ngui or 2DTOOLKIT. Not much to say.

Unity has four important coordinate systems.

Be able to view for example the following articles to understand:

I'm mainly talking about two things today: Guitext and Guitexture.

1, Guitext

The concept of Anchor point (Anchor) I will not introduce. Like Ngui and tookit2d and cocos2d have this important concept, for the picture we can feel is the original point of the picture itself.

The Guitext object itself is also a support for setting anchor points, but there are only 9 options. Used to set the origin of the entire body of the displayed text text.

No matter what object will have transform components, and in the Guitext object is practical is position. What does this position stand for?

An important formula:

The location of the entire text text's overall self-coordinate origin on the form screen:

POS (x, y) = (position.x*screen.width + pixeloffset.x, Position.y*screen.height + pixeloffset.y)

This Position represents the base of the entire text text's position of its own coordinate origin, position.x is a multiple of the width of the entire form screen, POSITION.Y is a multiple of the height of the entire form screen.

Note that the coordinate origin of the screen is the lower-left corner (0,0).

You'd better be able to try these values. Observe the changes.

The other attributes are not introduced here.

2, Guitexture

This is not an anchor point, the original point of the picture is in the lower left corner (0,0,).

An important formula:

The overall self-coordinate origin of the entire guitexture picture is located on the form screen:

POS (x, y) = (position.x*screen.width +pixelinset.x, Position.y*screen.height +pixelinset.y)

This Position represents the base of the entire text text's position of its own coordinate origin, position.x is a multiple of the width of the entire form screen, POSITION.Y is a multiple of the height of the entire form screen.

Two values of their own can try to modify. Observe the phenomenon.

Finally, let's make this logic: for example, the following game end effect.

The logic of the object is this, Gameover is an empty object.

Bg_end is the big background to the end of the game.

Here are three text Guitext

And there is guitexture "this time to get points" picture.

and a button-shaped guitexture picture

In order to make the whole logic not deformed.    is to let the front of each small UI element do the game end of the large background of the child object. We let the child object and the parent object be relative to the Transform.position.x,y value (to note), may think I set the child object's transform.position.x.y to be 0, 0,. Note that the child objects inherit from the parent object. The same as the child object. So we can adjust the pixelinset.x,y of the sub-object to the appropriate position.

We record this value for the button fake picture.

Such as: We write code to display the real button when used.

The following code is written to display this button:

Voidongui ()//Display button//

{

Gui.skin= Mygui;

if (GUI. Button (Newrect (screen.width*0.5f-26.3f,screen.height*0.5f+178.71f-67f,61f,67f), "", GUI.skin.GetStyle (" Continuebutton ")))

{

Application.loadlevel ("MainMenu");

}

}

One of the basic things to note here is "to draw The coordinate system of the GUI interface to the upper left corner of the screen (0, 0) point "

Position formula for button:

(Screen.width* (Child position.x+ parent position.x) + child Pixelinset.x's original value, screen.height* (child position.y+ parent POSITION.Y) + Absolute value of sub-pixelinset.y-sub-PixelInset.h)

The size of the game is generally the artwork provided by the original size.

You have a good understanding of it.

Unity3d GUI element Interface coordinate system summary (with formula)

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.