http://qiita.com/kwst/items/371542a6d3892b577b41
サンプルコードをそのまま使わせていただいています…
スクリプト記述後、メインカメラにアタッチ。
public class MainCamera : MonoBehaviour {
private new Camera camera;
private float width = 640f;
private float height = 480f;
private float pixelPerUnit = 100f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Awake ();
}
void Awake () {
float aspect = (float)Screen.height / (float)Screen.width;
float bgAcpect = height / width;
// カメラコンポーネントを取得します
camera = GetComponent ();
// カメラのorthographicSizeを設定
camera.orthographicSize = (height / 2f / pixelPerUnit);
if (bgAcpect > aspect) {
// 倍率
float bgScale = height / Screen.height;
// viewport rectの幅
float camWidth = width / (Screen.width * bgScale);
// viewportRectを設定
camera.rect = new Rect ((1f - camWidth) / 2f, 0f, camWidth, 1f);
} else {
// 倍率
float bgScale = width / Screen.width;
// viewport rectの幅
float camHeight = height / (Screen.height * bgScale);
// viewportRectを設定
camera.rect = new Rect (0f, (1f - camHeight) / 2f, 1f, camHeight);
}
}
}