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
3
Question by Samael_00001 · Apr 29, 2014 at 07:23 PM · convertpixelunits

How to convert pixels to Unity units?

Hi! I want to change object size depending on resolution, but objects size accepts Unity units and Screen size - pixels. How to convert pixels to Unity units?

Comment
Add comment · Show 2
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 CHPedersen · Apr 30, 2014 at 10:44 AM 0
Share

It sounds like you want to calculate the scale an object should have for its size to match some relative portion of the screen. It's not impossible to do this, but you have supplied way too little information for us to suggest a solid solution. For example, the approach would have different requirements for orthographic cameras than for perspective cameras.

Provide some more information that details your actual use case. What effect or behaviour are you trying to achieve? $$anonymous$$aybe there's a simpler way to accomplish what you need than trying to calculate object scale from screen space size.

avatar image Samael_00001 · Apr 30, 2014 at 11:08 AM 0
Share

What I need is smtg like this:

GameObject.Find("cube").transform.localScale = new Vector3(Screen.Width/4, Screen.Width/4, 1);

3 Replies

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

Answer by robertbu · Apr 30, 2014 at 02:38 PM

What I need is smtg like this:

GameObject.Find("cube").transform.localScale = new Vector3(Screen.Width/4, Screen.Width/4, 1);

I'm not sure what you are asking for is what you really want. Unity keeps the vertical world view of the same regardless of the resolution. The horizontal size changes. So lets say you author your game for 640 x 480. If you run this game on a screen of 1280 x 960, the object in world space will be identical in size on the new device.

But if you really want to do as you describe, then it is easier to do as a ratio. So you record the size you authored your app for, then the calculation would be:

  transform.localScale = Vector3.Scale(transform.localScale, new Vector3(Screen.width / orgWidth, Screen.height / orgHeight, 1.0f));

So in the case of 640 x 480 running on 1280 x 960, this calculation would scale by (2,2,1).

Comment
Add comment · Show 3 · 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 Samael_00001 · Apr 30, 2014 at 03:09 PM 0
Share

Thanks a lot!

avatar image albertofdb · Sep 09, 2014 at 07:50 PM 0
Share

@robertbu I love your answers about screens with Unity. I am learning a lot. Thank you very much :)

avatar image albertofdb · Sep 09, 2014 at 09:56 PM 0
Share

@robertbu The code works but when I change to a screen with a different aspect ratio (for example: 1536x2048 of iPad to 540x960 of mobile), the ratio of the gameobjects is so small. Do you know how can I solve this? Thank you

avatar image
7

Answer by 0V3RR1D3 · Apr 29, 2014 at 10:06 PM

if its 2D Then select the sprite in your assets and there should be a box in the inspector that says pixels to units :D

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
avatar image
0

Answer by liortal · Apr 29, 2014 at 09:37 PM

You can use the Camera.WorldToScreenPoint method. It transforms a position from world space to screen space.

This can be helpful to convert from units in world space to screen space (in pixels).

For more information, see this similar question: link

Comment
Add comment · Show 1 · 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 Samael_00001 · Apr 30, 2014 at 09:34 AM 0
Share

What I need is smtg like this:

GameObject.Find("cube").transform.localScale = new Vector3(Screen.Width/4, Screen.Width/4, 1);

And I don't know how to do this with Camera.WorldToScreenPoint. Explain further, plz..

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

23 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Unity units to pixel? 1 Answer

2D Games with C#? 3 Answers

A little help? 1 Answer

Another KeyCode problem... 2 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