提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录前言一、Android的配置和代码1.引入库1.代码二、Unity端的代码1.C#前言
现在打开的AppsFlyer的官方帮助文档,Unity版本的接入指南已经没有了,只能找到AndroID版的和IOS版本的指南,就只能写一个简单的AndroID的接入,比较简单,仅供自己在下次使用的时候参考,只添加了上报事件的功能,它原来版本添加SDK里面的很多功能没有添加
一、AndroID的配置和代码1.引入库
dependencIEs { compileOnly files('libs/CorePlayingNativelib.aar') testImplementation 'junit:junit:4.+' androIDTestImplementation 'com.androID.support.test:runner:1.0.2' androIDTestImplementation 'com.androID.support.test.espresso:espresso-core:3.0.2' implementation 'com.appsflyer:af-androID-sdk:6.1.4' implementation 'com.androID.installreferrer:installreferrer:1.0' compile 'cn.hutool:hutool-all:5.5.7'}
这些是阅读官网的时候发现要添加的库,第一行代码是公司自己的库,添加于本地的,这个库在这里只是提供一个获取当前的Activity的方法,不用太在意
1.代码import java.util.Map;import androID.util.Log;import java.util.HashMap;import com.appsflyer.AppsFlyerlib;import com.appsflyer.AppsFlyerConversionListener;import com.coreplaying.coreplayingnativelib.CorePlayingNativelib;class AppsFlyerNativelib { private static AppsFlyerNativelib m_instance; private String TAG = "AppsFlyerNativelib"; public static AppsFlyerNativelib getInstance() { if (m_instance == null) m_instance = new AppsFlyerNativelib(); return m_instance; } private String AF_DEV_KEY = "yourkey"; private String currencyType = "USD"; public voID Init(String devKey,String currencyType){ Log.d(TAG, "Init: devKey = " + devKey + " currencyType = " + currencyType); if (devKey.isEmpty() == false) this.AF_DEV_KEY = devKey; if (currencyType.isEmpty() && currencyType != "") this.currencyType = currencyType; else this.currencyType = "USD"; AppsFlyerConversionListener conversionListener = new AppsFlyerConversionListener() { @OverrIDe public voID onConversionDataSuccess(Map<String, Object> map) { for(String attrname:map.keySet()) { Log.d(TAG, "attribute: " + attrname + " = " + map.get(attrname)); } } @OverrIDe public voID onConversionDataFail(String s) { Log.d(TAG, "onConversionDataFail: " + s); } @OverrIDe public voID onAppOpenAttribution(Map<String, String> map) { for (String attrname:map.keySet()){ Log.d(TAG, "onAppOpenAttribution: " + attrname + " = " + map.get(attrname)); } } @OverrIDe public voID onAttributionFailure(String s) { Log.d(TAG, "onAttributionFailure: " + s); } }; AppsFlyerlib.getInstance().init(AF_DEV_KEY,conversionListener, CorePlayingNativelib.GetUnityActivity()); AppsFlyerlib.getInstance().start(CorePlayingNativelib.GetUnityActivity()); } public voID SendEvent(String eventname,HashMap<String, Object> eventValues) { AppsFlyerlib.getInstance().logEvent(CorePlayingNativelib.GetUnityActivity(),eventname,eventValues); }}
这就是安卓端的代码,只有初始化和事件上报的内容
二、Unity端的代码1.C#public class AndroIDAppsFlyerManager{ private AndroIDJavaObject appsFlyerObject { get; set; } private string eventname { get; set; } = "eventID"; private string initKey { get; set; } = "Init"; private string currencyType { get; set; } = "currencyType"; private string devKeyname { get; set; } = "devKey"; public static AndroIDAppsFlyerManager Instance { get { if (m_Instance == null) m_Instance = new AndroIDAppsFlyerManager(); return m_Instance; } } private bool isInit { get; set; } = false; private static AndroIDAppsFlyerManager m_Instance; private AndroIDAppsFlyerManager() {#if UNITY_ANDROID && !UNITY_EDITOR DeBUG.Log("AndroIDAppsFlyerManager:AndroIDAppsFlyerManager"); var packagename = "com.coreplaying.appsflyernativelib.AppsFlyerNativelib"; AndroIDJavaClass javaClass = new AndroIDJavaClass(packagename); if (javaClass == null) { DeBUG.LogError("未能获得AndroID对象,无法调用Affsflyer上报数据"); return; } appsFlyerObject = javaClass.CallStatic<AndroIDJavaObject>("getInstance"); if (appsFlyerObject == null) { DeBUG.LogError("未能获得AndroID对象,无法调用Affsflyer上报数据"); return; } else { DeBUG.Log("AndroIDAppsFlyerManager:AndroIDAppsFlyerManager:成功"); }#endif } /// <summary> /// /// </summary> /// <param name="currencyType">货币类型</param> private voID Init(string devKey,string currencyType) { if (this.appsFlyerObject == null) return; DeBUG.Log("AndroIDAppsFlyerManager:Init"); if (string.IsNullOrEmpty(currencyType)) currencyType = "USD"; appsFlyerObject.Call("Init", devKey, currencyType); } public voID SendEvent(string JsonData) { if (this.appsFlyerObject == null) return; DeBUG.Log("Unity:AndroIDAppsFlyerManager:SendEvent---->" + JsonData); var evtDic = this.JasonToDictionary(JsonData); if (evtDic.Count == 0) { DeBUG.LogError("AndroIDAppsFlyerManager:SendEvent:数据有误无法转换--->" + JsonData); return; } if (evtDic[this.eventname] == this.initKey) { string currency = string.Empty; string devKey = string.Empty; if (evtDic.ContainsKey(currencyType)) currency = evtDic[currencyType]; if (evtDic.ContainsKey(devKeyname)) devKey = evtDic[devKeyname]; Init(devKey,currency); return; } appsFlyerObject.Call("SendEvent", evtDic[this.eventname], convertDictionaryToJavaMap(evtDic)); } private Dictionary<string, string> JasonToDictionary(string jasonData) { if (string.IsNullOrEmpty(jasonData)) { DeBUG.LogError("AppEventRequest:ProcessData---->jasonData = null"); return new Dictionary<string, string>(); } List<object> items = (List<object>)MiniJsON.Json.Deserialize(jasonData); if (items == null) { DeBUG.LogError("AppEventRequest:ProcessData---->items = null"); return new Dictionary<string, string>(); } var evtDic = new Dictionary<string, string>(); for (int i = 0; i < items.Count; ++i) { var evtList = items[i] as List<object>; if (evtList == null) { DeBUG.LogError("AppEventRequest:ProcessData:evtList == null"); return new Dictionary<string, string>(); } evtDic.Add(evtList[0].ToString(), evtList[1].ToString()); } return evtDic; } private static AndroIDJavaObject convertDictionaryToJavaMap(Dictionary<string, string> dictionary) { AndroIDJavaObject map = new AndroIDJavaObject("java.util.HashMap"); IntPtr putMethod = AndroIDJNIHelper.getmethodID(map.GetRawClass(), "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); if (dictionary != null) { foreach (var entry in dictionary) { AndroIDJNI.CallObjectMethod(map.GetRawObject(), putMethod, AndroIDJNIHelper.CreateJNIArgArray(new object[] { entry.Key, entry.Value })); } } return map; }}
初始化和上传事件的都是通过SendEvent(string Json)上传的,参数为Json字符串
总结以上是内存溢出为你收集整理的Android版AppsFlyer的Unity简单接入全部内容,希望文章能够帮你解决Android版AppsFlyer的Unity简单接入所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)