Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by LynxSolaris · Aug 12, 2014 at 01:12 PM · pixelunitsporting

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LynxSolaris · Aug 12, 2014 at 02:13 PM 0
Share

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!

avatar image smoggach · Aug 26, 2014 at 01:37 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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

Build crashing on macOS (works on windows) 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges