LoginSignup
1
0

More than 1 year has passed since last update.

【Unity】SRDebuggerのセーフエリア対応

Last updated at Posted at 2021-05-15

Unityのデバッグ用アセットとして導入する機会の多いSRDebuggerですが、2021年5月現在セーフエリアに対応されていないため、iPhone Xのようなディスプレイの形状が長方形ではないデバイスでのデバッグ時に少しだけ不便です。

※追記
2021年6月3日にリリースされた1.11.0でセーフエリア対応が入ったため、それ以降のバージョンを使用する場合はこの記事の対応は不要です。

画面上部の表示の一部がノッチの後ろに隠れますし、右上の閉じるボタンは押せなくはないのですがとても押しづらかったりします。

そこで、アセット内のスクリプトに処理を追加してなるべく簡単に解決します。

環境

  • Unity: 2021.1.6f1
  • SRDebugger: 1.10.0
  • macOS: Big Sur 11.3.1 (20E241)

追加するコード

DebugPanelRoot.cs内にAwakeを追加し、プレハブのインスタンス化直後のタイミングでフレームサイズを変更します。

Assets/StompyRobot/SRDebugger/Scripts/UI/DebugPanelRoot.cs
        protected override void Awake()
        {
            base.Awake();

            var panel = transform.GetComponentInChildren<Canvas>()?
                .transform.GetChild(0)?
                .gameObject?
                .GetComponent<RectTransform>();

            if (panel) {
                var area = Screen.safeArea;
                var anchorMin = area.position;
                var anchorMax = area.position + area.size;
                anchorMin.x /= Screen.width;
                anchorMin.y /= Screen.height;
                anchorMax.x /= Screen.width;
                anchorMax.y /= Screen.height;
                panel.anchorMin = anchorMin;
                panel.anchorMax = anchorMax;
            }
        }

対応結果

画面内に収まってスッキリしました。

簡単な対応なので、デバイスの向きを変更して画面が回転した時には上下左右のマージンが変化しません。Input.deviceOrientationを監視して向きが変わった時にフレームサイズを再計算する必要があります。

また、メインパネルのみの対応です。他にもいくつかのUIがありますが、今回の追加処理ではそこまでは影響しません。

もっとも、デバッグでしか使わないということもあるので上記2点は無視しても問題ないと思います。

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