问题
I'm building a Unity Android app, and looking at some advertising. One of the services we are considering requires my google advertising ID and limit advertising state in order to do server-to-server conversion tracking.
The problem is I'm not sure how to get either of these values within Unity. It seems like I would need some form of plugin? I already have google ad services implemented and in use for both AdMob and Chartboost, but as near as I can tell neither of these plugins give me access to the java calls I would need to retrieve the aforementioned values.
So I guess I'm not sure how to access the data I need. I'm hesitant to add more plugins to the game because they are getting difficult enough to manage as it is. If I understand correctly I think there should be a way to access the java through Unity's libraries, but I haven't the slightest how to do that.
回答1:
I was able to get the desired data using the following code:
string advertisingID = "";
bool limitAdvertising = false;
AndroidJavaClass up = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = up.GetStatic<AndroidJavaObject> ("currentActivity");
AndroidJavaClass client = new AndroidJavaClass ("com.google.android.gms.ads.identifier.AdvertisingIdClient");
AndroidJavaObject adInfo = client.CallStatic<AndroidJavaObject> ("getAdvertisingIdInfo",currentActivity);
advertisingID = adInfo.Call<string> ("getId").ToString();
limitTracking = (adInfo.Call<bool> ("isLimitAdTrackingEnabled"));
来源:https://stackoverflow.com/questions/28179150/getting-the-google-advertising-id-and-limit-advertising