2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Unity] 画面比率に合わせて背景を全画面に引き延ばす

Last updated at Posted at 2020-03-06

1. 注意点

・Canvas Scaler(Script)を外す。
・適用したいImage(Script)があるオブジェクトに下記のスクリプトをアタッチする。
・isAllScreenをInspector上でtrueにする事で使用できる

2. コード

BackGroundScaler.cs
using UnityEngine;

public class BackGroundScaler : MonoBehaviour
{
    /// <summary>
    /// 縦横比を無視して全画面にするか
    /// </summary>
    [SerializeField]
    private bool isAllScreen = true;


    private void Start()
    {
        Vector2 standardResolution = GetComponent<RectTransform>().sizeDelta;

        if (isAllScreen)
        {
            //縦横比率を変更してでも全画面に合わせる。
            float magnification_x = 0;
            float magnification_y = 0;

            float height = Screen.height;
            magnification_y = height / standardResolution.y;
            float width = Screen.width;
            magnification_x = width / standardResolution.x;

            GetComponent<RectTransform>().sizeDelta = new Vector2(standardResolution.x * magnification_x, standardResolution.y * magnification_y);
        }
    }
}


2
3
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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?