How do Windows Store apps built from Unity support changing the window size?

Asked 1 years ago, Updated 1 years ago, 82 views

iThis is a problem that you encounter when porting Unity titles developed for iOS and Android as Windows Store apps.

Windows 8.1 allows you to change the window size of the Windows Store app, but when you run the Windows Store app that you built from Unity and change the window size, both sides of the screen appear disconnected.

US>Full Screen Status
Full Screen State Image

Changed window size
Window size changed

What code should I write in Unity C#Script to keep the original aspect ratio intact?

windows unity3d windows-store-apps

2022-09-30 19:17

1 Answers

To work as expected, add C#Script to one of the GameObjects that will not be deleted during play, such as Terrain. Implement the UnityEngine.WSA.Application.windowSizeChanged event handler.For some reason, it is not mentioned in the Unity document.

Assuming that the main camera is named mainCamera, you can resolve it with the following code: Keep the aspect in place when the device is turned.

http://gamedesigntheory.blogspot.jp/2010/09/controlling-aspect-ratio-in-unity.html Reference the code in the .

void Start()
{
# if UNITY_METRO
    UnityEngine.WSA.Application.windowSizeChanged+=Application_windowSizeChanged;
#endif
}

void Application_windowSizeChanged(int width, int height)
{
    // set the desired aspect ratio (the values in this example are)
    // hard-coded for 16:9, but you could make them into public
    // variables installed so you can set them at design time)
    float targetaspect = 16.0f/9.0f;

    // determine the game window's current aspect ratio
    float windowaspect=(float)width/(float)height;

    // current viewport height should be scaled by this amount
    float scaleheight=windowaspect/targetaspect;

    // obtain camera component so we can modify its viewport
    Camera camera=GameObject.Find("mainCamera").camera;
    if(camera==null)
    {
        return;
    }

    // if scaled height is less than current height, add letterbox
    if(scaleheight<1.0f)
    {
        Correct = camera.rect;

        rect.width = 1.0f;
        rect.height = scaleheight;
        rect.x = 0;
        rect.y = (1.0f-scaleheight)/2.0f;

        camera.rect=rect;
    }
    else//add pillarbox
    {
        float scalewidth = 1.0f/scaleheight;

        Correct = camera.rect;

        rect.width = scalewidth;
        rect.height=1.0f;
        rect.x = (1.0f-scalewidth)/2.0f;
        rect.y = 0;

        camera.rect=rect;
    }

}


2022-09-30 19:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.