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 Fluckysan · Jun 17, 2015 at 07:04 PM · c#positionscalescreen size

Position and scale GameObject according to screen size

Hi,

Newby on Unity here sorry if this is an already answered question

I'm instantiating GameObject from prefab and positioning it on my scene with Instantiate()

Now I want my game to support as many screen size as possible, so how can I set the correct position and / or scale of my GameObject according to screen size?

How do you do that on your games?

Comment
Add comment · Show 4
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 digzou · Jun 18, 2015 at 10:02 AM 0
Share

Look into canvas scaler's "scale with screen size" property :

http://docs.unity3d.com/$$anonymous$$anual/script-CanvasScaler.html

avatar image Hellium · Jun 18, 2015 at 10:05 AM 0
Share

@digzou :

From the doc :

The Canvas Scaler component is used for controlling the overall scale and pixel density of UI elements in the Canvas. This scaling affects everything under the Canvas, including font sizes and image borders.

Here, Fluckysan wants to scale his GameObjects.

avatar image Fluckysan · Jun 18, 2015 at 11:55 AM 0
Share

Woah @highpockets, according to your description, it's a lot that I'm looking for (compute pixel size of a GameObject) I'll look into it, don't hesitate to post it as an answer, I'm sure a lot off people will be interested (and if it fit my needs, I'll accept it as an answer)

avatar image highpockets · Jun 18, 2015 at 11:59 AM 0
Share

Ok, I'll modify the code to look better

2 Replies

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

Answer by highpockets · Jun 18, 2015 at 11:48 AM

I made a script for detecting a game objects pixel scale according to screen size. Just add this to your game objects to find the pixel width and length based on the device.

 void Start ()
      {
 
      
          float depth = gameObject.transform.lossyScale.z;
          float width = gameObject.transform.lossyScale.x;
          float height = gameObject.transform.lossyScale.y;
 
          Vector3 lowerLeftPoint = Camera.main.WorldToScreenPoint( new Vector3( gameObject.transform.position.x - width/2, gameObject.transform.position.y - height/2, gameObject.transform.position.z - depth/2 ) );
          Vector3 upperRightPoint = Camera.main.WorldToScreenPoint( new Vector3( gameObject.transform.position.x + width/2, gameObject.transform.position.y + height/2, gameObject.transform.position.z - depth/2 ) );
          Vector3 upperLeftPoint = Camera.main.WorldToScreenPoint( new Vector3( gameObject.transform.position.x - width/2, gameObject.transform.position.y + height/2, gameObject.transform.position.z - depth/2 ) );
          Vector3 lowerRightPoint = Camera.main.WorldToScreenPoint( new Vector3( gameObject.transform.position.x + width/2, gameObject.transform.position.y - height/2, gameObject.transform.position.z - depth/2 ) );
 
          float xPixelDistance = Mathf.Abs( lowerLeftPoint.x - upperRightPoint.x );
          float yPixelDistance = Mathf.Abs ( lowerLeftPoint.y - upperRightPoint.y );
 
          print( "This is the X pixel distance: " + xPixelDistance + " This is the Y pixel distance: " + yPixelDistance );
          print ("This is the lower left pixel point: " + lowerLeftPoint);
          print(" This is the upper left point: " + upperLeftPoint );
          print ("This is the lower right pixel point: " + lowerRightPoint);
          print(" This is the upper right pixel point: " + upperRightPoint);
 
      }

I used this and then adjusted my game objects manually accordingly because although it proves as a good guide to know how much you need to adjust everything, I think it is best to manually adjust the scale/position because to do this by code will take unnecessary overhead, but if you just take a bit of time and do it manually, you will end up with exactly what you want at no cost, but your time. On the other hand, if you want to do it manually maybe this will give you a head start.

Unfortunately it doesn't allow me to post nice looking code as a comment and I didn't want to answer as I'm not sure if this completely answers your question..

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 BMRG14 · Aug 13, 2016 at 04:17 AM 0
Share

Thank you very very much! This works perfectly for me, but with a little change... You divided everything by 2, I did that by 6 and got the best result. I don't know why 6, but it worked! :/

avatar image MESSI007 · Mar 17, 2017 at 01:38 PM 0
Share

thank you ! :D

avatar image fonko · Aug 03, 2018 at 05:58 PM 0
Share

do you guys know how to set all positions according to the screen size? i have a game with autogenerated platforms and the player spawns in the air for some screen sizes and in the platform for other sizes... what should i do?

avatar image
0

Answer by hiteshthummar99 · Aug 31, 2018 at 02:09 PM

@highpockets How can i set scale for object base on screen size? suppose i want to add 10 box prefab programatically in raw so how i can set scale to fit different screens.

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

8 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Transform scale evenly from center to specific position 1 Answer

Distribute terrain in zones 3 Answers

When raising object/s to one direction how can I calculate the last position of the last scaled part of the object/s ? 0 Answers

I tried to make a box to select units like you do in windows 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