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
4
Question by 0mr_ashutosh0 · Nov 01, 2014 at 07:47 PM · scalescreen resolutionscreen sizedimensions

Scale gameobject to the width of screen

Here is my image in which the red box (Cube GameObject from unity - no sprite ) needs to have a width and height of 1/9 of screen width. For different screen width the cube's dimension will change accordingly.

I am using this way

 public GameObject cube;
 void start()
 {
 }
 
 void update()
 {
 
 //writing this code in update only to see the changes on the run :P
 
 float width = Screen.width;
 float dimension = width/9;
 
 cube.transform.localScale = new Vector3(dimension ,dimension ,dimension);
 }

 

Now the problem here is if my screen width is lets say 300 then dimension evaluates to 33.33 (300/9) and then my gameobject scales to 33 TIMES the original which i figure that it was 1 in the editor.

how can i scale my game object ( non-sprite or even image sprite ) in the unity 3D alt text

untitled2.png (5.5 kB)
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 robertbu · Nov 01, 2014 at 07:50 PM 1
Share

Perspective or orthographic camera? Note that with a perspective camera, the size of the object changes with the distance from the camera.

http://docs.unity3d.com/$$anonymous$$anual/FrustumSizeAtDistance.html

avatar image 0mr_ashutosh0 · Nov 01, 2014 at 07:51 PM 0
Share

orthographic camera it is @robertbu

1 Reply

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

Answer by robertbu · Nov 01, 2014 at 07:57 PM

For an orthographic camera, the Camera.orthographicSize is 1/2 of the height of the window seen by the camera. So the width a camera sees might be calculated by:

  var width = Camera.main.orthographicSize * 2.0 * Screen.width / Screen.height;

So to make a non-rotated cube 1/9 of the screen width (assuming no parent, or parents with localScale of (1,1,1)), you can set the localSize:

  transform.localScale = Vector3(width / 9.0, width / 9.0, width / 9.0);

For random game objects the code is more complicated. This code works with a non-rotated cube because it size is (1,1,1) when it localScale is (1,1,1), but that is not true of other object. You will need to get their world size. Depending on your app and how you want to define the size, you can use Collider.bounds, or Renderer.bounds, or Mesh.bounds or hard-code the data, or...

Comment
Add comment · Show 7 · 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 0mr_ashutosh0 · Nov 01, 2014 at 08:26 PM 0
Share

@robertbu THAN$$anonymous$$S IT WOR$$anonymous$$ED !! just some changes like using float and typecasting (im using C#)

Now for my second part

 cube = (GameObject)Instantiate(cube, new Vector3(0.0f,0.0f,0.0f), transform.rotation);

In here, how can i assign position with the help of pixel information i m getting ?

the idea is to add 4 such squares in screen each having distance in between them of 1 square. so after dividing screen in 9, 4 part for square and 5 for spaces. thanks

avatar image robertbu · Nov 02, 2014 at 12:04 AM 1
Share

Am i greedy

First, we like to keep each question on a single subject, so we don't like to answer follow-on questions in the same question. Second, we like to see people take a stab at solving their own problems, not write code from them. If this is the way you want to approach your problem, make a honest stab, and if you get stuck, post a new question.

With that said, there may be a magic bullet 'fix' to your problem related to the original question. Rather than sizing the cubes to the screen width, how about sizing the camera to fit the cubes. $$anonymous$$eep the cubes with a size of (1,1,1). Do the layout based on a unit grid, so that the four-across-with-spaces cubes would be 9 world units. To set the camera you can do:

  Camera.main.orthographicSize = 9.0f / Screen.width * Screen.height / 2.0f;


avatar image 0mr_ashutosh0 · Nov 02, 2014 at 12:27 PM 0
Share

Will keep that in $$anonymous$$d @robertbu Thanks !!

avatar image tpainton · May 17, 2015 at 07:11 PM 0
Share

Lots of questions on this, and everytime the answer is to change the camera. Isn't this a problem if you're only trying to scale one object and not every object in the scene??

avatar image ThangHQ · Aug 13, 2018 at 10:24 AM 0
Share

It's now 2018, but can I ask what if the camera is not orthographic? I have my game objects scaling depends on Screen.width, but it seems like the ratio is not exactly and the objects is just scaled a little bit, not equal to the change of the screen width.

void Scale$$anonymous$$arks() { Vector2 scale = gameObject.GetComponent<RectTransform>().localScale; scale.x *= Screen.width / screenWidth; gameObject.GetComponent<RectTransform>().localScale = scale; screenWidth = Screen.width; }

Here is my code, screenWidth is the width before changing.

Show more comments

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

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

Related Questions

How to make my UI items/canvas stretch to fill preset aspect ratio 0 Answers

multiple apks to match certain devices 0 Answers

2d game for different screen resolutions 1 Answer

Scale 3D object according to screen size on orthographic camera 2 Answers

View fits in landscape but not portrait ?! (Mobile) 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