「画面いっぱいにオブジェクト表示」 とは?
文字通りなのだが、カメラの描写領域ぴったりまでオブジェクトを拡大・縮小するというもの。
はみ出したり、忖度したりするのはダメ!
★結論
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);
}
}
スクリプトをアタッチ後、プレイモードにするとこんな感じ。
以上です。
