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!

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);
	}