Unity中如何正確使用AndroidJavaClass獲取android_id
阿新 • • 發佈:2018-12-16
示例:
正確使用Unity官方提供的AndroidJavaClass呼叫android.provider.Settings中的屬性,以android_id為例
在unity中使用可以方便的呼叫到什麼androidid啦之類的,只要系統有寫就可以
public string GetAndroidID() { string android_id = "NONE"; try { #if UNITY_ANDROID && !UNITY_EDITOR AndroidJavaClass up = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject currentActivity = up.GetStatic<AndroidJavaObject>("currentActivity"); AndroidJavaObject contentResolver = currentActivity.Call<AndroidJavaObject>("getContentResolver"); AndroidJavaClass secure = new AndroidJavaClass("android.provider.Settings$Secure"); android_id = secure.CallStatic<string>("getString", contentResolver, "android_id"); #endif } catch (System.Exception e) { Debug.LogError(e); } return android_id; }