質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

1回答

2425閲覧

unity 画面サイズ調整について

hikaaaaaaaa

総合スコア19

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2020/07/22 04:21

編集2020/07/23 01:26

unityにてゲーム開発をしております。

動画を組み込んでおり、動画のサイズを1242×2688で作成しており
他の背景画像も同じ1242×2688で作成をしております。

アプリの下部にバナー広告を実装しようとしており、
サイトを参考にスクリプトを作成し、1242×2688を基準にリサイズ(1242×2208等)を試みましたが、縦の比率は固定で反映されるのですが
背景の横の部分が黒くなってしまいます。

黒い部分をなくし、他のサイズでも同じ画角での実装をするにはどのようにしたらできるのでしょうか。
アドバイスをいただけますと幸いです。

図を添付させていただきます。

現在1242x2688で組み込みを行い、1242x2208にすると縦の大きさは1242x2688の比率でサイズが調整がされるのですが、左右の部分が黒くなってしまいます。
(Unityのデフォルトだと青だと思います。)

理想は1242x2688のサイズの配置を黒い部分なく、縦横の比率を変えずに1242x2208の画面内におさめたいです。

カメラにアタッチ

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5[ExecuteInEditMode()] 6[RequireComponent(typeof(Camera))] 7public class CameraStableAspect : MonoBehaviour 8{ 9 [SerializeField] 10 Camera refCamera; 11 12 [SerializeField] 13 int width = 1242; 14 15 [SerializeField] 16 int height = 2688; 17 18 [SerializeField] 19 float pixelPerUnit = 100f; 20 21 22 int m_width = -1; 23 int m_height = -1; 24 25 void Awake() 26 { 27 if (refCamera == null) 28 { 29 refCamera = GetComponent<Camera>(); 30 } 31 UpdateCamera(); 32 } 33 34 private void Update() 35 { 36 UpdateCameraWithCheck(); 37 } 38 39 void UpdateCameraWithCheck() 40 { 41 if (m_width == Screen.width && m_height == Screen.height) 42 { 43 return; 44 } 45 UpdateCamera(); 46 } 47 48 void UpdateCamera() 49 { 50 float screen_w = (float)Screen.width; 51 float screen_h = (float)Screen.height; 52 float target_w = (float)width; 53 float target_h = (float)height; 54 55 //アスペクト比 56 float aspect = screen_w / screen_h; 57 float targetAcpect = target_w / target_h; 58 float orthographicSize = (target_h / 2f / pixelPerUnit); 59 60 //縦に長い 61 if (aspect < targetAcpect) 62 { 63 float bgScale_w = target_w / screen_w; 64 float camHeight = target_h / (screen_h * bgScale_w); 65 refCamera.rect = new Rect(0f, (1f - camHeight) * 0.5f, 1f, camHeight); 66 } 67 // 横に長い 68 else 69 { 70 // カメラのorthographicSizeを横の長さに合わせて設定しなおす 71 float bgScale = aspect / targetAcpect; 72 orthographicSize *= bgScale; 73 74 float bgScale_h = target_h / screen_h; 75 float camWidth = target_w / (screen_w * bgScale_h); 76 refCamera.rect = new Rect((1f - camWidth) * 0.5f, 0f, camWidth, 1f); 77 } 78 79 refCamera.orthographicSize = orthographicSize; 80 81 m_width = Screen.width; 82 m_height = Screen.height; 83 } 84}

Panaleを作成しアタッチ

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5[ExecuteInEditMode()] 6public class RectScalerWithViewport : MonoBehaviour 7{ 8 [SerializeField] 9 RectTransform refRect = null; 10 11 [SerializeField] 12 Vector2 referenceResolution = new Vector2(1242, 2688); 13 14 [Range(0, 1)] 15 [SerializeField] float matchWidthOrHeight = 0; 16 17 float m_width = -1; 18 float m_height = -1; 19 20 private const float kLogBase = 2; 21 22 private void Awake() 23 { 24 if (refRect == null) 25 { 26 refRect = GetComponent<RectTransform>(); 27 } 28 UpdateRect(); 29 } 30 31 private void Update() 32 { 33 UpdateRectWithCheck(); 34 } 35 36 private void OnValidate() 37 { 38 UpdateRect(); 39 } 40 41 void UpdateRectWithCheck() 42 { 43 Camera cam = Camera.main; 44 float width = cam.rect.width * Screen.width; 45 float height = cam.rect.height * Screen.height; 46 if (m_width == width && m_height == height) 47 { 48 return; 49 } 50 UpdateRect(); 51 } 52 53 void UpdateRect() 54 { 55 Camera cam = Camera.main; 56 float width = cam.rect.width * Screen.width; 57 float height = cam.rect.height * Screen.height; 58 m_width = width; 59 m_height = height; 60 61 // canvas scalerから引用 62 float logWidth = Mathf.Log(width / referenceResolution.x, kLogBase); 63 float logHeight = Mathf.Log(height / referenceResolution.y, kLogBase); 64 float logWeightedAverage = Mathf.Lerp(logWidth, logHeight, matchWidthOrHeight); 65 float scale = Mathf.Pow(kLogBase, logWeightedAverage); 66 67 refRect.localScale = new Vector3(scale, scale, scale); 68 69 // スケールで縮まるので領域だけ広げる 70 float revScale = 1f / scale; 71 refRect.sizeDelta = new Vector2(m_width * revScale, m_height * revScale); 72 } 73}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

sakura_hana

2020/07/23 00:09

どのような状態になっていて、どのような状態になってほしいのかよくわからないのでスクリーンショットや図を載せてもらえますか?
hikaaaaaaaa

2020/07/23 01:26 編集

返信ありがとうございます。 図を追加させていただきました。 現在1242x2688で組み込みを行い、1242x2208にすると縦の大きさは1242x2688の比率でサイズが調整がされるのですが、左右の部分が黒くなってしまいます。 (Unityのデフォルトだと青だと思います。) 理想は1242x2688のサイズの配置を黒い部分なく、縦横の比率を変えずに1242x2208の画面内におさめたいです。
sakura_hana

2020/07/23 02:40

そのサイズで縦横比を変えずに表示する場合、「左右に余白を入れる」か「上下を画面外にはみ出させる」しかありません。上下はみ出しの方がいいということでしょうか?
hikaaaaaaaa

2020/07/23 03:53

上下はみ出しのほうが良いです。。
IShix

2020/07/25 10:07

2Dと3Dどちらですか?
guest

回答1

0

今使っているものを共有します

CameraStableAspectRectScalerWithViewportを一旦停止して下記スクリプトを試してみてください。

9:16または16:9を基準にCamera.aspectで調整する方法です。
動画を使ったことが無いのでどうなるか分かりませんが、一度お試しください。

サンプル

動画ではなく画像を配置したサンプルです。

1242×2688
![イメージ説明

1242×2208
イメージ説明

ダウンロード

今回のサンプルを共有します。
AspectFitCameraTest.unitypackage 保存期間60日

使い方

  1. メインカメラにAspectFitCameraをアタッチ
  2. カメラのSizeを11にする

イメージ説明
3. 再生して確認 ※再生中のみ動作します。

カメラにアタッチするスクリプト

C#

1using System; 2using System.Diagnostics; 3using UnityEngine; 4using UnityEngine.Assertions; 5 6public sealed class AspectFitCamera : MonoBehaviour 7{ 8 /// <summary> 9 /// 基準となるアスペクト ※必ずportalの値を入れる 10 /// </summary> 11 public static readonly Vector2 DEFAULT_ASPECT = new Vector2(9, 16); 12 /// <summary> 13 /// ターゲットとなるカメラ 14 /// </summary> 15 [SerializeField] Camera targetCamera; 16 /// <summary> 17 /// (Editor Only) 再生中にアスペクト比が変わった場合対応するか? 18 /// </summary> 19 [SerializeField] bool isUpdateOnRuntime = true; 20 21 float startOrthographicSize; 22 float defaultAspect; 23 float currentAspect; 24 25 void Awake() 26 { 27 Assert.IsTrue(targetCamera.orthographic, "Cameraの設定をorthographicにしてください"); 28 startOrthographicSize = targetCamera.orthographicSize; 29 // 端末がlandscapeの場合逆にする 30 defaultAspect = Screen.width > Screen.height 31 ? DEFAULT_ASPECT.y / DEFAULT_ASPECT.x 32 : DEFAULT_ASPECT.x / DEFAULT_ASPECT.y; 33 } 34 35 void Start() 36 { 37 Apply(); 38 } 39 40 // エディタ上のみUpdateを機能させる 41 [Conditional("UNITY_EDITOR")] 42 void Update() 43 { 44 if(isUpdateOnRuntime) 45 { 46 Apply(); 47 } 48 } 49 50 void Apply() 51 { 52 if(Math.Abs(targetCamera.aspect - currentAspect) > 0) 53 { 54 currentAspect = targetCamera.aspect; 55 var r = defaultAspect / targetCamera.aspect; 56 targetCamera.orthographicSize = startOrthographicSize * r; 57 } 58 } 59 60 void Reset() 61 { 62 targetCamera = Camera.main; 63 } 64} 65 66

Canvasの設定

Expandに設定すると良いです
イメージ説明

投稿2020/07/26 06:39

編集2020/07/26 06:42
IShix

総合スコア1724

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問