Source Code For Vertical Fit
Use this code for when you want the the left and right edges of the area to touch the camera edges.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraScript : MonoBehaviour {
public SpriteRenderer rink;
// Use this for initialization
void Start () {
Camera.main.orthographicSize = rink.bounds.size.x * Screen.height / Screen.width * 0.5f;
}
}
Source Code For Horizontal Fit
Use this code for when you want the top and bottom edges of the area to touch the camera edges.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraScript : MonoBehaviour {
public SpriteRenderer rink;
// Use this for initialization
void Start () {
Camera.main.orthographicSize = rink.bounds.size.y / 2;
}
}
Source Code For Entire Fit
Use this code when you want to calculate so the entire area is in view.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraScript : MonoBehaviour {
public SpriteRenderer rink;
// Use this for initialization
void Start () {
float screenRatio = (float)Screen.width / (float)Screen.height;
float targetRatio = rink.bounds.size.x / rink.bounds.size.y;
if(screenRatio >= targetRatio){
Camera.main.orthographicSize = rink.bounds.size.y / 2;
}else{
float differenceInSize = targetRatio / screenRatio;
Camera.main.orthographicSize = rink.bounds.size.y / 2 * differenceInSize;
}
}
}