2D Toolkit Forum

2D Toolkit => Support => Topic started by: Gooms9 on January 27, 2012, 06:11:16 pm

Title: Dealing with pixel dimensions?
Post by: Gooms9 on January 27, 2012, 06:11:16 pm
Hey, I was wondering how I can tell how large something is in pixels? I've got my sprite which is 1024x768. But it's width and height in unity are both 0.75. Do I have to perform these calculations myself, or is there a convenience method that I can make use of for getting and setting its pixel dimensions?
Title: Re: Dealing with pixel dimensions?
Post by: unikronsoftware on January 27, 2012, 06:41:08 pm
Unity has convenience functions to do these unit conversions. The two you'll need are:

camera.WorldToScreenPoint
and
camera.ScreenToWorldPoint

You can use this to calculate differences in position (eg. widths & heights) too. Make sure you pass correct z values in for this to work with a perspective camera. With an ortho camera, things are much much simpler - you simply do camera.WorldToScreenPoint(new Vector3(1,1,1)) and that gives you a conversion constant you can use to convert from world to screen space.