48 lines
1.7 KiB
C#
48 lines
1.7 KiB
C#
using UnityEngine;
|
|
using SystemInfo = UnityEngine.Device.SystemInfo;
|
|
|
|
namespace NBF
|
|
{
|
|
public static class PlatformInfo
|
|
{
|
|
public static string GetAndroidID()
|
|
{
|
|
string androidId = string.Empty;
|
|
|
|
#if UNITY_EDITOR
|
|
if (string.IsNullOrEmpty(androidId))
|
|
{
|
|
androidId = SystemInfo.deviceUniqueIdentifier;
|
|
}
|
|
#elif UNITY_ANDROID
|
|
// 只在 Android 平台上执行此操作
|
|
if (Application.platform == RuntimePlatform.Android)
|
|
{
|
|
try
|
|
{
|
|
using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
|
|
{
|
|
AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
|
|
AndroidJavaObject contentResolver =
|
|
currentActivity.Call<AndroidJavaObject>("getContentResolver");
|
|
AndroidJavaClass secureSettings = new AndroidJavaClass("android.provider.Settings$Secure");
|
|
androidId = secureSettings.CallStatic<string>("getString", contentResolver, "android_id");
|
|
}
|
|
}
|
|
catch (System.Exception e)
|
|
{
|
|
Debug.LogError("Error while fetching Android ID: " + e.Message);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("Not running on Android platform.");
|
|
}
|
|
#elif UNITY_IOS
|
|
androidId = SystemInfo.deviceUniqueIdentifier;
|
|
#endif
|
|
Debug.LogWarning($"androidId={androidId}");
|
|
return androidId;
|
|
}
|
|
}
|
|
} |