- Home /
How to convert pixels to Unity units?
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?
It sounds like you want to calculate the scale an object should have for its size to match some relative portion of the screen. It's not impossible to do this, but you have supplied way too little information for us to suggest a solid solution. For example, the approach would have different requirements for orthographic cameras than for perspective cameras.
Provide some more information that details your actual use case. What effect or behaviour are you trying to achieve? $$anonymous$$aybe there's a simpler way to accomplish what you need than trying to calculate object scale from screen space size.
What I need is smtg like this:
GameObject.Find("cube").transform.localScale = new Vector3(Screen.Width/4, Screen.Width/4, 1);
Answer by robertbu · Apr 30, 2014 at 02:38 PM
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).
@robertbu I love your answers about screens with Unity. I am learning a lot. Thank you very much :)
@robertbu The code works but when I change to a screen with a different aspect ratio (for example: 1536x2048 of iPad to 540x960 of mobile), the ratio of the gameobjects is so small. Do you know how can I solve this? Thank you
Answer by 0V3RR1D3 · Apr 29, 2014 at 10:06 PM
if its 2D Then select the sprite in your assets and there should be a box in the inspector that says pixels to units :D
Answer by liortal · Apr 29, 2014 at 09:37 PM
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: link
What I need is smtg like this:
GameObject.Find("cube").transform.localScale = new Vector3(Screen.Width/4, Screen.Width/4, 1);
And I don't know how to do this with Camera.WorldToScreenPoint. Explain further, plz..
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Unity units to pixel? 1 Answer
2D Games with C#? 3 Answers
A little help? 1 Answer
Another KeyCode problem... 2 Answers