Unity3d Remove screen black edge when full screen

Source: Internet
Author: User

Give full-screen after not care about stretching deformation still want to let the screen occupied a friend, online search for a morning, there is no relevant information, can only be self-pondering.

Using canvas scaler after full screen unity will automatically stretch the UI for us, but still retain the aspect ratio that we set in unity after stretching. Black edges in the screen require code to stretch the contents of the UI.

Here's my test environment:

Unity3d 5.3.4

Win 64bit

In my test, unity was set to automatically stretch according to the width of the screen, so that I needed to adjust the height of the UI to fill the black edges that appeared up and down the screen.

The resolution used in development is 16:9, but the actual release will be run on 5:3,16:10 or even 2:1 of the monitor, so unity will automatically stretch horizontally, depending on the settings above, but black edges will appear on the top and bottom of the screen.

Hang the following code in the start function of a script on a camera or canvas, and the UI will automatically pull up and down to fill out the black edges of the upper and lower parts of the screen.

        Standarratio = new Vector3[rechangeforms.length];        float referenceratio = 16f/9f;        float Currentratio = ((float) screen.width/  (float) screen.height);        for (int i = 0; i < rechangeforms.length; i++)        {            standarratio[i] = Rechangeforms[i].transform.localscale;            float Yfactor = standarratio[i].y * (referenceratio/currentratio);            float Posyfactor = rechangeforms[i].transform.position.y * (referenceratio/currentratio);            Rechangeforms[i].transform. Doscaley (Yfactor, 0.0f);            Rechangeforms[i].transform. Domovey (Posyfactor, 0.0f);        }    

Standarratio is an array of Vector3 that stores the value of the scale of the element before it is stretched, Rechangeforms is a collection of elements that need to be stretched, and is a gameobject array.

The code is populated with black edges by adjusting the value of y in the UI element's scale.

Mathematical formula used: The new yscale= primitive yscale* (ratio of reference resolution/actual resolution).

An additional problem here is that after adjusting the Y value of the UI element's scale, the center point of the element is changed to the left, so it needs to be adjusted accordingly. The formula is the same. (This may also be due to the reason for my own project)

After completing the above steps, you can switch to a variety of Billy's displays in Unity's preview window to test, and you can see that the black edges that have been stretched have been populated by the UI elements that have been extruded (although the UI is distorted, but the black Edge is gone).

Unity3d Remove screen black edge when full screen

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.