understanding positions and screen resolution, how to properly set position of object?

开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

The first thing you may want to note is that when you import an image into unity as Texture Type = Sprite, there is a attribute "Pixels To Units", which you will see on the sprite asset.

enter image description here

So your 128px, if left at default settings, would be 1.28 "Units" wide. This value doesn't correspond with your measuresment of 1.06 however, so I'm not sure where that comes from. It could be because the scale of your sprite object isn't uniform, or it could be because your "Pixels To Units" setting has been modified. Double check in the inspector that your input texture is actually 128x128, and also check that the Pixel to Units value is what you expect it to be.

Next, note that the "Size" of an orthographic camera corresponds to half its height.

So in the below image, the ortho camera will see the area in the white box. Size 5, means 10 units high. The picture of a flower I have in the scene is a 128 pixel wide by 96 pixel high image, imported at default settings (1 unit = 100 pixels).

Now I'm not sure what your exact use case is (i.e. perhaps this is something better solved using a GUITexture). However assuming you want to use a Sprite, and every frame you want it to be in the top left corner, and you want that sprite to always be displayed at native res, then the following code would do it. Add it to a script attached to the Sprite game object.

// Update is called once per frame
void Update () {
    float camHalfHeight = Camera.main.orthographicSize;
    float camHalfWidth = Camera.main.aspect * camHalfHeight; 

    Bounds bounds = GetComponent<SpriteRenderer>().bounds;

    // Set a new vector to the top left of the scene 
    Vector3 topLeftPosition = new Vector3(-camHalfWidth, camHalfHeight, 0) + Camera.main.transform.position; 

    // Offset it by the size of the object 
    topLeftPosition += new Vector3(bounds.size.x / 2,-bounds.size.y / 2, 0);

    transform.position = topLeftPosition;        
}


开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值