Problem Description
Since our main application is a vertical screen application, the packaging rotation method can only be Portrait;
and some functional modules need to be displayed on a horizontal screen. At this time, it is necessary to dynamically switch to a horizontal screen. After exiting the module, switch back to the vertical screen;
In fact
旋转相机
, it can be done, but it will be a headache when adapting the UI不推荐
.
solution:
Unity allows switching between horizontal and vertical screens at runtime, as follows:
/// <summary>
/// 切换为横屏
/// </summary>
private void SetOrientationLandscape()
{
//设置横屏
Screen.orientation = ScreenOrientation.Landscape;
//设置横屏画布标准分辨率,保证UI横竖屏一致
scaler.referenceResolution = new Vector2(1920, 1080);
//这个match比例一般情况是这样,可根据项目情况设置
scaler.matchWidthOrHeight = 1;
}
/// <summary>
/// 切换为竖屏
/// </summary>
public void SetOrientationPortrait()
{
//设置竖屏
Screen.orientation = ScreenOrientation.Portrait;
//设置竖屏画布标准分辨率
scaler.referenceResolution = new Vector2(1080, 1920);
scaler.matchWidthOrHeight = 0;
}