Unity screen adaptation and coordinate point adaptation

It’s been a long time since I’ve updated. Recently, I’ve been busy with internships and graduation projects. Although I haven’t finished my work yet, let’s update some of the knowledge I learned at work. Today, I will record how to design the functions of screen adaptation and coordinate point adaptation.

First, we need to confirm the aspect ratio of our original image and the background, then obtain the aspect ratio of the screen, and compare the screen ratio with the original ratio to determine whether it is length adaptation or width adaptation.
Write a simple code to facilitate understanding of ideas.

public float picturexy = picture.x/picture.y;
public float screenxy = UnityEngine.Screen.width/UnityEngine.Screen.height;
if(picturexy>screenxy)//屏幕比例小于原图,
//说明屏幕宽度过大所以需要原图长度等比适配
{
    
    
    picture.x = screenxy*picture.y;
}
else
{
    
    
    picture.y = picture.x/screenxy;
}

Next, record the coordinate point adaptation. If you understand the screen adaptation, it is very simple. Simply pass the ratio of the x of the original coordinate point to the length of the original image, and the ratio of y of the original coordinate point to the length of the original image. Multiply the two values ​​by the adapted screen length and width to get the adapted coordinate point x, y values. Record the code.

public float xscale = pos.x/picture.x
public float yscale = pos.y/picture.y
end.x = UnityEngine.Screen.width * xscale;
end.y = UnityEngine.Screen.height * yscale;

Guess you like

Origin blog.csdn.net/weixin_50746193/article/details/123929793
Recommended