Skip to content

Instantly share code, notes, and snippets.

@TarasOsiris
Last active November 22, 2023 13:31
Show Gist options
  • Save TarasOsiris/9270285 to your computer and use it in GitHub Desktop.
Save TarasOsiris/9270285 to your computer and use it in GitHub Desktop.
Unity3d method to retrieve device ANDROID_ID constant.
using UnityEngine;
public static class AndroidIdRetriever
{
public static string Retrieve()
{
AndroidJavaClass clsUnity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject objActivity = clsUnity.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject objResolver = objActivity.Call<AndroidJavaObject>("getContentResolver");
AndroidJavaClass clsSecure = new AndroidJavaClass("android.provider.Settings$Secure");
string ANDROID_ID = clsSecure.GetStatic<string>("ANDROID_ID");
string androidId = clsSecure.CallStatic<string>("getString", objResolver, ANDROID_ID);
return androidId;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment