Unity implements UI adaptation

When doing UI functions, we must pay attention to the adaptation of the screen. Different mobile devices have different resolutions.

So adaptive resolution is essential

Let's take a look at Unity's own adaptive function

1. It is Canvas

2. The Canvas Scaler component that comes with Canvas

3. Select the specific as shown in the figure

4. Is the width and height based on the on-site resolution

5. The following Match is based on the door-to-door (it is recommended that if the horizontal screen is high, the vertical screen is the opposite, and the square is recommended to look at personal needs in the middle)

But after setting this way, you will find that many UI subsets or many UIs are still the original size, so you need to use code to adapt

code show as below

You can also use the package method to use

    private void Awake()
    {
        float p1 = (float)Screen.width / 1028f;
        float p2 = (float)Screen.height / 600f;
        float p = p1 < p2 ? p1 : p2;
        transform.localScale = Vector3.one * p;
    }

Guess you like

Origin blog.csdn.net/q1295006114/article/details/130371277