Analytics.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  1. 
  2. // Created by ZhuCong on 1/1/14.
  3. // Copyright 2014 Umeng.com . All rights reserved.
  4. using UnityEngine;
  5. using System;
  6. using System.Runtime.InteropServices;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. namespace Umeng
  11. {
  12. public class Analytics
  13. {
  14. //iOS Android Universal API
  15. //
  16. /// <summary>
  17. /// 开始友盟统计 默认发送策略为启动时发送
  18. /// </summary>
  19. /// <param name="appKey">友盟appKey</param>
  20. /// <param name="channelId">渠道名称</param>
  21. public static void StartWithAppKeyAndChannelId(string appKey, string channelId)
  22. {
  23. #if UNITY_EDITOR
  24. //Debug.LogWarning("友盟统计在iOS/Android 真机上才会向友盟后台服务器发送事件 请在真机上测试");
  25. #elif UNITY_IPHONE
  26. CreateUmengManger();
  27. StartWithAppKeyAndReportPolicyAndChannelId(appKey, ReportPolicy.BATCH, channelId);
  28. #elif UNITY_ANDROID
  29. UMGameAgentInit();
  30. _AppKey = appKey;
  31. _ChannelId = channelId;
  32. CreateUmengManger();
  33. EnableActivityDurationTrack(false);
  34. #endif
  35. }
  36. /// <summary>
  37. /// 设置是否打印sdk的信息,默认不开启
  38. /// </summary>
  39. /// <param name="value">设置为true,Umeng SDK 会输出日志信息,记得release产品时要设置回false.</param>
  40. ///
  41. public static void SetLogEnabled(bool value)
  42. {
  43. #if UNITY_EDITOR
  44. //Debug.Log("SetLogEnabled");
  45. #elif UNITY_IPHONE
  46. _SetLogEnabled(value);
  47. #elif UNITY_ANDROID
  48. Agent.CallStatic("setDebugMode", value);
  49. #endif
  50. }
  51. //使用前,请先到友盟App管理后台的设置->编辑自定义事件 中添加相应的事件ID,然后在工程中传入相应的事件ID
  52. //eventId、attributes中key和value都不能使用空格和特殊字符,且长度不能超过255个字符(否则将截取前255个字符)
  53. //id, ts, du是保留字段,不能作为eventId及key的名称
  54. /// <summary>
  55. /// 基本事件
  56. /// </summary>
  57. /// <param name="eventId">友盟后台设定的事件Id</param>
  58. public static void Event(string eventId)
  59. {
  60. #if UNITY_EDITOR
  61. //Debug.Log("Event");
  62. #elif UNITY_IPHONE
  63. _Event(eventId);
  64. #elif UNITY_ANDROID
  65. Agent.CallStatic("onEvent", Context, eventId);
  66. #endif
  67. }
  68. //不同的标签会分别进行统计,方便同一事件的不同标签的对比,为nil或空字符串时后台会生成和eventId同名的标签.
  69. /// <summary>
  70. /// 基本事件
  71. /// </summary>
  72. /// <param name="eventId">友盟后台设定的事件Id</param>
  73. /// <param name="label">分类标签</param>
  74. public static void Event(string eventId, string label)
  75. {
  76. #if UNITY_EDITOR
  77. //Debug.Log("Event");
  78. #elif UNITY_IPHONE
  79. _EventWithLabel(eventId, label);
  80. #elif UNITY_ANDROID
  81. Agent.CallStatic("onEvent", Context, eventId, label);
  82. #endif
  83. }
  84. /// <summary>
  85. /// 属性事件
  86. /// </summary>
  87. /// <param name="eventId">友盟后台设定的事件Id</param>
  88. /// <param name="attributes"> 属性中的Key-Vaule Pair不能超过10个</param>
  89. public static void Event(string eventId, Dictionary<string, string> attributes)
  90. {
  91. #if UNITY_EDITOR
  92. //Debug.Log("Event");
  93. #elif UNITY_IPHONE
  94. _EventWithAttributes(eventId, DictionaryToJson(attributes));
  95. #elif UNITY_ANDROID
  96. Agent.CallStatic("onEvent", Context, eventId, ToJavaHashMap(attributes));
  97. #endif
  98. }
  99. /// <summary>
  100. /// 时长统计事件
  101. /// 与EventEnd要配对使用
  102. /// </summary>
  103. /// <param name="eventId">友盟后台设定的事件Id</param>
  104. public static void EventBegin(string eventId)
  105. {
  106. #if UNITY_EDITOR
  107. //Debug.Log("BeginEvent");
  108. #elif UNITY_IPHONE
  109. _BeginEventWithLabel(eventId, "");
  110. #elif UNITY_ANDROID
  111. Agent.CallStatic("onEventBegin", Context, eventId);
  112. #endif
  113. }
  114. /// <summary>
  115. /// 时长统计事件
  116. /// 与EventBegin要配对使用
  117. /// </summary>
  118. /// <param name="eventId">友盟后台设定的事件</param>
  119. public static void EventEnd(string eventId)
  120. {
  121. #if UNITY_EDITOR
  122. //Debug.Log("EndEvent");
  123. #elif UNITY_IPHONE
  124. _EndEventWithLabel(eventId, "");
  125. #elif UNITY_ANDROID
  126. Agent.CallStatic("onEventEnd", Context, eventId);
  127. #endif
  128. }
  129. /// <summary>
  130. /// 时长统计事件
  131. /// 与EventEnd要配对使用
  132. /// </summary>
  133. /// <param name="eventId">友盟后台设定的事件Id</param>
  134. /// <param name="label">分类标签</param>
  135. public static void EventBegin(string eventId, string label)
  136. {
  137. #if UNITY_EDITOR
  138. //Debug.Log("BeginEvent");
  139. #elif UNITY_IPHONE
  140. _BeginEventWithLabel(eventId, label);
  141. #elif UNITY_ANDROID
  142. Agent.CallStatic("onEventBegin", Context, eventId, label);
  143. #endif
  144. }
  145. /// <summary>
  146. /// 时长统计事件
  147. /// 与EventBegin要配对使用
  148. /// </summary>
  149. /// <param name="eventId">友盟后台设定的事件</param>
  150. /// <param name="label">分类标签</param>
  151. public static void EventEnd(string eventId, string label)
  152. {
  153. #if UNITY_EDITOR
  154. //Debug.Log("EndEvent");
  155. #elif UNITY_IPHONE
  156. _EndEventWithLabel(eventId, label);
  157. #elif UNITY_ANDROID
  158. Agent.CallStatic("onEventEnd", Context, eventId, label);
  159. #endif
  160. }
  161. /// <summary>
  162. /// 时长统计事件
  163. /// 与EventEndWithPrimarykey要配对使用 并传递相同的eventId 和 primaryKey
  164. ///
  165. /// </summary>
  166. /// <param name="eventId">友盟后台设定的事件</param>
  167. /// <param name="primaryKey">主键</param>
  168. /// <param name="attributes"></param>
  169. ///
  170. public static void EventBeginWithPrimarykeyAndAttributes(string eventId, string primaryKey, Dictionary<string, string> attributes)
  171. {
  172. #if UNITY_EDITOR
  173. //Debug.Log("EventBeginWithPrimarykeyAndAttributes");
  174. #elif UNITY_IPHONE
  175. _BeginEventWithPrimarykeyAndAttributes(eventId, primaryKey, DictionaryToJson(attributes));
  176. #elif UNITY_ANDROID
  177. Agent.CallStatic("onKVEventBegin", Context, eventId, ToJavaHashMap(attributes), primaryKey);
  178. #endif
  179. }
  180. /// <summary>
  181. /// 时长统计事件
  182. /// 与EventBeginWithPrimarykeyAndAttributes要配对使用 并传递相同的eventId 和 primaryKey
  183. /// </summary>
  184. /// <param name="eventId">友盟后台设定的事件</param>
  185. /// <param name="primaryKey">主键</param>
  186. public static void EventEndWithPrimarykey(string eventId, string primaryKey)
  187. {
  188. #if UNITY_EDITOR
  189. //Debug.Log("EventEndWithPrimarykey");
  190. #elif UNITY_IPHONE
  191. _EndEventWithPrimarykey(eventId, primaryKey);
  192. #elif UNITY_ANDROID
  193. Agent.CallStatic("onKVEventEnd", Context, eventId, primaryKey);
  194. #endif
  195. }
  196. /// <summary>
  197. /// 时长统计事件
  198. /// </summary>
  199. /// <param name="eventId">友盟后台设定的事件</param>
  200. /// <param name="milliseconds">时长 单位是毫秒</param>
  201. public static void EventDuration(string eventId, int milliseconds)
  202. {
  203. #if UNITY_EDITOR
  204. //Debug.Log("EventDuration");
  205. #elif UNITY_IPHONE
  206. _EventWithDuration(eventId, milliseconds);
  207. #elif UNITY_ANDROID
  208. Agent.CallStatic("onEventDuration", Context, eventId, (long)milliseconds);
  209. #endif
  210. }
  211. /// <summary>
  212. /// 时长统计事件
  213. /// </summary>
  214. /// <param name="eventId">友盟后台设定的事件</param>
  215. /// <param name="label">分类标签</param>
  216. /// <param name="milliseconds">时长 单位是毫秒</param>
  217. ///
  218. public static void EventDuration(string eventId, string label, int milliseconds)
  219. {
  220. #if UNITY_EDITOR
  221. //Debug.Log("EventDuration");
  222. #elif UNITY_IPHONE
  223. _EventWithDuration2(eventId,label,milliseconds);
  224. #elif UNITY_ANDROID
  225. Agent.CallStatic("onEventDuration", Context, eventId, label, (long)milliseconds);
  226. #endif
  227. }
  228. /// <summary>
  229. /// 时长统计事件
  230. /// </summary>
  231. /// <param name="eventId">友盟后台设定的事件</param>
  232. /// <param name="attributes"> 属性中的Key-Vaule Pair不能超过10个</param>
  233. /// <param name="milliseconds">时长 单位是毫秒</param>
  234. ///
  235. public static void EventDuration(string eventId, Dictionary<string, string> attributes, int milliseconds)
  236. {
  237. #if UNITY_EDITOR
  238. //Debug.Log("EventDuration");
  239. #elif UNITY_IPHONE
  240. _EventWithAttributesAndDuration(eventId, DictionaryToJson(attributes), milliseconds);
  241. #elif UNITY_ANDROID
  242. Agent.CallStatic("onEventDuration", Context, eventId, ToJavaHashMap(attributes), (long)milliseconds);
  243. #endif
  244. }
  245. /// <summary>
  246. /// 页面时长统计,记录某个页面被打开多长时间
  247. /// 与PageEnd配对使用
  248. /// </summary>
  249. /// <param name="pageName">被统计view名称</param>
  250. public static void PageBegin(string pageName)
  251. {
  252. #if UNITY_EDITOR
  253. //Debug.Log("PageBegin");
  254. #elif UNITY_IPHONE
  255. _BeginLogPageView(pageName);
  256. #elif UNITY_ANDROID
  257. Agent.CallStatic("onPageStart", pageName);
  258. #endif
  259. }
  260. /// <summary>
  261. /// 页面时长统计,记录某个页面被打开多长时间
  262. /// 与PageBegin配对使用
  263. /// </summary>
  264. /// <param name="pageName">被统计view名称</param>
  265. ///
  266. public static void PageEnd(string pageName)
  267. {
  268. #if UNITY_EDITOR
  269. //Debug.Log("PageEnd");
  270. #elif UNITY_IPHONE
  271. _EndLogPageView(pageName);
  272. #elif UNITY_ANDROID
  273. Agent.CallStatic("onPageEnd", pageName);
  274. #endif
  275. }
  276. /// <summary>
  277. /// 自定义事件 — 计算事件数
  278. /// </summary>
  279. public static void Event(string eventId, Dictionary<string, string> attributes, int value)
  280. {
  281. try
  282. {
  283. if (attributes == null)
  284. attributes = new System.Collections.Generic.Dictionary<string, string>();
  285. if (attributes.ContainsKey("__ct__"))
  286. {
  287. attributes["__ct__"] = value.ToString();
  288. Event(eventId, attributes);
  289. }
  290. else
  291. {
  292. attributes.Add("__ct__", value.ToString());
  293. Event(eventId, attributes);
  294. attributes.Remove("__ct__");
  295. }
  296. }
  297. catch (Exception)
  298. {
  299. }
  300. }
  301. /// <summary>
  302. /// 获取缓存的在线参数
  303. /// </summary>
  304. /// <param name="key">在线参数的Key 请在友盟后台设置</param>
  305. /// <returns>Key对应的在线参数值</returns>
  306. public static string GetDeviceInfo()
  307. {
  308. #if UNITY_EDITOR
  309. //Unity Editor 模式下 返回null 请在iOS/Anroid真机上测试
  310. //Debug.Log("GetDeviceInfo return null");
  311. return null;
  312. #elif UNITY_IPHONE
  313. return _GetDeviceID();
  314. #elif UNITY_ANDROID
  315. var util = new AndroidJavaClass("com.umeng.analytics.UnityUtil");
  316. var info = util.CallStatic<string>("getDeviceInfo", Context);
  317. return info;
  318. #endif
  319. }
  320. //设置是否对日志信息进行加密, 默认false(不加密).
  321. //value 设置为true, SDK会将日志信息做加密处理
  322. public static void SetLogEncryptEnabled(bool value)
  323. {
  324. #if UNITY_EDITOR
  325. //Debug.Log("SetLogEncryptEnabled");
  326. #elif UNITY_IPHONE
  327. _SetEncryptEnabled(value);
  328. #elif UNITY_ANDROID
  329. AnalyticsConfig.CallStatic("enableEncrypt", value);
  330. #endif
  331. }
  332. public static void SetLatency(int value)
  333. {
  334. #if UNITY_EDITOR
  335. //Debug.Log("SetLatency");
  336. #elif UNITY_IPHONE
  337. _SetLatency(value);
  338. #elif UNITY_ANDROID
  339. AnalyticsConfig.CallStatic("setLatencyWindow", (long)value);
  340. #endif
  341. }
  342. //Android Only
  343. #if UNITY_ANDROID
  344. //设置Session时长
  345. public static void SetContinueSessionMillis(long milliseconds)
  346. {
  347. #if UNITY_EDITOR
  348. //Debug.Log("setContinueSessionMillis");
  349. #else
  350. Agent.CallStatic("setSessionContinueMillis", milliseconds);
  351. #endif
  352. }
  353. [Obsolete("Flush")]
  354. //清空缓存
  355. public static void Flush()
  356. {
  357. #if UNITY_EDITOR
  358. //Debug.Log("flush");
  359. #else
  360. Agent.CallStatic("flush", Context);
  361. #endif
  362. }
  363. [Obsolete("SetEnableLocation已弃用")]
  364. //启用位置信息
  365. public static void SetEnableLocation(bool reportLocation)
  366. {
  367. #if UNITY_EDITOR
  368. //Debug.Log("setEnableLocation:"+ reportLocation);
  369. #else
  370. Agent.CallStatic("setAutoLocation", reportLocation);
  371. #endif
  372. }
  373. //启用页面统计
  374. public static void EnableActivityDurationTrack(bool isTraceActivity)
  375. {
  376. #if UNITY_EDITOR
  377. //Debug.Log("enableActivityDurationTrack:"+isTraceActivity);
  378. #else
  379. Agent.CallStatic("openActivityDurationTrack", isTraceActivity);
  380. #endif
  381. }
  382. /*
  383. android6.0中采集mac方式变更,新增接口 public static void setCheckDevice(boolean enable) 该接口默认参数是true,即采集mac地址,
  384. 但如果开发者需要在googleplay发布,考虑到审核风险,可以调用该接口,参数设置为 false 就不会采集mac地址。
  385. */
  386. public static void SetCheckDevice(bool value)
  387. {
  388. #if UNITY_EDITOR
  389. #else
  390. Agent.CallStatic("setCheckDevice", value);
  391. #endif
  392. }
  393. #endif
  394. //iOS Only
  395. #if UNITY_IPHONE
  396. /*
  397. BATCH(启动发送)为默认发送策略
  398. 关于发送策略的调整,请参见关于发送策略及发送策略变更的说明
  399. http://blog.umeng.com/index.php/2012/12/0601/
  400. SEND_INTERVAL 为按最小间隔发送,默认为10秒,取值范围为10 到 86400(一天), 如果不在这个区间的话,会按10设置。
  401. SEND_ON_EXIT 为退出或进入后台时发送,这种发送策略在App运行过程中不发送,对开发者和用户的影响最小。
  402. 不过这种发送策略只在iOS > 4.0时才会生效, iOS < 4.0 会被自动调整为BATCH。
  403. */
  404. public enum ReportPolicy
  405. {
  406. BATCH = 1,//启动发送
  407. SENDDAILY = 4, //每日发送
  408. SEND_INTERVAL = 6, //按最小间隔发送
  409. SEND_ON_EXIT = 7 //退出或进入后台时发送
  410. }
  411. /// <summary>
  412. /// 开启友盟统计
  413. /// </summary>
  414. /// <param name="appkey">友盟appKey</param>
  415. /// <param name="policy">发送策略</param>
  416. /// <param name="channelId">渠道名称</param>
  417. ///
  418. public static void StartWithAppKeyAndReportPolicyAndChannelId(string appkey, ReportPolicy policy, string channelId)
  419. {
  420. #if UNITY_EDITOR
  421. //Debug.LogWarning("友盟统计在iOS/Androi 真机上才会向友盟后台服务器发送事件 请在真机上测试");
  422. #else
  423. _StartWithAppKeyAndReportPolicyAndChannelId(appkey, (int)policy, channelId);
  424. _AppKey = appkey;
  425. _ChannelId = channelId;
  426. #endif
  427. }
  428. /// <summary>
  429. /// 当reportPolicy 为 SEND_INTERVAL 时设定log发送间隔
  430. /// </summary>
  431. /// <param name="seconds">单位为秒,最小为10,最大为86400(一天).</param>
  432. public static void SetLogSendInterval(int seconds)
  433. {
  434. #if UNITY_EDITOR
  435. //Debug.Log("SetLogSendInterval");
  436. #else
  437. _SetLogSendInterval((double)seconds);
  438. #endif
  439. }
  440. /// <summary>
  441. /// 手动设置app版本号 此API不再建议使用 因为启动时会自动读取Unity的PlayerSettings.bundleVersion(CFBundleVersion)作为版本
  442. /// </summary>
  443. /// <param name="value">版本号</param>
  444. [Obsolete("此API不再建议使用 因为启动时会自动读取Unity的PlayerSettings.bundleVersion(CFBundleVersion)作为版本")]
  445. public static void SetAppVersion(string value)
  446. {
  447. #if UNITY_EDITOR
  448. //Debug.Log("SetAppVersion");
  449. #else
  450. _SetAppVersion(value);
  451. #endif
  452. }
  453. /// <summary>
  454. /// 开启CrashReport收集, 默认是开启状态.
  455. /// </summary>
  456. /// <param name="value">设置成false,就可以关闭友盟CrashReport收集</param>
  457. public static void SetCrashReportEnabled(bool value)
  458. {
  459. #if UNITY_EDITOR
  460. //Debug.Log("SetCrashReportEnabled");
  461. #else
  462. //由于Unity在iOS平台使用AOT模式编译 你得到的CrashReport函数名将不是完全一致
  463. _SetCrashReportEnabled(value);
  464. //Anddroid 平台Crash Report 总是是开启的 无需调用SetCrashReportEnabled
  465. //Anddroid 平台Crash Report 仅限于Java层的崩溃日志
  466. #endif
  467. }
  468. /// <summary>
  469. /// 页面时长统计,记录某个view被打开多长时间,与调用PageBegin,PageEnd计时等价
  470. /// </summary>
  471. /// <param name="pageName">被统计view名称</param>
  472. /// <param name="seconds">时长单位为秒</param>
  473. ///
  474. public static void LogPageViewWithSeconds(string pageName, int seconds)
  475. {
  476. #if UNITY_EDITOR
  477. //Debug.Log("LogPageViewWithSeconds");
  478. #else
  479. _LogPageViewWithSeconds(pageName, seconds);
  480. #endif
  481. }
  482. /// <summary>
  483. /// 判断设备是否越狱,判断方法根据 apt和Cydia.app的path来判断
  484. /// </summary>
  485. /// <returns>是否越狱</returns>
  486. public static bool IsJailBroken()
  487. {
  488. #if UNITY_EDITOR
  489. //always return false in UNITY_EDITOR mode
  490. //Debug.Log("IsJailBroken always return false in UNITY_EDITOR mode");
  491. return false;
  492. #else
  493. return _IsJailBroken();
  494. #endif
  495. }
  496. /// <summary>
  497. /// 判断你的App是否被破解
  498. /// </summary>
  499. /// <returns>是否破解</returns>
  500. public static bool IsPirated()
  501. {
  502. #if UNITY_EDITOR
  503. //always return false in UNITY_EDITOR mode
  504. //Debug.Log("IsPirated always return false in UNITY_EDITOR mode");
  505. return false;
  506. #else
  507. return _IsPirated();
  508. #endif
  509. }
  510. //设置是否开启background模式, 默认true.
  511. //value 为YES,SDK会确保在app进入后台的短暂时间保存日志信息的完整性,对于已支持background模式和一般app不会有影响.
  512. //如果该模式影响某些App在切换到后台的功能,也可将该值设置为false.
  513. public static void SetBackgroundTaskEnabled(bool value)
  514. {
  515. #if UNITY_EDITOR
  516. //Debug.Log("SetBackgroundTaskEnabled");
  517. #elif UNITY_IPHONE
  518. _SetBackgroundTaskEnabled (value);
  519. #endif
  520. }
  521. #endif
  522. #region Wrapper
  523. static private string _AppKey=null;
  524. static private string _ChannelId=null;
  525. static public string AppKey
  526. {
  527. get
  528. {
  529. return _AppKey;
  530. }
  531. }
  532. static public string ChannelId
  533. {
  534. get
  535. {
  536. return _ChannelId;
  537. }
  538. }
  539. static private void CreateUmengManger()
  540. {
  541. GameObject go = new GameObject();
  542. go.AddComponent<UmengManager>();
  543. go.name = "UmengManager";
  544. }
  545. #if UNITY_ANDROID
  546. public static void onResume()
  547. {
  548. #if UNITY_EDITOR
  549. #else
  550. Agent.CallStatic("onResume", Context);
  551. #endif
  552. }
  553. public static void onResume(string appkey, string channelId)
  554. {
  555. #if UNITY_EDITOR
  556. #else
  557. Agent.CallStatic("onResume", Context, appkey, channelId);
  558. #endif
  559. }
  560. public static void onPause()
  561. {
  562. #if UNITY_EDITOR
  563. #else
  564. Agent.CallStatic("onPause", Context);
  565. #endif
  566. }
  567. public static void onKillProcess()
  568. {
  569. #if UNITY_EDITOR
  570. #else
  571. Agent.CallStatic("onKillProcess", Context);
  572. #endif
  573. }
  574. static AndroidJavaClass AnalyticsConfig = null;
  575. //lazy initialize singleton
  576. static class SingletonHolder
  577. {
  578. public static AndroidJavaClass instance_mobclick;
  579. public static AndroidJavaObject instance_context;
  580. static SingletonHolder()
  581. {
  582. //instance_mobclick will be null if you run in editor mode
  583. //try it on real android device
  584. instance_mobclick = new AndroidJavaClass("com.umeng.analytics.game.UMGameAgent");
  585. AnalyticsConfig = new AndroidJavaClass("com.umeng.analytics.AnalyticsConfig");
  586. //cls_UnityPlayer and instance_context will be null if you run in editor mode
  587. //try it on real android device
  588. using (AndroidJavaClass cls_UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
  589. {
  590. instance_context = cls_UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
  591. }
  592. }
  593. }
  594. private static AndroidJavaObject ToJavaHashMap(Dictionary<string, string> dic)
  595. {
  596. var hashMap = new AndroidJavaObject("java.util.HashMap");
  597. var putMethod = AndroidJNIHelper.GetMethodID(hashMap.GetRawClass(), "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
  598. var arguments = new object[2];
  599. foreach (var entry in dic)
  600. {
  601. using (var key = new AndroidJavaObject("java.lang.String", entry.Key))
  602. {
  603. using (var val = new AndroidJavaObject("java.lang.String", entry.Value))
  604. {
  605. arguments[0] = key;
  606. arguments[1] = val;
  607. AndroidJNI.CallObjectMethod(hashMap.GetRawObject(), putMethod, AndroidJNIHelper.CreateJNIArgArray(arguments));
  608. }
  609. } // end using
  610. } // end foreach
  611. return hashMap;
  612. }
  613. protected static AndroidJavaClass Agent
  614. {
  615. get
  616. {
  617. //instance_mobclick will be null if you run in editor mode
  618. //try it on real android device
  619. return SingletonHolder.instance_mobclick;
  620. }
  621. }
  622. static AndroidJavaClass _UpdateAgent;
  623. protected static AndroidJavaClass UpdateAgent
  624. {
  625. get
  626. {
  627. if (_UpdateAgent == null)
  628. _UpdateAgent = new AndroidJavaClass("com.umeng.update.UmengUpdateAgent");
  629. return _UpdateAgent;
  630. }
  631. }
  632. protected static AndroidJavaObject Context
  633. {
  634. get
  635. {
  636. //instance_mobclick will be null if you run in editor mode
  637. //try it on real android device
  638. return SingletonHolder.instance_context;
  639. }
  640. }
  641. public static void UMGameAgentInit()
  642. {
  643. Agent.CallStatic("initUnity", Context);
  644. }
  645. public void Dispose()
  646. {
  647. Agent.Dispose();
  648. Context.Dispose();
  649. }
  650. #endif
  651. #if UNITY_IPHONE
  652. static string DictionaryToJson(Dictionary<string, string> dict)
  653. {
  654. var builder = new StringBuilder("{");
  655. foreach (KeyValuePair<string, string> kv in dict)
  656. {
  657. builder.AppendFormat("\"{0}\":\"{1}\",", kv.Key, kv.Value);
  658. }
  659. builder[builder.Length - 1] = '}';
  660. return builder.ToString();
  661. }
  662. [DllImport("__Internal")]
  663. private static extern void _SetAppVersion(string value);
  664. [DllImport("__Internal")]
  665. private static extern string _GetAgentVersion();
  666. [DllImport("__Internal")]
  667. private static extern void _SetLogEnabled(bool value);
  668. [DllImport("__Internal")]
  669. private static extern void _SetCrashReportEnabled(bool value);
  670. [DllImport("__Internal")]
  671. private static extern void _StartWithAppKey(string appkey);
  672. [DllImport("__Internal")]
  673. private static extern void _StartWithAppKeyAndReportPolicyAndChannelId(string appkey, int policy, string channelId);
  674. [DllImport("__Internal")]
  675. private static extern void _SetLogSendInterval(double interval);
  676. [DllImport("__Internal")]
  677. private static extern void _Event(string eventId);
  678. [DllImport("__Internal")]
  679. private static extern void _EventWithDuration(string eventId, int duration);
  680. [DllImport("__Internal")]
  681. private static extern void _EventWithDuration2(string eventId, string Label, int duration);
  682. [DllImport("__Internal")]
  683. private static extern void _EventWithAttributesAndDuration(string eventId, string jsonString, int duration);
  684. [DllImport("__Internal")]
  685. private static extern void _EventWithLabel(string eventId, string label);
  686. [DllImport("__Internal")]
  687. private static extern void _EventWithAccumulation(string eventId, int accumulation);
  688. [DllImport("__Internal")]
  689. private static extern void _EventWithLabelAndAccumulation(string eventId, string label, int accumulation);
  690. [DllImport("__Internal")]
  691. private static extern void _EventWithAttributes(string eventId, string jsonstring);
  692. [DllImport("__Internal")]
  693. private static extern void _BeginEventWithLabel(string eventId, string label);
  694. [DllImport("__Internal")]
  695. private static extern void _EndEventWithLabel(string eventId, string label);
  696. [DllImport("__Internal")]
  697. private static extern void _BeginEventWithPrimarykeyAndAttributes(string eventId, string primaryKey, string jsonstring);
  698. [DllImport("__Internal")]
  699. private static extern void _EndEventWithPrimarykey(string eventId, string primaryKey);
  700. [DllImport("__Internal")]
  701. private static extern void _LogPageViewWithSeconds(string pageName, int seconds);
  702. [DllImport("__Internal")]
  703. private static extern void _BeginLogPageView(string pageName);
  704. [DllImport("__Internal")]
  705. private static extern void _EndLogPageView(string pageName);
  706. [DllImport("__Internal")]
  707. private static extern bool _IsJailBroken();
  708. [DllImport("__Internal")]
  709. private static extern bool _IsPirated();
  710. [DllImport("__Internal")]
  711. private static extern string _GetDeviceID();
  712. [DllImport("__Internal")]
  713. private static extern void _SetBackgroundTaskEnabled(bool value);
  714. [DllImport("__Internal")]
  715. private static extern void _SetEncryptEnabled(bool value);
  716. [DllImport("__Internal")]
  717. private static extern void _SetLatency(int value);
  718. #endif
  719. #endregion
  720. }
  721. }