{Programing}/Game Engine

Unity - 모바일 해상도 고정 대응.

탱타로케이 2020. 6. 12. 14:43

Screen.SetResolution( width, Height, fullscreen)

으로 픽셀 해상도 고정 가능.

풀스크린 모드의 차이는 true 일때 화면 아래에 나오는 뒤로가기 홈버튼 이 나오지 않고

false일 때는 표시함.

 

void ResolutionFix()
    {

        float targetWidthAps = 16.0f;
        float targetHeightAps = 9.0f;

        Camera.main.aspect = targetWidthAps / targetHeightAps;

        float widthRatio = (float)Screen.width / targetWidthAps;
        float heightRatio = (float)Screen.height / targetHeightAps;

        float widthadd = ((heightRatio / (widthRatio / 100)) - 100) / 200;
        float heightadd = ((widthRatio / (heightRatio / 100)) - 100) / 200;

        if(heightRatio>widthRatio)
        {
            widthRatio = 0.0f;
        }
        else
        {
            heightRatio = 0.0f;
        }

        Camera.main.rect = new Rect(
            Camera.main.rect.x + Mathf.Abs(widthadd),
            Camera.main.rect.x + Mathf.Abs(heightadd),
            Camera.main.rect.width + (widthadd * 2),
            Camera.main.rect.height + (heightadd * 2));
    }

위 함수 같이 비율 계산을 통해 카메라에 보이는 화면을 잘라내어 비율 고정.

 

UI를 제외한 모든 화면비율을 자르는 것.