Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Pawandeep Singh · Jan 31, 2015 at 05:39 PM · sizebackgroundcoordinatesviewunity 4.5

Place and Scale Gameobject to the Screen Width and Height of the device

Hey everyone,

I am developing 2D Mobile games( using Orthographic camera Projection ) in Unity3D but I don't know how to place and scale gameobject so that it fits the device entire width and height like a background image( using sprites for making gameobjects )

I used " Screen.Width " but it is not giving the exact values of the Screen size and I also used " ViewportToWorldPoint "method and I don't think its the right approach.

Thank You!

Comment
Add comment · Show 3
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 DESIMA · Jan 31, 2015 at 06:00 PM 0
Share

can you explain what you are trying to accomplish because there might be a simpler solution

avatar image Pawandeep Singh · Feb 01, 2015 at 12:52 AM 0
Share

Like I want this sprite image to be the background of my scene.

alt text

screen-shot-2015-02-01-at-62200-am.png (35.8 kB)
avatar image DESIMA · Feb 01, 2015 at 03:41 PM 0
Share

why dont you just scale it so it fits. saying you want to support multiple devices would be your image would be stretched anyways, meaning it would be better to have multiple resolution images dependent on the screen size.

why dont you use the new GUI system in Unity 4.6 and just make sure it will always fill your screen? just make sure it is rendered in the background if thats what you wnated

1 Reply

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

Answer by NoseKills · Jan 31, 2015 at 10:29 PM

Look at orthographic camera size in Unity's Docs

based on that you can get the orthographic screen size in world units like this

 float height = Camera.main.orthographicSize * 2;
  float width = height * Screen.width/ Screen.height; // basically height * screen aspect ratio
 

To know how big your sprite is in world units, you have to simply calculate it using Sprite.pixelsPerUnit

Then you can get the scale(s) you must use:

 public SpriteRenderer spriteRenderer;
     void Start () 
     {
         float height = Camera.main.orthographicSize * 2;
         float width = height * Screen.width/ Screen.height; // basically height * screen aspect ratio
 
         Sprite s = spriteRenderer.sprite;
         float unitWidth = s.textureRect.width / s.pixelsPerUnit;
         float unitHeight = s.textureRect.height / s.pixelsPerUnit;
 
         spriteRenderer.transform.localScale = new Vector3(width / unitWidth, height / unitHeight);
     }

Comment
Add comment · Show 4 · 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 Pawandeep Singh · Feb 01, 2015 at 01:10 AM 0
Share

Thanks for your support, These width and height are giving the width and height values of the camera but how would I use that to rescale my sprite image because width = 6.65 and height = 10 and if I use this as transform.localScale = new Vector3( width,height,0 ); for the image it resize x by 6 and y by 10 times which is very huge

alt text

screen-shot-2015-02-01-at-64001-am.png (28.5 kB)
avatar image Pawandeep Singh · Feb 01, 2015 at 01:41 AM 1
Share

Thanks "Nose$$anonymous$$ills" I finally got the solution, The problem which was there that in Unity 1unit = 100pixel and I was using sprite image of different resolution 1920 * 1200 so I advice that everyone should use sprite image of equal width and height as the multiples of 100 and then calculate width and height of the camera as

float height = Camera.main.orthographicSize * 2;

float width = height Screen.width/ Screen.height; // basically height screen aspect ratio

and then use this for rescaling your gameobject as:

transform.localScale = new Vector3 (width, height, 0);

avatar image NoseKills · Feb 01, 2015 at 01:44 AM 0
Share

That's one way to do it. I changed the answer to show how to calculate the scale regardless of sprite settings.

avatar image DESIMA · Feb 01, 2015 at 03:43 PM 0
Share

if one wanted to do this by code

http://docs.unity3d.com/ScriptReference/Camera.WorldToScreenPoint.html

so I have my points in world space and get the points in screen or ScreenToWorldPoint, thus I have indication of scaling factor.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Images in games 1 Answer

Camera view different screen sizes? 0 Answers

GLSL - Paint fragments in a certain view position 0 Answers

Android Auto scale backgorund for any resolution 2 Answers

i can't see my object ? 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