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
1
Question by rvdb86 · Aug 01, 2013 at 11:39 PM · jsonscalingpositioningpixels

Converting Pixels to Units and Position Prefab

Hi,

I am working on a game that adds prefabs at runtime. Each prefab is added by getting information from a remote JSON file that contains the position & scale data for the prefab. I want to add the prefabs and position them correctly on a plane. So far I am able to read the JSON file and add the prefab, the problem is that the JSON position and scale data is in pixels. What would be the best way to normalize the data so that I can position and scale them correctly?

Any suggestions are really appreciated!

Comment
Add comment · Show 6
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 Benproductions1 · Aug 02, 2013 at 12:11 AM 0
Share

You can't convert pixels to units without a set conversion factor. Imagine an picture in 3d space. The image is 300x300 pixels. How many units tall is it? You can't possibly answer that

avatar image robertbu · Aug 02, 2013 at 12:16 AM 0
Share

What kind of a prefab are we taking about...2D or 3D? Are you trying to match some texture pixel size to screen pixels, or are you just looking for some arbitrary mapping? Not that it makes much difference, but are you using an Orthographic or Perspective camera?

avatar image rvdb86 · Aug 02, 2013 at 07:20 AM 0
Share

The prefab is 2D. I understand what @Benprodutions is saying however I know that the plane should be 1024x512 pixels, I tried creating an empty GameObject with those dimensions, adding the prefabs then scaling the GameObject to the size of the plane but that didnt work either

avatar image Benproductions1 · Aug 02, 2013 at 08:00 AM 0
Share

"The plane should be 1024x512 pixels", is that in screen space? If you convert 1 pixel of screen space to world space you get your conversion factor. A plane can't actually be 1024x512 pixels in world space, it's literally impossible in a 3d engine :)

avatar image rvdb86 · Aug 02, 2013 at 08:05 AM 0
Share

sorry i dont think I explained myself correctly. Basically I have an external application where users add "assets" to a workspace that is 1024x512. In unity the workspace is represented by a plane, and the assets are the prefabs. Because the external application is web-based all the data is pixels. How do I "convert 1 pixel of screen space to world space" as you suggested?

Show more comments

1 Reply

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

Answer by robhuhn · Aug 02, 2013 at 09:07 AM

if you just need the relation of the plane and the assets (e.g. assetposition 512, 256 would be center of the plane) then you need to devide the asset position by the half of the total with 1024, 512 and subtract 1 to get a ratio from -1 to 1 (0 would be the center). Then multiply the ratio by the half plane bounds size in meters:

 Vector3 assetPosition = new Vector3(1024, 256, 10);
 assetPosition.x = (assetPosition.x / (imageSize.x * .5f) - 1) * plane.renderer.bounds.size.x * .5f; //plane.renderer.bounds x y z depends on plane rotation
 assetPosition.y = (assetPosition.y / (imageSize.y * .5f) - 1) * plane.renderer.bounds.size.y * .5f; 

 
Comment
Add comment · 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

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

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Weird Positioning Problem? 1 Answer

Unity 4.6 and up have broken my sprite layers. How to fix? 1 Answer

Is there a way of correctly parenting to get scales and positions of gameObjects right? 2 Answers

Wrong positions of new objects. Problem with scaling 1 Answer

Unity scaling 1 Answer


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