It is just about a background that needs to be exactly scaled to the seen space.
The environment is 2D and the camera projection orthographic.
I tried a bit around with Screen.height, the orthographic size isn’t regarded here, but I can’t figure out the formula to get the actual size of the seen space.
I need it for the background, because the game works basically with big cells that will be displayed on the texture, so I need it to be exact.
Something I slabbed together quickly. It might work.
// I'm assuming the the sprite is centering on the camera since it's a background you want to scale.
// That is GameObject Position is at (0,0,*) while camera is at (0,0,*) as well.
public static void fitToCamera(GameObject obj)
{
float height = Camera.main.orthographicSize;
float width = height * Camera.main.aspect;
Vector3 extents;
if (obj.GetComponent<MeshFilter>() != null) { // If it's a Mesh
extents = obj.GetComponent<MeshFilter>().sharedMesh.bounds.extents;
}
else if (obj.GetComponent<SpriteRenderer>() != null)
{ // If it's a Sprite
extents = obj.GetComponent<SpriteRenderer>().bounds.extents;
}
else {
// How is your 2D gameObject being rendered really...?
extents = new Vector3(width, height, 1); // Incase you're not using Mesh or Renderer.
}
Vector3 scale = new Vector3(width / extents.x, height / extents.y, 1);
obj.transform.localScale = scale;
}