104 lines
2.1 KiB
C#
104 lines
2.1 KiB
C#
using UnityEngine;
|
|
using UnityEditor;
|
|
using System.Collections.Generic;
|
|
using System.Runtime.InteropServices;
|
|
using System;
|
|
using Beebyte.Obfuscator;
|
|
|
|
[Skip]
|
|
public class NotchScreenUtil
|
|
{
|
|
private AndroidJavaObject jo;
|
|
private AndroidJavaObject context;
|
|
|
|
private static NotchScreenUtil _instance;
|
|
|
|
public static NotchScreenUtil Instance
|
|
{
|
|
get
|
|
{
|
|
if (_instance == null)
|
|
{
|
|
_instance = new NotchScreenUtil();
|
|
}
|
|
|
|
return _instance;
|
|
}
|
|
}
|
|
|
|
|
|
#if UNITY_IOS
|
|
//初始化
|
|
[DllImport("__Internal")]
|
|
private static extern string m_GetNotchHeight();
|
|
[DllImport("__Internal")]
|
|
private static extern string m_GetNotchBottomHeight();
|
|
#endif
|
|
|
|
|
|
private NotchScreenUtil()
|
|
{
|
|
#if UNITY_IOS
|
|
|
|
#elif UNITY_ANDROID
|
|
using (var up = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
|
|
{
|
|
context = up.GetStatic<AndroidJavaObject>("currentActivity");
|
|
using (var adi = new AndroidJavaClass("com.bluewhale.androidutils.NotchScreenUtil"))
|
|
{
|
|
jo = adi.CallStatic<AndroidJavaObject>("instance");
|
|
jo.Call("Init", context);
|
|
}
|
|
}
|
|
#endif
|
|
}
|
|
|
|
|
|
public void Init()
|
|
{
|
|
XDebug.Log.l("设备信息初始化");
|
|
}
|
|
|
|
|
|
// 获取刘海屏高度
|
|
public int GetNotchHeight()
|
|
{
|
|
int height = -1;
|
|
|
|
try
|
|
{
|
|
#if UNITY_IOS
|
|
height = (int)Mathf.Floor(Convert.ToSingle(m_GetNotchHeight()));
|
|
#elif UNITY_ANDROID
|
|
height = jo.CallStatic<int>("getNotchHeight");
|
|
#endif
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogError(e);
|
|
}
|
|
return height;
|
|
}
|
|
|
|
// 获取刘海屏高度
|
|
public int GetNotchBottomHeight()
|
|
{
|
|
int height = -1;
|
|
|
|
try
|
|
{
|
|
#if UNITY_IOS
|
|
height = (int)Mathf.Floor(Convert.ToSingle(m_GetNotchBottomHeight()));
|
|
#elif UNITY_ANDROID
|
|
height = jo.CallStatic<int>("getNotchBottomHeight");
|
|
#endif
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogError(e);
|
|
}
|
|
return height;
|
|
}
|
|
|
|
|
|
} |