일지
유니티 화면 비율 고정 처리...16
niamdank
2021. 10. 22. 02:24
특이한 변수가 사용되는 코드 확인
어제 확인한 변수가 사용되는 부분을 확인하여 코드를 파악한다.
- 최소 픽셀 크기와 최대 픽셀 크기
Clamp를 이용하여 화면 크기가 정해진 크기보다 커지지 않도록 제한한다.
int newWidth = Mathf.Clamp(rc.Right - rc.Left, minWidthPixel, maxWidthPixel);
int newHeight = Mathf.Clamp(rc.Bottom - rc.Top, minHeightPixel, maxHeightPixel);
- 전체 화면 제한 플래그와 이전 프레임의 전체 화면 여부
전체 화면 제한 플래그는 창 화면만 지원하는 게임을 위해 존재하는 것으로 보이며 전체 화면이 되었을 때 다시 창 화면으로 되돌리는 역할을 한다.
if (!allowFullscreen && Screen.fullScreen)
{
Screen.fullScreen = false;
}
이전 프레임의 전체 화면 여부는 전체 화면에서 창 화면으로 돌아올 때 화면 비율에 맞게 되돌리기 위해 존재하는 것으로 보인다. 그런데 이 부분은 Size 이벤트를 인터셉트하는 방식이라 없어도 되지 않을까 싶기도 하다. 테스트해봐야겠다.
else if (!Screen.fullScreen && wasFullscreenLastFrame)
{
// Switch from fullscreen to window detected. Set previous window resolution.
Screen.SetResolution(setWidth, setHeight, false);
resolutionChangedEvent.Invoke(setWidth, setHeight, false);
}
- 사용자가 변경한 화면 크기
이 부분은 WinProc 내부에서 변경된 크기를 가져온 뒤 Update 함수에서 SetResolution 처리를 진행하기 위해 존재하는 것 같다.
else if (!Screen.fullScreen && setWidth != -1 && setHeight != -1 && (Screen.width != setWidth || Screen.height != setHeight))
{
// Aero Snap detected. Set width by height.
// Necessary because Aero Snap doesn't trigger WM_SIZING.
setHeight = Screen.height;
setWidth = Mathf.RoundToInt(Screen.height * aspect);
Screen.SetResolution(setWidth, setHeight, Screen.fullScreen);
resolutionChangedEvent.Invoke(setWidth, setHeight, Screen.fullScreen);
}
- 종료 체크 플래그
종료가 될 때 연결했던 WinProc을 해제하기 위해 종료를 지연하기 위한 플래그로 존재한다.
IEnumerator DelayedQuit()
{
// Re-set old WindowProc callback. Normally, this would be done in the new callback itself
// once WM_CLOSE is detected. This seems to work fine on 64 bit, but when I build 32 bit
// executables, this causes the application to crash on quitting.
// This shouldn't really happen and I'm not sure why it does.
// However, this solution right here seems to work fine on both 32 and 64 bit.
SetWindowLong(unityHWnd, GWLP_WNDPROC, oldWndProcPtr);
// Wait for end of frame (our callback is now un-registered), then allow application to quit.
yield return new WaitForEndOfFrame();
quitStarted = true;
Application.Quit();
}
유니티 함수는 크게 어려운 부분이 없으므로 WinProc을 등록하는 부분과 WinProc 함수를 위주로 분석하면 될 것 같다.