Adaptation of Unity image resources

foreword

 

When I was working on a Unity project recently, I found that resource processing is different from Android; for example: Android resource folder res will have mipmap-mdpi, mipmap-hdpi, mipmap-xhdpi, mipmap-xxhdpi, mipmap-xxxhdpi These five folders are because the picture resources we usually download on the Blue Lake UI design map are pictures corresponding to these five resolutions, but in Unity, there is no concept of self-adaptation of the resolution of picture resources ;However, Unity has a Canvas Scaler component that can do adaptive processing;

The difference between Android and Unity loading resources

As a person who is used to writing Android, can you encapsulate a set of adaptive scripts for Image resolution? Of course, we can use Unity’s built-in resource loading function to do it. In Android, resources are basically stored in the res folder. We also use R.Layout to load the corresponding folder of resources to obtain resources.

Android loads resources

From the picture above, you can see that the picture resources in Android are placed in the mipmap folder, the resolutions of the five pictures are arranged in order from small to large, and the minmap folder is placed in the res file, and we are in When you want to load image resources in the UI statement, use R.minmap. image resource name, as shown in the figure below

In fact, there should be an ldpi folder, which forms a picture folder with low, medium and high resolutions;

screen dpi value (the number of pixels per inch of the length of the image);

In terms of dpi:

dpi range density
0dpi ~ 120dpi ldpi
120dpi ~ 160dpi mdpi
160dpi ~ 240dpi hdpi
240dpi ~ 320dpi xhdpi
320dpi ~ 480dpi xxhdpi
480dpi ~ 640dpi           xxxhdpi

If the screen density of the mobile phone is xxhdpi, then the pictures in the mipmap-xxhdpi folder are the most suitable pictures. If the image is placed in another mipmap, the image is scaled.

The order in which the system automatically searches for pictures under other files: High Density -> Low Density

Android divides the actual screen density into four common sizes (low, medium, high, and extra high) 
for ordinary screens under normal circumstances: ldpi is 120dpi, mdpi is 160dpi, hdpi is 240dpi, xhdpi is 320dpi 
for screens, The larger the dpi, the higher the fineness of the screen, and the clearer the screen looks 

In terms of screen size:

density recommended size
mipmap-mdpi 48*48
mipmap-hdpi 72*72
mipmap-xhdpi 96*96
mipmap-xxhdpi 144*144
mipmap-xxxhdpi 192*192


 

Unity loads resources

Well, the above is the different ways of loading resources between Android and Unity that Xiaobian briefly brought to you. If you need to know more about it, you can turn to my other articles to explain it.

Unity combat exercise

ImageAdapter.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ImageAdapter : MonoBehaviour
{
    public Text text;
    public GameObject imageObject;

    //不同分辨率图片路径
    string path_hdpi = "Images/mipmap-hdpi/image_hdpi";
    string path_mdpi = "Images/mipmap-mdpi/image_mdpi";
    string path_xdpi = "Images/mipmap-xhdpi/image_xhdpi";
    string path_xxdpi = "Images/mipmap-xxhdpi/image_xxhdpi";
    string path_xxxdpi = "Images/mipmap-xxhdpi//image_xxxhdpi";

    // Start is called before the first frame update
    void Start()
    {
        Sprite image;
        //判断dpi,来确认适合哪种分辨率的图片资源
        if (Screen.dpi >= 120 && Screen.dpi < 160)
        {
            image = Resources.Load<Sprite>(path_mdpi);

            text.text = "当前屏幕每寸的像素点值: " + Screen.dpi + '\n'
                + "当前加载的图片为: " + path_mdpi;
        }
        else if (Screen.dpi >= 160 && Screen.dpi < 240)
        {
            image = Resources.Load<Sprite>(path_hdpi);

            text.text = "当前屏幕每寸的像素点值: " + Screen.dpi + '\n'
                + "当前加载的图片为: " + path_hdpi;
        }
        else if (Screen.dpi >= 240 && Screen.dpi < 320)
        {
            image = Resources.Load<Sprite>(path_xdpi);

            text.text = "当前屏幕每寸的像素点值: " + Screen.dpi + '\n'
                + "当前加载的图片为: " + path_xdpi;
        }
        else if (Screen.dpi >= 320 && Screen.dpi < 480)
        {
            image = Resources.Load<Sprite>(path_xxdpi);

            text.text = "当前屏幕每寸的像素点值: " + Screen.dpi + '\n'
                + "当前加载的图片为: " + path_xxdpi;
            Debug.Log(image.name);
        }
        else if (Screen.dpi >= 480 && Screen.dpi < 640)
        {
            image = Resources.Load<Sprite>(path_xxxdpi);

            text.text = "当前屏幕每寸的像素点值: " + Screen.dpi + '\n'
                + "当前加载的图片为: " + path_xxxdpi;
        }
        else
        {
            text.text = "当前屏幕每寸的像素点值: " + Screen.dpi;
        }

        //将读取到的图片资源加载到游戏物体上
        imageObject.GetComponent<Image>().sprite = Resources.Load<Sprite>(path_xxdpi);
    }
}

Renderings:

screen-related concept sharing

Resolution:
refers to how many pixels there are horizontally and vertically on the screen 


Screen size:
refers to the actual physical size of the mobile phone, such as commonly used 2.8 inches, 3.2 inches, 3.5 inches, 3.7 inches 
android divides the screen size into four levels (small, normal, large, and extra large)


Screen Density:
Pixels Per Inch 

Phones can have the same resolution but different screen sizes


For the screen, the larger the dpi, the higher the fineness of the screen, and the clearer the screen looks 

Guess you like

Origin blog.csdn.net/Ai1114/article/details/132041507