SDKManager.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  1. using FrameWork;
  2. using FrameWork.SDKManager;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Reflection;
  7. using UnityEngine;
  8. namespace FrameWork.SDKManager
  9. {
  10. public static class SDKManager
  11. {
  12. static bool isInit = false;
  13. #if UNITY_ANDROID
  14. public const string c_ConfigName = "SDKConfig_Android";
  15. #elif UNITY_IOS
  16. public const string c_ConfigName = "SDKConfig_IOS";
  17. #else
  18. public const string c_ConfigName = "SDKConfig";
  19. #endif
  20. public const string c_KeyName = "SDKInfo";
  21. static List<LoginInterface> s_loginServiceList = null;
  22. static List<PayInterface> s_payServiceList = null;
  23. static List<ADInterface> s_ADServiceList = null;
  24. static List<LogInterface> s_logServiceList = null;
  25. static List<OtherSDKInterface> s_otherServiceList = null;
  26. private static PayCallBack s_payCallBack;
  27. private static ADCallBack s_adCallBack;
  28. static Dictionary<string, OtherCallBack> s_callBackDict = new Dictionary<string, OtherCallBack>();
  29. static bool s_useNewSDKManager = false; //是否使用新版本SDKManager
  30. #region 属性
  31. public static LoginCallBack LoginCallBack { get; set; }
  32. public static PayCallBack PayCallBack
  33. {
  34. get
  35. {
  36. return s_payCallBack;
  37. }
  38. set
  39. {
  40. s_payCallBack = value;
  41. }
  42. }
  43. public static ADCallBack ADCallBack
  44. {
  45. get
  46. {
  47. return s_adCallBack;
  48. }
  49. set
  50. {
  51. s_adCallBack = value;
  52. }
  53. }
  54. #endregion
  55. #region 外部调用
  56. #region 初始化
  57. /// <summary>
  58. /// 初始化
  59. /// </summary>
  60. public static void Init()
  61. {
  62. if (!isInit)
  63. {
  64. isInit = true;
  65. try
  66. {
  67. if (ConfigManager.GetIsExistConfig(c_ConfigName))
  68. {
  69. SchemeData data = LoadGameSchemeConfig();
  70. s_useNewSDKManager = data.UseNewSDKManager;
  71. if (s_useNewSDKManager)
  72. {
  73. SDKManagerNew.Init();
  74. }
  75. Debug.Log("SDKManager Init");
  76. LoadService(data);
  77. InitSDK();
  78. AutoListenerInit();
  79. }
  80. }
  81. catch (Exception e)
  82. {
  83. Debug.LogError("SDKManager Init Exception: " + e.ToString());
  84. }
  85. }
  86. }
  87. /// <summary>
  88. /// 额外初始化,SDKName == null时初始化全体
  89. /// </summary>
  90. public static void ExtraInit(SDKType type, string sdkName = null, string tag = null)
  91. {
  92. if (sdkName != null)
  93. {
  94. switch (type)
  95. {
  96. case SDKType.AD:
  97. GetADService(sdkName).ExtraInit(tag); break;
  98. case SDKType.Log:
  99. GetLogService(sdkName).ExtraInit(tag); break;
  100. case SDKType.Login:
  101. GetLoginService(sdkName).ExtraInit(tag); break;
  102. case SDKType.Other:
  103. GetOtherService(sdkName).ExtraInit(tag); break;
  104. case SDKType.Pay:
  105. GetPayService(sdkName).ExtraInit(tag); break;
  106. }
  107. }
  108. else
  109. {
  110. switch (type)
  111. {
  112. case SDKType.AD:
  113. AllExtraInit(s_ADServiceList, tag); break;
  114. case SDKType.Log:
  115. AllExtraInit(s_logServiceList, tag); break;
  116. case SDKType.Login:
  117. AllExtraInit(s_loginServiceList, tag); break;
  118. case SDKType.Other:
  119. AllExtraInit(s_otherServiceList, tag); break;
  120. case SDKType.Pay:
  121. AllExtraInit(s_payServiceList, tag); break;
  122. }
  123. }
  124. }
  125. static void AllExtraInit<T>(List<T> list, string tag = null) where T : SDKInterfaceBase
  126. {
  127. for (int i = 0; i < list.Count; i++)
  128. {
  129. try
  130. {
  131. list[i].ExtraInit(tag);
  132. }
  133. catch (Exception e)
  134. {
  135. Debug.LogError("AllExtraInit Exception " + list[i].m_SDKName + " " + e.ToString());
  136. }
  137. }
  138. }
  139. static void CheckInit()
  140. {
  141. if(!isInit)
  142. {
  143. throw new Exception("SDKManager not init !");
  144. }
  145. }
  146. #endregion
  147. #region 获取SDKInterface
  148. public static LoginInterface GetLoginService<T>() where T : LoginInterface
  149. {
  150. return GetLoginService(typeof(T).Name);
  151. }
  152. public static LoginInterface GetLoginService(string SDKName)
  153. {
  154. return GetSDKService(s_loginServiceList, SDKName);
  155. }
  156. public static LoginInterface GetLoginService(int index)
  157. {
  158. if (s_loginServiceList.Count <= index)
  159. {
  160. throw new Exception("GetLoginService error index->" + index + " count->" + s_loginServiceList.Count);
  161. }
  162. return s_loginServiceList[index];
  163. }
  164. public static PayInterface GetPayService<T>() where T : PayInterface
  165. {
  166. return GetPayService(typeof(T).Name);
  167. }
  168. public static PayInterface GetPayService(string SDKName)
  169. {
  170. return GetSDKService(s_payServiceList, SDKName);
  171. }
  172. public static PayInterface GetPayService(int index)
  173. {
  174. if (s_payServiceList.Count <= index)
  175. {
  176. throw new Exception("GetPayService error index->" + index + " count->" + s_payServiceList.Count);
  177. }
  178. return s_payServiceList[index];
  179. }
  180. public static ADInterface GetADService<T>() where T : ADInterface
  181. {
  182. return GetADService(typeof(T).Name);
  183. }
  184. public static ADInterface GetADService(string SDKName)
  185. {
  186. return GetSDKService(s_ADServiceList, SDKName);
  187. }
  188. public static ADInterface GetADService(int index)
  189. {
  190. if (s_ADServiceList.Count <= index)
  191. {
  192. throw new Exception("GetADService error index->" + index + " count->" + s_ADServiceList.Count);
  193. }
  194. return s_ADServiceList[index];
  195. }
  196. public static LogInterface GetLogService<T>() where T : LogInterface
  197. {
  198. return GetLogService(typeof(T).Name);
  199. }
  200. public static LogInterface GetLogService(string SDKName)
  201. {
  202. return GetSDKService(s_logServiceList, SDKName);
  203. }
  204. public static LogInterface GetLogService(int index)
  205. {
  206. if (s_logServiceList.Count <= index)
  207. {
  208. throw new Exception("GetLogService error index->" + index + " count->" + s_logServiceList.Count);
  209. }
  210. return s_logServiceList[index];
  211. }
  212. public static OtherSDKInterface GetOtherService<T>() where T : OtherSDKInterface
  213. {
  214. return GetOtherService(typeof(T).Name);
  215. }
  216. public static OtherSDKInterface GetOtherService(string SDKName)
  217. {
  218. return GetSDKService(s_otherServiceList, SDKName);
  219. }
  220. public static OtherSDKInterface GetOtherService(int index)
  221. {
  222. if (s_otherServiceList.Count <= index)
  223. {
  224. throw new Exception("GetOtherService error index->" + index + " count->" + s_otherServiceList.Count);
  225. }
  226. return s_otherServiceList[index];
  227. }
  228. #endregion
  229. #region 登录
  230. static void InitLogin(List<LoginInterface> list)
  231. {
  232. for (int i = 0; i < list.Count; i++)
  233. {
  234. try
  235. {
  236. //list[i].m_SDKName = list[i].GetType().Name;
  237. list[i].Init();
  238. //s_loginCallBack += list[i].m_callBack;
  239. }
  240. catch (Exception e)
  241. {
  242. Debug.LogError("Init LoginInterface SDK Exception:\n" + e.ToString());
  243. }
  244. }
  245. }
  246. /// <summary>
  247. /// 登陆,默认访问第一个接口
  248. /// </summary>
  249. public static void Login(string tag = "")
  250. {
  251. if (s_useNewSDKManager)
  252. {
  253. SDKManagerNew.Login();
  254. }
  255. else
  256. {
  257. try
  258. {
  259. GetLoginService(0).Login(tag);
  260. }
  261. catch (Exception e)
  262. {
  263. Debug.LogError("SDKManager Login Exception: " + e.ToString());
  264. }
  265. }
  266. }
  267. /// <summary>
  268. /// 登陆
  269. /// </summary>
  270. public static void LoginBySDKName(string SDKName, string tag = "")
  271. {
  272. if (s_useNewSDKManager)
  273. {
  274. SDKManagerNew.Login(SDKName, tag);
  275. }
  276. else
  277. {
  278. try
  279. {
  280. GetLoginService(SDKName).Login(tag);
  281. }
  282. catch (Exception e)
  283. {
  284. Debug.LogError("SDKManager Login Exception: " + SDKName + "===" + e.ToString());
  285. }
  286. }
  287. }
  288. /// <summary>
  289. /// 登陆
  290. /// </summary>
  291. public static void LoginByPlatform(LoginPlatform loginPlatform, string tag = "")
  292. {
  293. try
  294. {
  295. bool isHave = false;
  296. foreach (var item in s_loginServiceList)
  297. {
  298. if (item.GetPlatform().Contains(Application.platform) && item.GetLoginPlatform() == loginPlatform)
  299. {
  300. item.Login(tag);
  301. isHave = true;
  302. }
  303. }
  304. if (!isHave)
  305. {
  306. if (s_useNewSDKManager)
  307. {
  308. SDKManagerNew.Login(loginPlatform.ToString(), tag);
  309. }
  310. else
  311. {
  312. Debug.LogError("SDKManager Login dont find class by platform:" + Application.platform + " loginPlatform:" + loginPlatform);
  313. }
  314. }
  315. }
  316. catch (Exception e)
  317. {
  318. Debug.LogError("SDKManager Login Exception: " + e.ToString());
  319. }
  320. }
  321. public static List<LoginPlatform> GetSupportLoginPlatform()
  322. {
  323. List<LoginPlatform> platforms = new List<LoginPlatform>();
  324. try
  325. {
  326. foreach (var item in s_loginServiceList)
  327. {
  328. if (item.GetPlatform().Contains(Application.platform))
  329. {
  330. platforms.Add(item.GetLoginPlatform());
  331. }
  332. }
  333. if (s_useNewSDKManager)
  334. {
  335. List<LoginPlatform> newList = SDKManagerNew.GetSupportLoginPlatform();
  336. for (int i = 0; i < newList.Count; i++)
  337. {
  338. if (!platforms.Contains(newList[i]))
  339. {
  340. platforms.Add(newList[i]);
  341. }
  342. }
  343. }
  344. if (platforms.Count == 0)
  345. {
  346. Debug.LogError("SDKManager Login dont find class by platform:" + Application.platform + " please check config");
  347. }
  348. }
  349. catch (Exception e)
  350. {
  351. Debug.LogError("SDKManager Login Exception: " + e.ToString());
  352. }
  353. return platforms;
  354. }
  355. #endregion
  356. #region 支付
  357. static void InitPay(List<PayInterface> list)
  358. {
  359. for (int i = 0; i < list.Count; i++)
  360. {
  361. try
  362. {
  363. //list[i].m_SDKName = list[i].GetType().Name;
  364. list[i].Init();
  365. //list[i].m_PayResultCallBack= s_payCallBack;
  366. Debug.Log("初始化支付回调");
  367. }
  368. catch (Exception e)
  369. {
  370. Debug.LogError("Init PayInterface SDK Exception:\n" + e.ToString());
  371. }
  372. }
  373. }
  374. /// <summary>
  375. /// 支付,默认访问第一个接口
  376. /// </summary>
  377. public static void Pay(PayInfo payInfo)
  378. {
  379. //优先使用本地配置的SDK
  380. if (s_payServiceList.Count > 0)
  381. {
  382. try
  383. {
  384. GetPayService(0).Pay(payInfo.goodsID, payInfo.tag);
  385. }
  386. catch (Exception e)
  387. {
  388. Debug.LogError("SDKManager Pay Exception: " + e.ToString());
  389. }
  390. }
  391. else if (s_useNewSDKManager)
  392. {
  393. SDKManagerNew.Pay(payInfo);
  394. }
  395. else
  396. {
  397. Debug.Log("支付SDK 没有配置! ");
  398. }
  399. }
  400. /// <summary>
  401. /// 支付
  402. /// </summary>
  403. public static void Pay(string SDKName, PayInfo payInfo )
  404. {
  405. Debug.Log("Pay SDKname " + SDKName + " GetHasSDKService " + GetHasSDKService(s_payServiceList, SDKName));
  406. //优先使用本地配置的SDK
  407. if (GetHasSDKService(s_payServiceList, SDKName))
  408. {
  409. try
  410. {
  411. GetPayService(SDKName).Pay(payInfo.goodsID, payInfo.tag);
  412. }
  413. catch (Exception e)
  414. {
  415. Debug.LogError("SDKManager Pay Exception: " + e.ToString());
  416. }
  417. }
  418. else if (s_useNewSDKManager)
  419. {
  420. SDKManagerNew.Pay(SDKName, payInfo);
  421. }
  422. else
  423. {
  424. Debug.LogError("支付SDK 没有配置! ");
  425. }
  426. }
  427. /// <summary>
  428. /// 支付,默认访问第一个接口
  429. /// </summary>
  430. public static void ConfirmPay(string orderID, string tag = "")
  431. {
  432. try
  433. {
  434. GetPayService(0).ConfirmPay(orderID, tag);
  435. }
  436. catch (Exception e)
  437. {
  438. Debug.LogError("SDKManager Pay Exception: " + e.ToString());
  439. }
  440. }
  441. /// <summary>
  442. /// 支付
  443. /// </summary>
  444. public static void ConfirmPay(string SDKName, string orderID, string tag = "")
  445. {
  446. try
  447. {
  448. GetPayService(SDKName).ConfirmPay(orderID, tag);
  449. }
  450. catch (Exception e)
  451. {
  452. Debug.LogError("SDKManager Pay Exception: " + e.ToString());
  453. }
  454. }
  455. public static LocalizedGoodsInfo GetGoodsInfo(string goodsID, string tag = "")
  456. {
  457. try
  458. {
  459. return GetPayService(0).GetGoodsInfo(goodsID);
  460. }
  461. catch (Exception e)
  462. {
  463. Debug.LogError("SDKManager GetGoodsInfo Exception: " + e.ToString());
  464. }
  465. return null;
  466. }
  467. public static LocalizedGoodsInfo GetGoodsInfo(string SDKName, string goodsID, string tag = "")
  468. {
  469. try
  470. {
  471. return GetPayService(SDKName).GetGoodsInfo(goodsID);
  472. }
  473. catch (Exception e)
  474. {
  475. Debug.LogError("SDKManager GetGoodsInfo Exception: " + e.ToString());
  476. }
  477. return null;
  478. }
  479. public static List<LocalizedGoodsInfo> GetAllGoodsInfo(string tag = "")
  480. {
  481. try
  482. {
  483. return GetPayService(0).GetAllGoodsInfo();
  484. }
  485. catch (Exception e)
  486. {
  487. Debug.LogError("SDKManager GetGoodsInfo Exception: " + e.ToString());
  488. }
  489. return null;
  490. }
  491. public static List<LocalizedGoodsInfo> GetAllGoodsInfo(string SDKName, string tag = "")
  492. {
  493. try
  494. {
  495. return GetPayService(SDKName).GetAllGoodsInfo();
  496. }
  497. catch (Exception e)
  498. {
  499. Debug.LogError("SDKManager GetGoodsInfo Exception: " + e.ToString());
  500. }
  501. return null;
  502. }
  503. #endregion
  504. #region 广告
  505. /// <summary>
  506. /// 加载广告,默认访问第一个接口
  507. /// </summary>
  508. public static void LoadAD(ADType adType, string tag = "")
  509. {
  510. if (s_ADServiceList.Count > 0)
  511. {
  512. GetADService(0).LoadAD(adType, tag);
  513. }
  514. else
  515. {
  516. if (s_useNewSDKManager)
  517. {
  518. SDKManagerNew.LoadAD(adType, tag);
  519. }
  520. else
  521. {
  522. Debug.LogError("SDKManager LoadAD s_ADServiceList count is 0");
  523. }
  524. }
  525. }
  526. /// <summary>
  527. /// 加载广告
  528. /// </summary>
  529. public static void LoadAD(string SDKName, ADType adType, string tag = "")
  530. {
  531. if (s_useNewSDKManager)
  532. {
  533. SDKManagerNew.LoadAD(SDKName, adType, tag);
  534. }
  535. else
  536. {
  537. try
  538. {
  539. GetADService(SDKName).LoadAD(adType, tag);
  540. }
  541. catch (Exception e)
  542. {
  543. Debug.LogError("SDKManager LoadAD Exception: " + e.ToString());
  544. }
  545. }
  546. }
  547. /// <summary>
  548. /// 广告已加载成功,默认访问第一个接口
  549. /// </summary>
  550. public static bool ADIsLoaded(ADType adType, string tag = "")
  551. {
  552. if (s_useNewSDKManager)
  553. {
  554. return true;
  555. }
  556. else
  557. {
  558. try
  559. {
  560. return GetADService(0).IsLoaded(adType, tag);
  561. }
  562. catch (Exception e)
  563. {
  564. Debug.LogError("SDKManager LoadAD Exception: " + e.ToString());
  565. return false;
  566. }
  567. }
  568. }
  569. /// <summary>
  570. /// 加载广告成功
  571. /// </summary>
  572. public static bool ADIsLoaded(string SDKName, ADType adType, string tag = "")
  573. {
  574. try
  575. {
  576. if (GetHasSDKService(s_ADServiceList, SDKName))
  577. {
  578. return GetADService(SDKName).IsLoaded(adType, tag);
  579. }
  580. else
  581. {
  582. if (s_useNewSDKManager)
  583. {
  584. //TODO
  585. return true;
  586. }
  587. else
  588. {
  589. Debug.LogError("SDKManager ADIsLoaded Not find: " + SDKName);
  590. return false;
  591. }
  592. }
  593. }
  594. catch (Exception e)
  595. {
  596. Debug.LogError("SDKManager ADIsLoaded Exception: " + e.ToString());
  597. return false;
  598. }
  599. }
  600. /// <summary>
  601. /// 显示广告
  602. /// </summary>
  603. public static void PlayAD(ADType adType, string tag = "")
  604. {
  605. if (s_ADServiceList.Count > 0)
  606. {
  607. GetADService(0).PlayAD(adType, tag);
  608. }
  609. else
  610. {
  611. if (s_useNewSDKManager)
  612. {
  613. SDKManagerNew.PlayAD(adType, tag);
  614. }
  615. else
  616. {
  617. Debug.LogError("SDKManager PlayAD s_ADServiceList count is 0");
  618. }
  619. }
  620. }
  621. /// <summary>
  622. /// 显示广告
  623. /// </summary>
  624. public static void PlayAD(string SDKName, ADType adType, string tag = "")
  625. {
  626. try
  627. {
  628. if (GetHasSDKService(s_ADServiceList, SDKName))
  629. {
  630. GetADService(SDKName).PlayAD(adType, tag);
  631. }
  632. else
  633. {
  634. if (s_useNewSDKManager)
  635. {
  636. SDKManagerNew.PlayAD(SDKName, adType, tag);
  637. }
  638. else
  639. {
  640. Debug.LogError("SDKManager PlayAD Not find: " + SDKName);
  641. }
  642. }
  643. }
  644. catch (Exception e)
  645. {
  646. Debug.LogError("SDKManager PlayAD Exception: " + e.ToString());
  647. }
  648. }
  649. /// <summary>
  650. /// 隐藏广告
  651. /// </summary>
  652. /// <param name="adType"></param>
  653. public static void CloseAD(ADType adType, string tag = "")
  654. {
  655. if (s_ADServiceList.Count > 0)
  656. {
  657. GetADService(0).CloseAD(adType, tag);
  658. }
  659. else
  660. {
  661. if (s_useNewSDKManager)
  662. {
  663. SDKManagerNew.CloseAD(adType, tag);
  664. }
  665. else
  666. {
  667. Debug.LogError("SDKManager CloseAD s_ADServiceList count is 0");
  668. }
  669. }
  670. }
  671. /// <summary>
  672. /// 隐藏广告
  673. /// </summary>
  674. /// <param name="adType"></param>
  675. public static void CloseAD(string SDKName, ADType adType, string tag = "")
  676. {
  677. if (GetHasSDKService(s_ADServiceList, SDKName))
  678. {
  679. GetADService(SDKName).CloseAD(adType, tag);
  680. }
  681. else
  682. {
  683. if (s_useNewSDKManager)
  684. {
  685. SDKManagerNew.CloseAD(SDKName, adType, tag);
  686. }
  687. else
  688. {
  689. Debug.LogError("SDKManager CloseAD Not find: " + SDKName);
  690. }
  691. }
  692. }
  693. #endregion
  694. #region 数据上报
  695. /// <summary>
  696. /// 数据上报
  697. /// </summary>
  698. /// <param name="data"></param>
  699. public static void Log(string eventID, Dictionary<string, string> data)
  700. {
  701. CheckInit();
  702. if (s_useNewSDKManager)
  703. {
  704. SDKManagerNew.Log(eventID, data);
  705. }
  706. for (int i = 0; i < s_logServiceList.Count; i++)
  707. {
  708. try
  709. {
  710. s_logServiceList[i].Log(eventID, data);
  711. }
  712. catch (Exception e)
  713. {
  714. Debug.LogError("SDKManager Log Exception: " + e.ToString());
  715. }
  716. }
  717. }
  718. public static void LogLogin(string accountID, Dictionary<string, string> data = null)
  719. {
  720. CheckInit();
  721. if (s_useNewSDKManager)
  722. {
  723. SDKManagerNew.LogLogin(accountID, data);
  724. }
  725. for (int i = 0; i < s_logServiceList.Count; i++)
  726. {
  727. try
  728. {
  729. s_logServiceList[i].LogLogin(accountID, data);
  730. }
  731. catch (Exception e)
  732. {
  733. Debug.LogError("SDKManager LogLogin Exception: " + e.ToString());
  734. }
  735. }
  736. }
  737. public static void LogLoginOut(string accountID)
  738. {
  739. CheckInit();
  740. if (s_useNewSDKManager)
  741. {
  742. SDKManagerNew.LogLoginOut(accountID);
  743. }
  744. for (int i = 0; i < s_logServiceList.Count; i++)
  745. {
  746. try
  747. {
  748. s_logServiceList[i].LogLoginOut(accountID);
  749. }
  750. catch (Exception e)
  751. {
  752. Debug.LogError("SDKManager LogLoginOut Exception: " + e.ToString());
  753. }
  754. }
  755. }
  756. public static void LogPay(string orderID, string goodsID,int count, float price, string currency, string payment)
  757. {
  758. CheckInit();
  759. if (s_useNewSDKManager)
  760. {
  761. SDKManagerNew.LogPay(orderID, goodsID, count, price, currency,payment);
  762. }
  763. for (int i = 0; i < s_logServiceList.Count; i++)
  764. {
  765. try
  766. {
  767. s_logServiceList[i].LogPay(orderID, goodsID, count, price, currency, payment);
  768. }
  769. catch (Exception e)
  770. {
  771. Debug.LogError("SDKManager LogPay Exception: " + e.ToString());
  772. }
  773. }
  774. }
  775. public static void LogPaySuccess(string orderID)
  776. {
  777. CheckInit();
  778. if (s_useNewSDKManager)
  779. {
  780. SDKManagerNew.LogPaySuccess(orderID);
  781. }
  782. for (int i = 0; i < s_logServiceList.Count; i++)
  783. {
  784. try
  785. {
  786. s_logServiceList[i].LogPaySuccess(orderID);
  787. }
  788. catch (Exception e)
  789. {
  790. Debug.LogError("SDKManager LogPaySuccess Exception: " + e.ToString());
  791. }
  792. }
  793. }
  794. //以下三个配合使用,用于追踪虚拟物品的产出消耗
  795. public static void LogRewardVirtualCurrency(float count, string reason)
  796. {
  797. CheckInit();
  798. if (s_useNewSDKManager)
  799. {
  800. SDKManagerNew.LogRewardVirtualCurrency(count, reason);
  801. }
  802. for (int i = 0; i < s_logServiceList.Count; i++)
  803. {
  804. try
  805. {
  806. s_logServiceList[i].LogRewardVirtualCurrency(count, reason);
  807. }
  808. catch (Exception e)
  809. {
  810. Debug.LogError("SDKManager RewardVirtualCurrency Exception: " + e.ToString());
  811. }
  812. }
  813. }
  814. public static void LogPurchaseVirtualCurrency(string goodsID, int num, float price)
  815. {
  816. CheckInit();
  817. if (s_useNewSDKManager)
  818. {
  819. SDKManagerNew.LogPurchaseVirtualCurrency(goodsID, num, price);
  820. }
  821. for (int i = 0; i < s_logServiceList.Count; i++)
  822. {
  823. try
  824. {
  825. s_logServiceList[i].LogPurchaseVirtualCurrency(goodsID, num, price);
  826. }
  827. catch (Exception e)
  828. {
  829. Debug.LogError("SDKManager PurchaseVirtualCurrency Exception: " + e.ToString());
  830. }
  831. }
  832. }
  833. public static void LogUseItem(string goodsID, int num)
  834. {
  835. CheckInit();
  836. if (s_useNewSDKManager)
  837. {
  838. SDKManagerNew.LogUseItem(goodsID, num);
  839. }
  840. for (int i = 0; i < s_logServiceList.Count; i++)
  841. {
  842. try
  843. {
  844. s_logServiceList[i].LogUseItem(goodsID, num);
  845. }
  846. catch (Exception e)
  847. {
  848. Debug.LogError("SDKManager LogUseItem Exception: " + e.ToString());
  849. }
  850. }
  851. }
  852. #endregion
  853. #region 其他SDK
  854. public static void ToClipboard(string content)
  855. {
  856. #if UNITY_EDITOR
  857. GUIUtility.systemCopyBuffer = content;
  858. #elif UNITY_ANDROID
  859. SDKManagerNew.ToClipboard(content);
  860. #elif UNITY_IOS
  861. ClipboardManager.ToClipboard(content);
  862. #endif
  863. }
  864. public static void DownloadApk(string url = null)
  865. {
  866. #if UNITY_ANDROID
  867. SDKManagerNew.DownloadApk(url);
  868. #endif
  869. }
  870. public static void GetAPKSize(string url = null)
  871. {
  872. #if UNITY_ANDROID
  873. SDKManagerNew.GetAPKSize(url);
  874. #endif
  875. }
  876. /// <summary>
  877. /// 读取注入配置
  878. /// </summary>
  879. public static string GetProperties(string key,string defaultValue = "")
  880. {
  881. return GetProperties(SDKInterfaceDefine.FileName_ChannelProperties,key, defaultValue);
  882. }
  883. /// <summary>
  884. /// 读取注入配置
  885. /// </summary>
  886. public static string GetProperties(string properties, string key, string defaultValue)
  887. {
  888. return SDKManagerNew.GetProperties(properties, key, defaultValue);
  889. }
  890. #region 事件监听
  891. public static void AddOtherCallBackListener(string functionName, OtherCallBack callBack)
  892. {
  893. if (s_callBackDict.ContainsKey(functionName))
  894. {
  895. s_callBackDict[functionName] += callBack;
  896. }
  897. else
  898. {
  899. s_callBackDict.Add(functionName, callBack);
  900. }
  901. if(s_useNewSDKManager)
  902. {
  903. SDKManagerNew.AddOtherCallBackListener(functionName, callBack);
  904. }
  905. }
  906. public static void RemoveOtherCallBackListener(string functionName, OtherCallBack callBack)
  907. {
  908. if (s_callBackDict.ContainsKey(functionName))
  909. {
  910. s_callBackDict[functionName] -= callBack;
  911. }
  912. else
  913. {
  914. Debug.LogError("RemoveOtherCallBackListener 不存在的 function Name ->" + functionName + "<-");
  915. }
  916. if (s_useNewSDKManager)
  917. {
  918. SDKManagerNew.RemoveOtherCallBackListener(functionName, callBack);
  919. }
  920. }
  921. #endregion
  922. #endregion
  923. #endregion
  924. #region 加载SDK设置
  925. /// <summary>
  926. /// 读取当前游戏内的SDK配置
  927. /// 找不到或者解析失败会返回Null
  928. /// </summary>
  929. /// <returns></returns>
  930. public static SchemeData LoadGameSchemeConfig()
  931. {
  932. if (ConfigManager.GetIsExistConfig(c_ConfigName))
  933. {
  934. Debug.Log("LoadGameSchemeConfig");
  935. Dictionary<string, SingleField> configData = ConfigManager.GetData(c_ConfigName);
  936. return JsonUtility.FromJson<SchemeData>(configData[c_KeyName].GetString());
  937. }
  938. else
  939. {
  940. Debug.Log("LoadGameSchemeConfig null");
  941. return null;
  942. }
  943. }
  944. public static void AnalyzeSchemeData(
  945. SchemeData schemeData,
  946. out List<LoginInterface> loginScheme,
  947. out List<ADInterface> ADScheme,
  948. out List<PayInterface> payScheme,
  949. out List<LogInterface> logScheme,
  950. out List<OtherSDKInterface> otherScheme
  951. )
  952. {
  953. if (schemeData != null)
  954. {
  955. loginScheme = new List<LoginInterface>();
  956. for (int i = 0; i < schemeData.LoginScheme.Count; i++)
  957. {
  958. loginScheme.Add((LoginInterface)AnalysisConfig(schemeData.LoginScheme[i]));
  959. }
  960. ADScheme = new List<ADInterface>();
  961. for (int i = 0; i < schemeData.ADScheme.Count; i++)
  962. {
  963. ADScheme.Add((ADInterface)AnalysisConfig(schemeData.ADScheme[i]));
  964. }
  965. payScheme = new List<PayInterface>();
  966. for (int i = 0; i < schemeData.PayScheme.Count; i++)
  967. {
  968. payScheme.Add((PayInterface)AnalysisConfig(schemeData.PayScheme[i]));
  969. }
  970. logScheme = new List<LogInterface>();
  971. for (int i = 0; i < schemeData.LogScheme.Count; i++)
  972. {
  973. logScheme.Add((LogInterface)AnalysisConfig(schemeData.LogScheme[i]));
  974. }
  975. otherScheme = new List<OtherSDKInterface>();
  976. for (int i = 0; i < schemeData.OtherScheme.Count; i++)
  977. {
  978. otherScheme.Add((OtherSDKInterface)AnalysisConfig(schemeData.OtherScheme[i]));
  979. }
  980. }
  981. else
  982. {
  983. loginScheme = new List<LoginInterface>();
  984. ADScheme = new List<ADInterface>();
  985. payScheme = new List<PayInterface>();
  986. logScheme = new List<LogInterface>();
  987. otherScheme = new List<OtherSDKInterface>();
  988. }
  989. }
  990. static SDKInterfaceBase AnalysisConfig(SDKConfigData data)
  991. {
  992. if (data == null)
  993. {
  994. return new NullSDKInterface();
  995. }
  996. else
  997. {
  998. return (SDKInterfaceBase)JsonUtility.FromJson(data.SDKContent, Assembly.Load("Assembly-CSharp").GetType(data.SDKName));
  999. }
  1000. }
  1001. #endregion
  1002. #region 初始化SDK
  1003. static void LoadService(SchemeData data)
  1004. {
  1005. AnalyzeSchemeData(
  1006. data,
  1007. out s_loginServiceList,
  1008. out s_ADServiceList,
  1009. out s_payServiceList,
  1010. out s_logServiceList,
  1011. out s_otherServiceList
  1012. );
  1013. }
  1014. static void InitSDK()
  1015. {
  1016. InitLogin(s_loginServiceList);
  1017. InitSDKList(s_ADServiceList);
  1018. InitPay(s_payServiceList);
  1019. InitSDKList(s_logServiceList);
  1020. InitSDKList(s_otherServiceList);
  1021. //Debug.Log("s_loginServiceList: " + s_loginServiceList.Count);
  1022. }
  1023. #endregion
  1024. #region 功能函数
  1025. static T GetSDKService<T>(List<T> list, string name) where T : SDKInterfaceBase
  1026. {
  1027. for (int i = 0; i < list.Count; i++)
  1028. {
  1029. if (list[i].m_SDKName == name)
  1030. {
  1031. return list[i];
  1032. }
  1033. }
  1034. throw new Exception("GetSDKService " + typeof(T).Name + " Exception dont find ->" + name + "<-");
  1035. }
  1036. static bool GetHasSDKService<T>(List<T> list, string name) where T : SDKInterfaceBase
  1037. {
  1038. for (int i = 0; i < list.Count; i++)
  1039. {
  1040. if (list[i].m_SDKName == name)
  1041. {
  1042. return true;
  1043. }
  1044. }
  1045. return false;
  1046. }
  1047. static void InitSDKList<T>(List<T> list) where T : SDKInterfaceBase
  1048. {
  1049. for (int i = 0; i < list.Count; i++)
  1050. {
  1051. try
  1052. {
  1053. list[i].m_SDKName = list[i].GetType().Name;
  1054. list[i].Init();
  1055. }
  1056. catch (Exception e)
  1057. {
  1058. Debug.LogError("Init " + typeof(T).Name + " SDK Exception:\n" + e.ToString());
  1059. }
  1060. }
  1061. }
  1062. #endregion
  1063. #region 自动监听上报
  1064. /// <summary>
  1065. /// 自动上报监听初始化
  1066. /// </summary>
  1067. static void AutoListenerInit()
  1068. {
  1069. PayCallBack += OnPayCallBackListener;
  1070. }
  1071. /// <summary>
  1072. /// 自动上报支付
  1073. /// </summary>
  1074. static void OnPayCallBackListener(OnPayInfo info)
  1075. {
  1076. if (info.isSuccess)
  1077. {
  1078. LogPay(info.orderID,info.goodsId,1,info.price,info.currency, info.storeName.ToString());
  1079. }
  1080. }
  1081. #endregion
  1082. }
  1083. #region 声明
  1084. public delegate void LoginCallBack(OnLoginInfo info);
  1085. public delegate void PayCallBack(OnPayInfo info);
  1086. public delegate void ADCallBack(OnADInfo info);
  1087. public delegate void OtherCallBack(OnOtherInfo info);
  1088. public class SchemeData
  1089. {
  1090. public string SchemeName;
  1091. public bool UseNewSDKManager;
  1092. public List<SDKConfigData> LogScheme = new List<SDKConfigData>();
  1093. public List<SDKConfigData> LoginScheme = new List<SDKConfigData>();
  1094. public List<SDKConfigData> ADScheme = new List<SDKConfigData>();
  1095. public List<SDKConfigData> PayScheme = new List<SDKConfigData>();
  1096. public List<SDKConfigData> OtherScheme = new List<SDKConfigData>();
  1097. }
  1098. [System.Serializable]
  1099. public class SDKConfigData
  1100. {
  1101. public string SDKName;
  1102. public string SDKContent;
  1103. }
  1104. public enum SDKType
  1105. {
  1106. Log,
  1107. Login,
  1108. AD,
  1109. Pay,
  1110. Other,
  1111. }
  1112. #endregion
  1113. }