- Home /
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!
can you explain what you are trying to accomplish because there might be a simpler solution
Like I want this sprite image to be the background of my scene.
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
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);
}
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
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);
That's one way to do it. I changed the answer to show how to calculate the scale regardless of sprite settings.
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
Follow this Question
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