| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148 |
- // version: 5.0.4
- using UnityEngine;
- using System.Collections.Generic;
- #if UNITY_ANDROID
- using System;
- #endif
- #if UNITY_IPHONE
- using System.Runtime.InteropServices;
- using System.Collections;
- #endif
- public static class TalkingDataSDK
- {
- #if UNITY_ANDROID
- private static readonly string TALKINGDATA_CLASS = "com.tendcloud.tenddata.TalkingDataSDK";
- private static AndroidJavaClass talkingdataClass;
- private static AndroidJavaClass unityPlayerClass;
- #endif
- #if UNITY_IPHONE
- [DllImport("__Internal")]
- private static extern void TDInitSDK(string appId, string channelId, string custom);
- [DllImport("__Internal")]
- private static extern void TDStartA();
- [DllImport("__Internal")]
- private static extern void TDBackgroundSessionEnabled();
- [DllImport("__Internal")]
- private static extern string TDGetDeviceId();
- [DllImport("__Internal")]
- private static extern void TDSetVerboseLogDisable();
- [DllImport("__Internal")]
- private static extern void TDSetLocation(double latitude, double longitude);
- [DllImport("__Internal")]
- private static extern void TDOnPageBegin(string pageName);
- [DllImport("__Internal")]
- private static extern void TDOnPageEnd(string pageName);
- [DllImport("__Internal")]
- private static extern void TDOnReceiveDeepLink(string url);
- [DllImport("__Internal")]
- private static extern void TDOnRegister(string profileId, string profileJson, string invitationCode, string eventValueJson);
- [DllImport("__Internal")]
- private static extern void TDOnLogin(string profileId, string profileJson, string eventValueJson);
- [DllImport("__Internal")]
- private static extern void TDOnProfileUpdate(string profileJson);
- [DllImport("__Internal")]
- private static extern void TDOnCreateCard(string profileId, string method, string content);
- [DllImport("__Internal")]
- private static extern void TDOnFavorite(string category, string content, string eventValueJson);
- [DllImport("__Internal")]
- private static extern void TDOnShare(string profileId, string content, string eventValueJson);
- [DllImport("__Internal")]
- private static extern void TDOnPunch(string profileId, string punchId);
- [DllImport("__Internal")]
- private static extern void TDOnSearch(string searchJson);
- #if TD_RETAIL || TD_FINANCE || TD_TOUR || TD_ONLINEEDU
- [DllImport("__Internal")]
- private static extern void TDOnContact(string profileId, string content);
- #endif
- #if TD_GAME || TD_TOUR || TD_ONLINEEDU || TD_READING || TD_OTHER
- [DllImport("__Internal")]
- private static extern void TDOnPay(string profileId, string orderId, int amount, string currencyType, string paymentType, string itemId, int itemCount);
- #endif
- #if TD_RETAIL || TD_FINANCE || TD_TOUR || TD_ONLINEEDU
- [DllImport("__Internal")]
- private static extern void TDOnChargeBack(string profileId, string orderId, string reason, string type);
- #endif
- #if TD_FINANCE || TD_ONLINEEDU
- [DllImport("__Internal")]
- private static extern void TDOnReservation(string profileId, string reservationId, string category, int amount, string term);
- #endif
- #if TD_RETAIL || TD_TOUR
- [DllImport("__Internal")]
- private static extern void TDOnBooking(string profileId, string bookingId, string category, int amount, string content);
- #endif
- #if TD_RETAIL
- [DllImport("__Internal")]
- private static extern void TDOnViewItem(string itemId, string category, string name, int unitPrice, string eventValueJson);
- [DllImport("__Internal")]
- private static extern void TDOnAddItemToShoppingCart(string item, string category, string name, int unitPrice, int amount, string eventValueJson);
- [DllImport("__Internal")]
- private static extern void TDOnViewShoppingCart(string shoppingCartJson);
- [DllImport("__Internal")]
- private static extern void TDOnPlaceOrder(string orderJson, string profileId, string eventValueJson);
- [DllImport("__Internal")]
- private static extern void TDOnOrderPaySucc(string orderJson, string paymentType, string profileId);
- [DllImport("__Internal")]
- private static extern void TDOnCancelOrder(string orderJson);
- #endif
- #if TD_FINANCE
- [DllImport("__Internal")]
- private static extern void TDOnCredit(string profileId, int amount, string content);
- [DllImport("__Internal")]
- private static extern void TDOnTransaction(string profileId, string transactionJson);
- #endif
- #if TD_GAME
- [DllImport("__Internal")]
- private static extern void TDOnCreateRole(string name);
- [DllImport("__Internal")]
- private static extern void TDOnLevelPass(string profileId, string levelId);
- [DllImport("__Internal")]
- private static extern void TDOnGuideFinished(string profileId, string content);
- #endif
- #if TD_ONLINEEDU
- [DllImport("__Internal")]
- private static extern void TDOnLearn(string profileId, string course, long begin, int duration);
- [DllImport("__Internal")]
- private static extern void TDOnPreviewFinished(string profileId, string content);
- #endif
- #if TD_READING
- [DllImport("__Internal")]
- private static extern void TDOnRead(string profileId, string book, long begin, int duration);
- [DllImport("__Internal")]
- private static extern void TDOnFreeFinished(string profileId, string content);
- #endif
- #if TD_GAME || TD_ONLINEEDU
- [DllImport("__Internal")]
- private static extern void TDOnAchievementUnlock(string profileId, string achievementId);
- #endif
- #if TD_FINANCE || TD_TOUR || TD_OTHER
- [DllImport("__Internal")]
- private static extern void TDOnBrowse(string profileId, string content, long begin, int duration);
- #endif
- #if TD_RETAIL || TD_FINANCE || TD_TOUR || TD_OTHER
- [DllImport("__Internal")]
- private static extern void TDOnTrialFinished(string profileId, string content);
- #endif
- [DllImport("__Internal")]
- private static extern void TDOnEvent(string eventId, string parameters, string eventValueJson);
- [DllImport("__Internal")]
- private static extern void TDSetGlobalKV(string key, string strVal, double numVal);
- [DllImport("__Internal")]
- private static extern void TDRemoveGlobalKV(string key);
- #endif
- #if UNITY_ANDROID
- private static AndroidJavaObject GetCurrentActivity()
- {
- if (unityPlayerClass == null)
- {
- unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
- }
- AndroidJavaObject activity = unityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity");
- return activity;
- }
- private static AndroidJavaObject DictionaryToAndroidMap(Dictionary<string, object> parameters)
- {
- AndroidJavaObject map = null;
- if (parameters != null && parameters.Count > 0)
- {
- int count = parameters.Count;
- map = new AndroidJavaObject("java.util.HashMap", count);
- IntPtr method_Put = AndroidJNIHelper.GetMethodID(map.GetRawClass(), "put",
- "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
- object[] args = new object[2];
- foreach (KeyValuePair<string, object> kvp in parameters)
- {
- args[0] = new AndroidJavaObject("java.lang.String", kvp.Key);
- args[1] = typeof(string).IsInstanceOfType(kvp.Value)
- ? new AndroidJavaObject("java.lang.String", kvp.Value)
- : new AndroidJavaObject("java.lang.Double", "" + kvp.Value);
- AndroidJNI.CallObjectMethod(map.GetRawObject(), method_Put, AndroidJNIHelper.CreateJNIArgArray(args));
- }
- }
- return map;
- }
- #endif
- #if UNITY_IPHONE
- private static string DictionaryToJSONString(Dictionary<string, object> parameters)
- {
- string json = null;
- if (parameters != null && parameters.Count > 0)
- {
- json = "{";
- foreach (KeyValuePair<string, object> kvp in parameters)
- {
- if (kvp.Value is string)
- {
- json += "\"" + kvp.Key + "\":\"" + kvp.Value + "\",";
- }
- else
- {
- try
- {
- double tmp = System.Convert.ToDouble(kvp.Value);
- json += "\"" + kvp.Key + "\":" + tmp + ",";
- }
- catch (System.Exception)
- {
- }
- }
- }
- json = json.TrimEnd(',');
- json += "}";
- }
- return json;
- }
- #endif
- public static void SetConfig(TalkingDataSDKConfig config)
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass == null)
- {
- talkingdataClass = new AndroidJavaClass(TALKINGDATA_CLASS);
- }
- talkingdataClass.CallStatic("setConfig", config.javaObj);
- #endif
- }
- }
- public static void InitSDK(string appId, string channelId, string custom)
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- Debug.Log("TalkingData Unity SDK.");
- #if UNITY_ANDROID
- using (AndroidJavaClass dz = new AndroidJavaClass("com.tendcloud.tenddata.dz"))
- {
- dz.SetStatic("a", 2);
- }
- if (talkingdataClass == null)
- {
- talkingdataClass = new AndroidJavaClass(TALKINGDATA_CLASS);
- }
- AndroidJavaObject activity = GetCurrentActivity();
- talkingdataClass.CallStatic("initSDK", activity, appId, channelId, custom);
- #endif
- #if UNITY_IPHONE
- TDInitSDK(appId, channelId, custom);
- #endif
- }
- }
- public static void StartA()
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass != null)
- {
- AndroidJavaObject activity = GetCurrentActivity();
- talkingdataClass.CallStatic("startA", activity);
- talkingdataClass.CallStatic("onResume", activity);
- }
- #endif
- #if UNITY_IPHONE
- TDStartA();
- #endif
- }
- }
- public static void OnPause()
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass != null)
- {
- talkingdataClass.CallStatic("onPause", GetCurrentActivity());
- }
- #endif
- }
- }
- public static void BackgroundSessionEnabled()
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_IPHONE
- TDBackgroundSessionEnabled();
- #endif
- }
- }
- private static string deviceId = null;
- public static string GetDeviceId()
- {
- if (deviceId == null && Application.platform != RuntimePlatform.OSXEditor &&
- Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass == null)
- {
- talkingdataClass = new AndroidJavaClass(TALKINGDATA_CLASS);
- }
- deviceId = talkingdataClass.CallStatic<string>("getDeviceId", GetCurrentActivity());
- #endif
- #if UNITY_IPHONE
- deviceId = TDGetDeviceId();
- #endif
- }
- return deviceId;
- }
- private static string oaid = null;
- public static string GetOAID()
- {
- if (oaid == null && Application.platform != RuntimePlatform.OSXEditor &&
- Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass == null)
- {
- talkingdataClass = new AndroidJavaClass(TALKINGDATA_CLASS);
- }
- oaid = talkingdataClass.CallStatic<string>("getOAID", GetCurrentActivity());
- #endif
- }
- return oaid;
- }
- public static void SetVerboseLogDisable()
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass == null)
- {
- talkingdataClass = new AndroidJavaClass(TALKINGDATA_CLASS);
- }
- talkingdataClass.CallStatic("setVerboseLogDisable");
- #endif
- #if UNITY_IPHONE
- TDSetVerboseLogDisable();
- #endif
- }
- }
- public static void SetLocation(double latitude, double longitude)
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_IPHONE
- TDSetLocation(latitude, longitude);
- #endif
- }
- }
- public static void OnPageBegin(string pageName)
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass != null)
- {
- talkingdataClass.CallStatic("onPageBegin", GetCurrentActivity(), pageName);
- }
- #endif
- #if UNITY_IPHONE
- TDOnPageBegin(pageName);
- #endif
- }
- }
- public static void OnPageEnd(string pageName)
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass != null)
- {
- talkingdataClass.CallStatic("onPageEnd", GetCurrentActivity(), pageName);
- }
- #endif
- #if UNITY_IPHONE
- TDOnPageEnd(pageName);
- #endif
- }
- }
- public static void OnReceiveDeepLink(string url)
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass != null)
- {
- talkingdataClass.CallStatic("onReceiveDeepLink", url);
- }
- #endif
- #if UNITY_IPHONE
- TDOnReceiveDeepLink(url);
- #endif
- }
- }
- public static void OnRegister(string profileId, TalkingDataProfile profile, string invitationCode,
- Dictionary<string, object> eventValue)
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass != null)
- {
- AndroidJavaObject eventValueMap = DictionaryToAndroidMap(eventValue);
- talkingdataClass.CallStatic("onRegister", profileId, profile.javaObj, invitationCode, eventValueMap);
- if (eventValueMap != null)
- {
- eventValueMap.Dispose();
- }
- }
- #endif
- #if UNITY_IPHONE
- string eventValueJson = DictionaryToJSONString(eventValue);
- TDOnRegister(profileId, profile.ToString(), invitationCode, eventValueJson);
- #endif
- }
- }
- public static void OnLogin(string profileId, TalkingDataProfile profile, Dictionary<string, object> eventValue)
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass != null)
- {
- AndroidJavaObject eventValueMap = DictionaryToAndroidMap(eventValue);
- talkingdataClass.CallStatic("onLogin", profileId, profile.javaObj, eventValueMap);
- if (eventValueMap != null)
- {
- eventValueMap.Dispose();
- }
- }
- #endif
- #if UNITY_IPHONE
- string eventValueJson = DictionaryToJSONString(eventValue);
- TDOnLogin(profileId, profile.ToString(), eventValueJson);
- #endif
- }
- }
- public static void OnProfileUpdate(TalkingDataProfile profile)
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass != null)
- {
- talkingdataClass.CallStatic("onProfileUpdate", profile.javaObj);
- }
- #endif
- #if UNITY_IPHONE
- TDOnProfileUpdate(profile.ToString());
- #endif
- }
- }
- public static void OnCreateCard(string profileId, string method, string content)
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass != null)
- {
- talkingdataClass.CallStatic("onCreateCard", profileId, method, content);
- }
- #endif
- #if UNITY_IPHONE
- TDOnCreateCard(profileId, method, content);
- #endif
- }
- }
- public static void OnFavorite(string category, string content, Dictionary<string, object> eventValue)
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass != null)
- {
- AndroidJavaObject eventValueMap = DictionaryToAndroidMap(eventValue);
- talkingdataClass.CallStatic("onFavorite", category, content, eventValueMap);
- if (eventValueMap != null)
- {
- eventValueMap.Dispose();
- }
- }
- #endif
- #if UNITY_IPHONE
- string eventValueJson = DictionaryToJSONString(eventValue);
- TDOnFavorite(category, content, eventValueJson);
- #endif
- }
- }
- public static void OnShare(string profileId, string content, Dictionary<string, object> eventValue)
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass != null)
- {
- AndroidJavaObject eventValueMap = DictionaryToAndroidMap(eventValue);
- talkingdataClass.CallStatic("onShare", profileId, content, eventValueMap);
- if (eventValueMap != null)
- {
- eventValueMap.Dispose();
- }
- }
- #endif
- #if UNITY_IPHONE
- string eventValueJson = DictionaryToJSONString(eventValue);
- TDOnShare(profileId, content, eventValueJson);
- #endif
- }
- }
- public static void OnPunch(string profileId, string punchId)
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass != null)
- {
- talkingdataClass.CallStatic("onPunch", profileId, punchId);
- }
- #endif
- #if UNITY_IPHONE
- TDOnPunch(profileId, punchId);
- #endif
- }
- }
- public static void OnSearch(TalkingDataSearch search)
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass != null)
- {
- talkingdataClass.CallStatic("onSearch", search.javaObj);
- }
- #endif
- #if UNITY_IPHONE
- TDOnSearch(search.ToString());
- #endif
- }
- }
- #if TD_RETAIL || TD_FINANCE || TD_TOUR || TD_ONLINEEDU
- public static void OnContact(string profileId, string content)
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass != null)
- {
- talkingdataClass.CallStatic("onContact", profileId, content);
- }
- #endif
- #if UNITY_IPHONE
- TDOnContact(profileId, content);
- #endif
- }
- }
- #endif
- #if TD_GAME || TD_TOUR || TD_ONLINEEDU || TD_READING || TD_OTHER
- public static void OnPay(string profileId, string orderId, int amount, string currencyType, string paymentType, string itemId, int itemCount)
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass != null)
- {
- talkingdataClass.CallStatic("onPay", profileId, orderId, amount, currencyType, paymentType, itemId, itemCount);
- }
- #endif
- #if UNITY_IPHONE
- TDOnPay(profileId, orderId, amount, currencyType, paymentType, itemId, itemCount);
- #endif
- }
- }
- #endif
- #if TD_RETAIL || TD_FINANCE || TD_TOUR || TD_ONLINEEDU
- public static void OnChargeBack(string profileId, string orderId, string reason, string type)
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass != null)
- {
- talkingdataClass.CallStatic("onChargeBack", profileId, orderId, reason, type);
- }
- #endif
- #if UNITY_IPHONE
- TDOnChargeBack(profileId, orderId, reason, type);
- #endif
- }
- }
- #endif
- #if TD_FINANCE || TD_ONLINEEDU
- public static void OnReservation(string profileId, string reservationId, string category, int amount, string term)
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass != null)
- {
- talkingdataClass.CallStatic("onReservation", profileId, reservationId, category, amount, term);
- }
- #endif
- #if UNITY_IPHONE
- TDOnReservation(profileId, reservationId, category, amount, term);
- #endif
- }
- }
- #endif
- #if TD_RETAIL || TD_TOUR
- public static void OnBooking(string profileId, string bookingId, string category, int amount, string content)
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass != null)
- {
- talkingdataClass.CallStatic("onBooking", profileId, bookingId, category, amount, content);
- }
- #endif
- #if UNITY_IPHONE
- TDOnBooking(profileId, bookingId, category, amount, content);
- #endif
- }
- }
- #endif
- #if TD_RETAIL
- public static void OnViewItem(string itemId, string category, string name, int unitPrice, Dictionary<string, object> eventValue)
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass != null)
- {
- AndroidJavaObject eventValueMap = DictionaryToAndroidMap(eventValue);
- talkingdataClass.CallStatic("onViewItem", itemId, category, name, unitPrice, eventValueMap);
- if (eventValueMap != null)
- {
- eventValueMap.Dispose();
- }
- }
- #endif
- #if UNITY_IPHONE
- string eventValueJson = DictionaryToJSONString(eventValue);
- TDOnViewItem(itemId, category, name, unitPrice, eventValueJson);
- #endif
- }
- }
- public static void OnAddItemToShoppingCart(string itemId, string category, string name, int unitPrice, int amount, Dictionary<string, object> eventValue)
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass != null)
- {
- AndroidJavaObject eventValueMap = DictionaryToAndroidMap(eventValue);
- talkingdataClass.CallStatic("onAddItemToShoppingCart", itemId, category, name, unitPrice, amount, eventValueMap);
- if (eventValueMap != null)
- {
- eventValueMap.Dispose();
- }
- }
- #endif
- #if UNITY_IPHONE
- string eventValueJson = DictionaryToJSONString(eventValue);
- TDOnAddItemToShoppingCart(itemId, category, name, unitPrice, amount, eventValueJson);
- #endif
- }
- }
- public static void OnViewShoppingCart(TalkingDataShoppingCart shoppingCart)
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass != null)
- {
- talkingdataClass.CallStatic("onViewShoppingCart", shoppingCart.javaObj);
- }
- #endif
- #if UNITY_IPHONE
- TDOnViewShoppingCart(shoppingCart.ToString());
- #endif
- }
- }
- public static void OnPlaceOrder(TalkingDataOrder order, string profileId, Dictionary<string, object> eventValue)
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass != null)
- {
- AndroidJavaObject eventValueMap = DictionaryToAndroidMap(eventValue);
- talkingdataClass.CallStatic("onPlaceOrder", order.javaObj, profileId, eventValueMap);
- if (eventValueMap != null)
- {
- eventValueMap.Dispose();
- }
- }
- #endif
- #if UNITY_IPHONE
- string eventValueJson = DictionaryToJSONString(eventValue);
- TDOnPlaceOrder(order.ToString(), profileId, eventValueJson);
- #endif
- }
- }
- public static void OnOrderPaySucc(TalkingDataOrder order, string paymentType, string profileId)
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass != null)
- {
- talkingdataClass.CallStatic("onOrderPaySucc", order.javaObj, paymentType, profileId);
- }
- #endif
- #if UNITY_IPHONE
- TDOnOrderPaySucc(order.ToString(), paymentType, profileId);
- #endif
- }
- }
- public static void OnCancelOrder(TalkingDataOrder order)
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass != null)
- {
- talkingdataClass.CallStatic("onCancelOrder", order.javaObj);
- }
- #endif
- #if UNITY_IPHONE
- TDOnCancelOrder(order.ToString());
- #endif
- }
- }
- #endif
- #if TD_FINANCE
- public static void OnCredit(string profileId, int amount, string content)
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass != null)
- {
- talkingdataClass.CallStatic("onCredit", profileId, amount, content);
- }
- #endif
- #if UNITY_IPHONE
- TDOnCredit(profileId, amount, content);
- #endif
- }
- }
- public static void OnTransaction(string profileId, TalkingDataTransaction transaction)
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass != null)
- {
- talkingdataClass.CallStatic("onTransaction", profileId, transaction.javaObj);
- }
- #endif
- #if UNITY_IPHONE
- TDOnTransaction(profileId, transaction.ToString());
- #endif
- }
- }
- #endif
- #if TD_GAME
- public static void OnCreateRole(string name)
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass != null)
- {
- talkingdataClass.CallStatic("onCreateRole", name);
- }
- #endif
- #if UNITY_IPHONE
- TDOnCreateRole(name);
- #endif
- }
- }
- public static void OnLevelPass(string profileId, string levelId)
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass != null)
- {
- talkingdataClass.CallStatic("onLevelPass", profileId, levelId);
- }
- #endif
- #if UNITY_IPHONE
- TDOnLevelPass(profileId, levelId);
- #endif
- }
- }
- public static void OnGuideFinished(string profileId, string content)
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass != null)
- {
- talkingdataClass.CallStatic("onGuideFinished", profileId, content);
- }
- #endif
- #if UNITY_IPHONE
- TDOnGuideFinished(profileId, content);
- #endif
- }
- }
- #endif
- #if TD_ONLINEEDU
- public static void OnLearn(string profileId, string course, long begin, int duration)
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass != null)
- {
- talkingdataClass.CallStatic("onLearn", profileId, course, begin, duration);
- }
- #endif
- #if UNITY_IPHONE
- TDOnLearn(profileId, course, begin, duration);
- #endif
- }
- }
- public static void OnPreviewFinished(string profileId, string content)
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass != null)
- {
- talkingdataClass.CallStatic("onPreviewFinished", profileId, content);
- }
- #endif
- #if UNITY_IPHONE
- TDOnPreviewFinished(profileId, content);
- #endif
- }
- }
- #endif
- #if TD_READING
- public static void OnRead(string profileId, string book, long begin, int duration)
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass != null)
- {
- talkingdataClass.CallStatic("onRead", profileId, book, begin, duration);
- }
- #endif
- #if UNITY_IPHONE
- TDOnRead(profileId, book, begin, duration);
- #endif
- }
- }
- public static void OnFreeFinished(string profileId, string content)
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass != null)
- {
- talkingdataClass.CallStatic("onFreeFinished", profileId, content);
- }
- #endif
- #if UNITY_IPHONE
- TDOnFreeFinished(profileId, content);
- #endif
- }
- }
- #endif
- #if TD_GAME || TD_ONLINEEDU
- public static void OnAchievementUnlock(string profileId, string achievementId)
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass != null)
- {
- talkingdataClass.CallStatic("onAchievementUnlock", profileId, achievementId);
- }
- #endif
- #if UNITY_IPHONE
- TDOnAchievementUnlock(profileId, achievementId);
- #endif
- }
- }
- #endif
- #if TD_FINANCE || TD_TOUR || TD_OTHER
- public static void OnBrowse(string profileId, string content, long begin, int duration)
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass != null)
- {
- talkingdataClass.CallStatic("onBrowse", profileId, content, begin, duration);
- }
- #endif
- #if UNITY_IPHONE
- TDOnBrowse(profileId, content, begin, duration);
- #endif
- }
- }
- #endif
- #if TD_RETAIL || TD_FINANCE || TD_TOUR || TD_OTHER
- public static void OnTrialFinished(string profileId, string content)
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass != null)
- {
- talkingdataClass.CallStatic("onTrialFinished", profileId, content);
- }
- #endif
- #if UNITY_IPHONE
- TDOnTrialFinished(profileId, content);
- #endif
- }
- }
- #endif
- public static void OnEvent(string eventId, Dictionary<string, object> parameters,
- Dictionary<string, object> eventValue)
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass != null)
- {
- AndroidJavaObject parametersMap = DictionaryToAndroidMap(parameters);
- AndroidJavaObject eventValueMap = DictionaryToAndroidMap(eventValue);
- talkingdataClass.CallStatic("onEvent", GetCurrentActivity(), eventId, parametersMap, eventValueMap);
- if (parametersMap != null)
- {
- parametersMap.Dispose();
- }
- if (eventValueMap != null)
- {
- eventValueMap.Dispose();
- }
- }
- #endif
- #if UNITY_IPHONE
- string parametersJson = DictionaryToJSONString(parameters);
- string eventValueJson = DictionaryToJSONString(eventValue);
- TDOnEvent(eventId, parametersJson, eventValueJson);
- #endif
- }
- }
- /// <summary>
- /// 手动报告错误
- /// </summary>
- public static void OnError(string message, string stackTrace, LogType logType)
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass != null)
- {
- string exceptionMessage = $"{logType}: {message}\n{stackTrace}";
- using (AndroidJavaObject javaException = new AndroidJavaObject("java.lang.Exception", exceptionMessage))
- {
- talkingdataClass.CallStatic("onError", unityPlayerClass, javaException);
- }
- }
- #endif
- }
- }
- /// <summary>
- /// 手动报告异常
- /// </summary>
- public static void ReportException(System.Exception exception)
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- try
- {
- using (AndroidJavaObject javaException = new AndroidJavaObject("java.lang.Exception",
- $"{exception.Message}\n{exception.StackTrace}"))
- {
- talkingdataClass.CallStatic("onError", unityPlayerClass, javaException);
- }
- }
- catch (System.Exception e)
- {
- Debug.LogError("报告异常到TalkingData失败: " + e.Message);
- }
- #endif
- }
- }
- /// <summary>
- /// 设置是否自动捕获未处理异常
- /// </summary>
- /// <param name="enabled">true:开启, false:关闭</param>
- public static void SetReportUncaughtExceptions(bool enabled)
- {
- try
- {
- talkingdataClass.CallStatic("setReportUncaughtExceptions", enabled);
- Debug.Log($"TalkingData自动异常捕获: {(enabled ? "开启" : "关闭")}");
- }
- catch (System.Exception e)
- {
- Debug.LogError("设置TalkingData自动异常捕获失败: " + e.Message);
- }
- }
- public static void SetGlobalKV(string key, object val)
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass != null)
- {
- AndroidJavaObject valObject = typeof(string).IsInstanceOfType(val)
- ? new AndroidJavaObject("java.lang.String", val)
- : new AndroidJavaObject("java.lang.Double", "" + val);
- talkingdataClass.CallStatic("setGlobalKV", key, valObject);
- }
- #endif
- #if UNITY_IPHONE
- if (val is string)
- {
- string tmp = System.Convert.ToString(val);
- TDSetGlobalKV(key, tmp, 0);
- }
- else
- {
- double tmp = System.Convert.ToDouble(val);
- TDSetGlobalKV(key, null, tmp);
- }
- #endif
- }
- }
- public static void RemoveGlobalKV(string key)
- {
- if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
- {
- #if UNITY_ANDROID
- if (talkingdataClass != null)
- {
- talkingdataClass.CallStatic("removeGlobalKV", key);
- }
- #endif
- #if UNITY_IPHONE
- TDRemoveGlobalKV(key);
- #endif
- }
- }
- }
|