画面を解像度に合わせる

スマホやタブレットの画面サイズは様々なので
機種によっては思わぬエラーを発生する可能性があります
そうならないように 画像を画面に合わせるようにしましょう

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MainCamera : MonoBehaviour
{
    void Awake()
    {
        Camera cam = gameObject.GetComponent<Camera>();

        // 理想の画面の比率
        float targetRatio = 16f / 9f;
        // 現在の画面の比率
        float currentRatio = Screen.width * 1f / Screen.height;
        // 理想と現在の比率
        float ratio = targetRatio / currentRatio;

        //カメラの描画開始位置をX座標にどのくらいずらすか
        float rectX = (1.0f - ratio) / 2f;
        //カメラの描画開始位置と表示領域の設定
        cam.rect = new Rect(rectX, 0f, ratio, 1f);
    }
}

以下のスクリプトをMainCameraに入れると 比率16/9に合わせるように画面が変わり
足りない所は黒帯で埋めてくれます

戻る