Created
June 11, 2015 08:32
-
-
Save yasuakiohama/36e04f054b797017d13b to your computer and use it in GitHub Desktop.
FixedAspectRatio.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class FixedAspectRatio : MonoBehaviour | |
{ | |
public Camera cam = null; // アスペクト比を固定化するカメラ | |
public float width = 1920.0f; // 横幅 (目標解像度) | |
public float height = 1080.0f; // 高さ (目標解像度) | |
private Rect camRect = new Rect(); | |
void Update() | |
{ | |
float targetAspect; // 目標のアスペクト比 | |
float curAspect; // 補正前の「Scene」のアスペクト比 | |
float ratio; // 補正前の「Scene」のアスペクト比 ÷ 目標のアスペクト比 | |
targetAspect = width / height; | |
// 補正前の「Scene」の横幅・縦幅はSceneのメンバ変数から取得可能 | |
curAspect = Screen.width * 1.0f / Screen.height; | |
ratio = curAspect / targetAspect; | |
// 表示領域の横幅・高さ・左上のXY座標をセット | |
// 横長の場合 | |
if (1.0f > ratio) { | |
camRect.x = 0.0f; | |
camRect.width = 1.0f; | |
camRect.y = (1.0f - ratio) / 2.0f; | |
camRect.height = ratio; | |
cam.orthographicSize = Screen.width / 2.0f; | |
} | |
// 縦長の場合 | |
else { | |
ratio = 1.0f / ratio; | |
camRect.x = (1.0f - ratio) / 2.0f; | |
camRect.width = ratio; | |
camRect.y = 0.0f; | |
camRect.height = 1.0f; | |
cam.orthographicSize = Screen.height / 2.0f; | |
} | |
cam.rect = camRect; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment