UICanvas一般会做一个初始化操作,把代码挂在Canvas上就好了
public class ProjectSceneMatch : MonoBehaviour
{
CanvasScaler canvas;
private void Awake()
{
canvas = GetComponent<CanvasScaler>();
SetPreferedMatch();
}
void SetPreferedMatch()
{
var screenWidth = Screen.width;
var screenHeight = Screen.height;
float screenRate = (float)screenWidth / screenHeight;
var sceneScreenWidth = canvas.referenceResolution.x;
var sceneScreenHeight = canvas.referenceResolution.y;
float sceneCanvasRate = sceneScreenWidth / sceneScreenHeight;
if (screenRate < sceneCanvasRate)
{
canvas.matchWidthOrHeight = 0;
}
}
}
|