- Home /
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
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
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...
@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
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;
Will keep that in $$anonymous$$d @robertbu Thanks !!
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??
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.
Your answer
Follow this Question
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