unity3d 獲取android id
廢話不多直接上代碼
public static string GetAndroidID()
{
string _strAndroidID = "none";
if (string.IsNullOrEmpty(_strAndroidID))
{
_strAndroidID = "none";
#if (UNITY_ANDROID && !UNITY_EDITOR) || ANDROID_CODE_VIEW
try
{
using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
using (AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
{
using (AndroidJavaObject contentResolver = currentActivity.Call<AndroidJavaObject>("getContentResolver"))
{
using (AndroidJavaClass secure = new AndroidJavaClass("android.provider.Settings$Secure"))
{
_strAndroidID = secure.CallStatic<string>("getString", contentResolver, "android_id");
if (string.IsNullOrEmpty(_strAndroidID))
{
_strAndroidID = "none";
}
}
}
}
}
}
catch (System.Exception e)
{
}
#endif
return _strAndroidID;
}
return _strAndroidID;
}