hch
7 天以前 f99dd641fb1eb7ba7cbeab5b763d26a7324315c7
Main/Utility/SafeAreaUI.cs
@@ -8,21 +8,8 @@
    public const int SafeWidth = 75;
    public const int SafeBottom = 15;   //竖屏下方间隔
    public enum SimDevice { None, IphoneX }
    public static SimDevice Sim
    {
        set { LocalSave.SetInt("SimDevice", (int)value); }
        get { return (SimDevice)LocalSave.GetInt("SimDevice"); }
    }
    Rect[] NSA_IphoneX = new Rect[]
    {
        new Rect (0f, 102f / 2436f, 1f, 2202f / 2436f),  // Portrait
        new Rect (132f / 2436f, 63f / 1125f, 2172f / 2436f, 1062f / 1125f)  // Landscape
    };
    RectTransform _Panel;
    Rect LastSafeArea = new Rect(0, 0, 0, 0);
    Rect LastRect = new Rect(0, 0, 0, 0);
    RectTransform Panel
@@ -47,8 +34,6 @@
    void Refresh()
    {
        // Rect safeArea = GetSafeArea();
        // if (safeArea != LastSafeArea || LastRect != Panel.rect)
        if (LastRect != Panel.rect)
            ApplySafeArea();
    }
@@ -88,53 +73,5 @@
        }
        LastRect = Panel.rect;
    }
    #region 系统api的安全区
    Rect GetSafeArea()
    {
        if (!Application.isEditor)
            return Screen.safeArea;
        Rect safeArea = Screen.safeArea;
        if (Sim != SimDevice.None)
        {
            Rect nsa = new Rect(0, 0, Screen.width, Screen.height);
            switch (Sim)
            {
                case SimDevice.IphoneX:
                    if (Screen.height > Screen.width)  // Portrait
                        nsa = NSA_IphoneX[0];
                    else  // Landscape
                        nsa = NSA_IphoneX[1];
                    break;
                default:
                    break;
            }
            safeArea = new Rect(Screen.width * nsa.x, Screen.height * nsa.y, Screen.width * nsa.width, Screen.height * nsa.height);
        }
        return safeArea;
    }
    void ApplySafeArea(Rect safeArea)
    {
        Debug.LogFormat("ApplySafeArea:{0} ; rect:{1}", safeArea, Panel.rect);
        Panel.anchorMin = Vector2.zero;
        Panel.anchorMax = Vector2.one;
        //适配四个方向
        // Panel.offsetMin = safeArea.min;
        // Panel.offsetMax = new Vector2(-(Screen.width - safeArea.max.x), -(Screen.height - safeArea.max.y));
        //只适配两边
        Panel.offsetMin = new Vector2(safeArea.min.x, 0);
        Panel.offsetMax = new Vector2(-(Screen.width - safeArea.max.x), 0);
        LastRect = Panel.rect;
        LastSafeArea = safeArea;
    }
    #endregion
}