Getting the Google Advertising ID and Limit Advertising

坚强是说给别人听的谎言 提交于 2019-12-24 00:19:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!