- Home /
Porting an HTML5 project to Unity
Hello all!
First, I'm new to Unity. I've only been working with the editor for a few weeks now, but I'm really enjoying it. I'm trying to port an existing 2D html5 game to Unity and am looking for some advice as to how I should go about loading level data to the scene. More specifically, getting the background sprites in a semi close proximity on the screen to where they are located in the original game. Via C#, I've successfully loaded all sprites and created prefabs for further use in the scene. I'm hitting a road block when it comes to positioning them on the screen. My camera (orthographic) size is set to 5 and PixelToUnit size is set to the default 100. I'm trying to figrue out what the best way to convert the sprites X/Y value (that is in pixels) to Unity Units. I thought that one of the camera functions ScreenTo{Viewport|World}point might be my answer, but I've not had much success with getting the assets plotted to the correct location. The same goes for the scaling of the sprites. Some of the sprites in the original game were either scaled up or down slightly. So my overall question is, what is the best way to handle porting a sprites location/size into unity without manually having to place all the objects by hand.
Thanks in advance for any help/suggestions/advice you can provide.
Answer by smoggach · Aug 12, 2014 at 02:01 PM
Most people will tell you to set the PixelToUnit size to 1 so that there is a 1:1 pixel:unit ratio. Then you can transfer values directly.
Another way is to change the orthographicSize of your camera to the height of your window. So for instance if your window is 768 pixels height you'd set your orthographicSize to 7.68
This depends on your PixelToUnit value as well. if it was 1 you'd want your orthographicSize to be the same as the height of the window, if it's 100 you want it to be 7.68
Here are some things to keep in mind: Screen.width and Screen.height are integers. cast them to floats before using them in calculations with other floats.
Unity's (0,0) is in the center of the screen. Screen's (0,0) is in the bottom left.
Thanks for the response, smoggach! $$anonymous$$y screen size is 1280x720, so I'll set the PixelToUnit value to 1 and set orthographicSize to 720. Is that correct?
I also noticed that (0,0) is at the center of the screen and (0,0) is at the bottom left. This is new to me, because the "engine" I came from had (0,0) at the top left. So I'm going to have to adjust for that.
Thanks again for all the information!
Edit: So I gave this a shot. Think I might have done something wrong, however. When ever I read Screen.height the value returned is 421. So should I be setting my orthographicSize to 421?
Also, would this work for scaling the object as well? I tried setting localScale to the pixel number (using PixelToUnit of 1) and all objects seem to be stretched vertically.
Thanks again!
Screen.width and Screen.height will return the size of whatever window your app is running in. So if you are running in the editor it will return the size of the editor's game window. It will have the proper value on a device though.
If you want to simulate the device's dimensions you can set your game window to Free Aspect and manually resize it until stats report the proper size. Or you can accomplish the same thing with an editor script. I seem to recall basing $$anonymous$$e off of one I found on the wiki.
Your answer

Follow this Question
Related Questions
Unity units to pixel? 1 Answer
How to convert pixels to Unity units? 3 Answers
Javascript in Unity, Including iPhone API's (Objective C?) 2 Answers
porting to windows phone 8? 0 Answers