모바일 환경에서 휴대폰이 정확히 직사각형 모양이 아니기때문에 해당 설정을 해줘야 ui가 안짤리고 나오게 된다.

using System.Collections;
using System.Collections.Generic;
using Sirenix.OdinInspector;
using UnityEngine;
using UnityEngine.UI;

public class SafeArea : MonoBehaviour
{
    RectTransform rt;
    private void Awake()
    {
        if(Application.platform == RuntimePlatform.IPhonePlayer){
            rt = GetComponent<RectTransform>();
            Rect safeArea = Screen.safeArea;
            Vector2 minAnchor = safeArea.position / 2;
            Vector2 maxAnchor = minAnchor * 3 + safeArea.size;

            float offsetRight = Screen.width - safeArea.xMax;
            float offsetLeft = safeArea.xMin;

            float correctionValue = (offsetRight - offsetLeft) / 2;

            minAnchor.x /= Screen.width;
            minAnchor.y /= Screen.height;
            maxAnchor.x /= Screen.width;
            maxAnchor.y /= Screen.height;

            minAnchor.x += correctionValue / Screen.width;
            maxAnchor.x += correctionValue / Screen.width;

            //y는 반영 안함
            minAnchor.y = 0;
            maxAnchor.y = 1;

            rt.anchorMin = minAnchor;
            rt.anchorMax = maxAnchor;
        }
        else{
            rt = GetComponent<RectTransform>();
            Rect safeArea = Screen.safeArea;
            Vector2 minAnchor = safeArea.position;
            Vector2 maxAnchor = minAnchor + safeArea.size;

            float offsetRight = Screen.width - safeArea.xMax;
            float offsetLeft = safeArea.xMin;

            bool isLeftNotch = offsetLeft >= offsetRight;
            


            //float correctionValue = (offsetRight - offsetLeft) / 2;

            minAnchor.x /= Screen.width;
            minAnchor.y /= Screen.height;
            maxAnchor.x /= Screen.width;
            maxAnchor.y /= Screen.height;

            if(isLeftNotch){
                maxAnchor.x -= (offsetLeft-offsetRight) / Screen.width;
            }
            else{
                minAnchor.x += (offsetRight-offsetLeft) / Screen.width;
            }

            //y는 반영 안함
            minAnchor.y = 0;
            maxAnchor.y = 1;

            rt.anchorMin = minAnchor;
            rt.anchorMax = maxAnchor;
        }

        

    }

    private void Start() {
        
        LayoutRebuilder.ForceRebuildLayoutImmediate(rt);
    }

    
}

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다