Hi! I want to change object size depending on resolution, but objects size accepts Unity units and Screen size - pixels. How to convert pixels to Unity units?
if its 2D Then select the sprite in your assets and there should be a box in the inspector that says pixels to units
What I need is smtg like this:
GameObject.Find(“cube”).transform.localScale
= new Vector3(Screen.Width/4, Screen.Width/4, 1);
I’m not sure what you are asking for is what you really want. Unity keeps the vertical world view of the same regardless of the resolution. The horizontal size changes. So lets say you author your game for 640 x 480. If you run this game on a screen of 1280 x 960, the object in world space will be identical in size on the new device.
But if you really want to do as you describe, then it is easier to do as a ratio. So you record the size you authored your app for, then the calculation would be:
transform.localScale = Vector3.Scale(transform.localScale, new Vector3(Screen.width / orgWidth, Screen.height / orgHeight, 1.0f));
So in the case of 640 x 480 running on 1280 x 960, this calculation would scale by (2,2,1).
You can use the Camera.WorldToScreenPoint method. It transforms a position from world space to screen space.
This can be helpful to convert from units in world space to screen space (in pixels).
For more information, see this similar question: 2