LoginSignup
0
0

More than 5 years have passed since last update.

スクリーンサイズからCanvasサイズを調整する

Last updated at Posted at 2018-10-03

引用:How to add the iPhone X screen dimensions to the mix of different iPhone dimensions in Unity

Canvas > Canvas Scaler > UI Scale ModeConstant Pixel Size に設定。

スクリーンショット 2018-10-03 12.05.35.png

Scale Factor を調整する以下のコードをCanvasにアタッチする。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CanvasScreenAutoFix : MonoBehaviour
{
    public static CanvasScreenAutoFix instance;

    private static float DEPEND_W = 1080;
    private static float DEPEND_H = 1920;

    public float scaleRatio = 1.0f;

    private void ResizeCanvas()
    {
        int screenW = Screen.width;
        int screenH = Screen.height;
        if (DEPEND_W == screenW && DEPEND_H == screenH)
        {
            scaleRatio = 1.0f;
            return;
        }
        else
        {
            float W_scale = screenW / (float)DEPEND_W;
            float H_scale = screenH / (float)DEPEND_H;
            float scale = W_scale < H_scale ? W_scale : H_scale;

            GetComponent<CanvasScaler>().scaleFactor = scale;

            scaleRatio = scale;
        }
    }

    private void Awake()
    {
        ResizeCanvas();
        if(instance == null)
       {
           instance = this;
       }
    }

}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0