- Home /
Scaling position of gameobjects based on screen res
Hi, I'm using gameobjects with the ex2d sprite component to drive my UI (Unity GUi was too slow) within the scene and I want to create a system that will work independent of resolution - so I can place the elements on my PC and their relative positions will remain when I build onto iPhone.
I was previously using GUI elements and using a percentage to define their position (i.e position of element = Screen.width* percentage). So I'd edit the percentage value to place elements where I want. However, I'm now dealing with gameobjects in world space, I'm not sure how to go about this.
I have come across camera.WorldToScreenPoint(transform.position) that converts world space into screen space - is this a recommended method?
Thanks!
Answer by PrimeDerektive · Sep 10, 2011 at 11:27 AM
Judging from your previous implementation, the best solution would probably be to use ViewportToWorldPoint(). In all the Camera functions, "Screen" gives you x/y pixel position on the screen, "Viewport" gives you x/y percentage.
Sorry not quite joining the dots here - it sounds as if I need the opposite? Aren't gameobjects in world space, and I need to convert them to viewport space? thanks for your time.
Well yeah, if you want to get the viewport position of a gameobject, you would use WorldToViewportPoint(). I assumed from your question that you were looking to position the gameobject based on the viewport, not get the existing position.