I am developing a 2d game. I have an object - a sprite (in the SpriteRenderer component I installed the sprite). I'm trying to install it in the upper left corner of the screen.

I would like it to be in this position (upper left corner, for example) at any screen resolution (I am developing for iOS, so now I’m talking about permissions for iPhone, retina, not retina, iPad and others)

I’m used to the fact that usually the zero coordinates of the system 0.0 start from the bottom left corner or top left. However, in Unity , a similar 0.0 indicates the center of the object. By and large, this is not a problem, but it is a bit confusing. If I print the width of the object (which is 128px) - I get the value 1.06 . The screen, as I understand it, is divided virtually into sections, where the edge on the left is -3 and on the right is 3

Question: Why is the sprite width 1.06? How do I properly place objects in this system? For example, imagine that I have a 128px * 128px sprite and I want to place it exactly in the upper left corner. How to do it?

Translated from https://stackoverflow.com/q/22594074/6104996

    1 answer 1

    When you import a picture into Unity and set the type of sprite (i.e. Texture Type = Sprite ), then in the same menu you should also notice the attribute "Pixels To Units".

    enter image description here

    100 pixels corresponds to 1 unit. Thus, your 128 pixels (if you leave the default settings) should be equal to 1.28 units (Units). This value is different from your 1.06, but this is most likely because your sprite does not have a uniform shape or you have already set Pixels To Units to a different value or you have already changed the scale value of your sprite. Check that these values ​​have not been changed. Put them in our default values.

    Further. The size of the "orthographic camera" corresponds to half its height.

    In the image below, the orthographic camera will see the area highlighted by the white rectangle. Size (Size) 5 - means 10 units height. The image of the flower is 128px wide and 96px in the height, which has default settings of 1 unit = 100 pixels.

    enter image description here

    Considering that you want to use the sprite and you need each frame to remain in the upper left corner at any resolution, you can now use the following code, which needs to be attached to the sprite:

     void Update () { float camHalfHeight = Camera.main.orthographicSize; float camHalfWidth = Camera.main.aspect * camHalfHeight; Bounds bounds = GetComponent<SpriteRenderer>().bounds; // Устанавливаем новый вектор в верхний левый угол Vector3 topLeftPosition = new Vector3(-camHalfWidth, camHalfHeight, 0) + Camera.main.transform.position; // Устанавливаем смещение на основе размера объекта topLeftPosition += new Vector3(bounds.size.x / 2, -bounds.size.y / 2, 0); transform.position = topLeftPosition; } 

    In some cases it is possible that the sprite will be slightly higher than the top edge of the camera. Then you can bring the mapping from screen space into world space. In this case, the screen size is determined in pixels. Because in this case, the bottom left of the screen has coordinates (0,0); right top ( pixelWidth , pixelHeight), a position z in world units from the camera, then you need to place the image in the top left corner like this:

     void Update () { Bounds bounds = GetComponent<SpriteRenderer>().bounds; Vector3 topLeftPosition = Camera.main.ScreenToWorldPoint (new Vector3 (0, Camera.main.pixelHeight, Camera.main.nearClipPlane)); // Устанавливаем смещение на основе размера объекта topLeftPosition += new Vector3(bounds.size.x / 2, -bounds.size.y / 2, 0); transform.position = topLeftPosition; } 

    The answer is translated and updated with https://stackoverflow.com/a/22596935/6104996