Unity竖屏打包,动态切换为横屏

问题描述

由于我们主应用是竖屏应用,打包的旋转方式只能是Portrait;
而某些功能模块需要横屏展示,这个时候就需要动态切换为横屏,退出该模块之后在切回竖屏;

其实旋转相机可以做到,但适配UI时候会很头疼,不推荐


解决方案:

Unity允许运行时切换横竖屏,如下:

/// <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;
}

猜你喜欢

转载自blog.csdn.net/xiezi5160/article/details/126426366