unity之UGUI屏幕分辨率調整

2019-12-03 15:28:41 浏览数 (1)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/CJB_King/article/details/52620155

unity之UGUI屏幕分辨率調整

1.Canvas的屬性配置:

2.Canvas Scaler的屬性配置:

3.根據不同的屏幕比例動態寫改碩放基準:

代码语言:javascript复制
  public float standard_width = 800f;        //初始宽度  
    public float standard_height = 480f;       //初始高度  
    float device_width = 0f;                //当前设备宽度  
    float device_height = 0f;               //当前设备高度  
    float adjustor = 0f;         //屏幕矫正比例  
    void Start()
    {
        
        //获取设备宽高  
        device_width = Screen.width;
        device_height = Screen.height;
        //计算宽高比例  
        float standard_aspect = standard_width / standard_height;
        float device_aspect = device_width / device_height;
        //计算矫正比例  
        if (device_aspect < standard_aspect)
        {
            adjustor = standard_aspect / device_aspect;
        }

        CanvasScaler canvasScalerTemp = transform.GetComponent<CanvasScaler>();
        if (adjustor == 0)
        {
            canvasScalerTemp.matchWidthOrHeight = 1;
        }
        else
        {
            canvasScalerTemp.matchWidthOrHeight = 0;
        }
    }

4.將脚本挂在畫布上:

效果如圖:

0 人点赞