c1_unity/Assets/Scripts/Common/SafeAreaManager.cs
2023-08-10 15:31:11 +08:00

70 lines
2.0 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace BF {
public class SafeAreaManager {
// public const string NOTCH_SCREEN_SIMULATE = "NOTCH_SCREEN_SIMULATE";
private static SafeAreaManager instance;
public static SafeAreaManager Instance {
get {
if (instance == null) {
instance = new SafeAreaManager();
}
return instance;
}
}
/// <summary>
/// 是否开启模拟模式
/// </summary>
/// <returns></returns>
// public static bool CheckIfSimulate () {
// return PlayerPrefs.GetInt ("NOTCH_SCREEN_SIMULATE", 0) == 1;
// }
/// <summary>
/// 得到安全区域
/// </summary>
/// <returns></returns>
public static Rect GetSafeArea () {
// 当前屏幕尺寸
float screenWidth = Screen.width;
float screenHeight = Screen.height;
#if UNITY_EDITOR
return Screen.safeArea;
#elif UNITY_IPHONE
return Screen.safeArea;
#elif UNITY_ANDROID
// 安卓直接用SDK方法 不使用SafeArea
return new Rect (0, 0, screenWidth, screenHeight);
#else
return new Rect (0, 0, screenWidth, screenHeight);
#endif
}
/// <summary>
/// 得到竖屏时的刘海高度
/// </summary>
/// <returns></returns>
public static float GetNotchScreenHeight() {
Rect rect = GetSafeArea ();
#if UNITY_EDITOR
return Screen.height - rect.height - rect.y;
#elif UNITY_ANDROID
float height = Screen.height - rect.height - rect.y;
if (height > 0)
{
return height;
}
else
{
return BFMain.Instance.SDKMgr.GetNotchScreenHeight();
}
#else
return Screen.height - rect.height - rect.y;
#endif
}
}
}