2
0

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 2021-04-04

「画面いっぱいにオブジェクト表示」 とは?

文字通りなのだが、カメラの描写領域ぴったりまでオブジェクトを拡大・縮小するというもの。
はみ出したり、忖度したりするのはダメ!

★結論

3Dでは、オブジェクトの形状によるけど、画角とか考慮せねばならず難易度が高い。
2Dは比較的簡単。

0.実行環境について

Unityバージョン:2019.4.18f1

Visual Studio Community 2019バージョン:16.9.1

1.スクリプト

SizeControll.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SizeControll : MonoBehaviour
{
    private Camera m_MainCamera; // メインカメラ
    Vector3 BottomLeft;//左下
    Vector3 TopRight; //右上
    float Width; //x座標系、幅
    float Height; //y座標系、高さ
    float ObjectWidth; //オブジェクトの幅
    float ObjectHeight; //オブジェクトの高さ
    // Start is called before the first frame update

    void Start()
    {
        m_MainCamera = Camera.main;
        //カメラ領域の左下の座標を取得
        BottomLeft = m_MainCamera.ScreenToWorldPoint(Vector3.zero);
        // カメラ領域の右上の座標を取得
        TopRight = m_MainCamera.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, 0.0f));
        //オブジェクトの幅・高さを取得
        ObjectWidth = this.GetComponent<MeshRenderer>().bounds.size.x;
        ObjectHeight = this.GetComponent<MeshRenderer>().bounds.size.y;
        //カメラの領域の幅・高さをワールド座標系数値で取得
        Width = (TopRight.x - BottomLeft.x) ;
        Height = (TopRight.y - BottomLeft.y) ;
        //上記二値からスケール(ローカル座標系)を調整
        transform.localScale = new Vector3(Width/ ObjectWidth, Height/ ObjectHeight, 0);
    }
}

これを単なるCubeにアタッチしてみます

スクリプトをアタッチ後、プレイモードにするとこんな感じ。

以上です。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?