【Unity】SafeArea适配大小

通过使用SafeArea,修改stretch适配类型的UI画布的Top偏移,适应安卓异型屏幕

    /// <summary>
    /// 适配safeArea
    /// </summary>
    void FitSafeArea(Transform obj)
    {
    
    
        Rect safeArea = Screen.safeArea;
        float y = Screen.height - safeArea.height;
        RectTransform rectTrans = obj.GetComponent<RectTransform>();
        SafeAreaOffect(rectTrans, y);
    }

    /// <summary>
    /// 设置便宜
    /// </summary>
    /// <param name="rectTrans"></param>
    /// <param name="size"></param>
    void SafeAreaOffect(RectTransform rectTrans, float size)
    {
    
    
        rectTrans.offsetMax = new Vector2(rectTrans.offsetMax.x, -size);
    }

猜你喜欢

转载自blog.csdn.net/qq_33205561/article/details/129321554