IAPManager.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. using cn.sharesdk.unity3d;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using UnityEngine.Events;
  5. using UnityEngine.Purchasing;
  6. using UnityEngine.Advertisements;
  7. using UnityEngine.Purchasing.Extension;
  8. using System;
  9. using System.IO;
  10. using System.Collections;
  11. using System.Diagnostics;
  12. using System.Collections.Generic;
  13. using Debug = UnityEngine.Debug;
  14. public enum PayChannel
  15. {
  16. AliPay,
  17. UnityIAP,
  18. WXPay,
  19. }
  20. public class IAPManager : MonoBehaviour, IStoreListener
  21. {
  22. #region Config
  23. public static float ReloadADTime = 10f;
  24. public static float CreateADChestTime = 1200f;
  25. public static float CreateADChestTimer;
  26. public static DateTime? LastPlayADsTime;
  27. public static List<Chest> ADChests = new List<Chest>();
  28. public static UnityAction OnPlayAD;
  29. public static bool Inited;
  30. public static IAPManager Instance;
  31. //public static bool UseAlipayOnIOS = false;
  32. public static PayChannel PayChannel;
  33. public static IStoreController IStoreController;
  34. public static IExtensionProvider IExtensionProvider;
  35. public static List<string> ProductList = new List<string>();
  36. public static Dictionary<string, string> PackNameDictionary = new Dictionary<string, string>();
  37. public static Dictionary<string, string> ProductIdDictionary = new Dictionary<string, string>();
  38. public static Dictionary<string, UnityAction> BuyProductCallbackDictionary = new Dictionary<string, UnityAction>();
  39. #region Share配置
  40. public static string ImageUrl = "http://or5zgoeui.bkt.clouddn.com/game_icon.png";
  41. public static string ImagePath;
  42. public static ShareSDK ShareSdk;
  43. #endregion
  44. #endregion
  45. public void Awake()
  46. {
  47. Instance = this;
  48. InitAD();
  49. InitShareSDK();
  50. }
  51. public void Update()
  52. {
  53. if (ADChests.Count > 0)
  54. return;
  55. CreateADChestTimer += Time.deltaTime;
  56. if (CreateADChestTimer >= ReloadADTime)
  57. {
  58. CreateADChestTimer = 0;
  59. if (!Advertisement.IsReady())
  60. {
  61. InitAD();
  62. }
  63. else
  64. {
  65. if (LastPlayADsTime == null)
  66. return;
  67. if (HttpManager.IsConnect && (HttpManager.CurrentDateTime.Subtract((DateTime) LastPlayADsTime).TotalSeconds > CreateADChestTime))
  68. {
  69. if (GardenManager.InMinigameFlag && !VisitManager.InVisit && !TutorialManager.NewplayerTutorial && !SFSManager.PlazaRoomController.InPlazaRoom && !Player.InDressRoom)
  70. {
  71. ADChests.Add(ResourceManager.GetADChest());
  72. }
  73. }
  74. }
  75. }
  76. }
  77. public static void RetrieveADChest()
  78. {
  79. if (ADChests.Count > 0)
  80. {
  81. ResourceManager.Save(ADChests[0].transform.parent);
  82. ADChests.RemoveAt(0);
  83. }
  84. }
  85. public static void Init()
  86. {
  87. GameObject.Find(CanvasLabel.U_DebugLab).GetComponent<Text>().text = "Error Code : I1";
  88. InitIAP();
  89. GameObject.Find(CanvasLabel.U_DebugLab).GetComponent<Text>().text = "Error Code : I2";
  90. LastPlayADsTime = DateTime.Parse(ConfigManager.GetStringFormConfig(PlayerConfigLabel.ADPlayTime));
  91. GameObject.Find(CanvasLabel.U_DebugLab).GetComponent<Text>().text = "Error Code : I3";
  92. }
  93. public static void InitAD()
  94. {
  95. if (Application.platform == RuntimePlatform.IPhonePlayer)
  96. {
  97. Advertisement.Initialize("1408492", false);
  98. }
  99. else if (Application.isMobilePlatform)
  100. {
  101. Advertisement.Initialize("1408493", false);
  102. }
  103. else if (Application.isEditor)
  104. {
  105. Advertisement.Initialize("1408493", false);
  106. }
  107. }
  108. public static void InitShareSDK()
  109. {
  110. if (Application.isMobilePlatform || Application.platform == RuntimePlatform.IPhonePlayer)
  111. {
  112. ShareSdk = Instance.gameObject.AddComponent<ShareSDK>();
  113. ShareSdk.Initialize();
  114. ShareSdk.shareHandler = ShareCallback;
  115. }
  116. }
  117. public static void InitIAP()
  118. {
  119. if (Application.isEditor)
  120. {
  121. return;
  122. }
  123. PayChannel = ResourceManager.Load<TextAsset>(ResourceLabel.Setting, Folder.Config).text.ToEnum<PayChannel>();
  124. if (Application.platform == RuntimePlatform.IPhonePlayer)
  125. {
  126. InitUnityIAP();
  127. }
  128. else
  129. {
  130. if (PayChannel == PayChannel.AliPay)
  131. {
  132. InitAlipay();
  133. }
  134. else if (PayChannel == PayChannel.UnityIAP)
  135. {
  136. InitUnityIAP();
  137. }
  138. }
  139. }
  140. public static void InitAlipay()
  141. {
  142. foreach (var attribute in ConfigManager.GetIAPConfig())
  143. {
  144. ProductList.Add(attribute[1].Value);
  145. ProductIdDictionary.Add(attribute[5].Value, attribute[1].Value);
  146. PackNameDictionary.Add(attribute[1].Value, attribute[5].Value);
  147. }
  148. AliplayManager.Instance.Init();
  149. }
  150. public static void InitUnityIAP()
  151. {
  152. ConfigurationBuilder cb = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
  153. foreach (var attribute in ConfigManager.GetIAPConfig())
  154. {
  155. cb.AddProduct(attribute[1].Value, ProductType.Consumable);
  156. ProductList.Add(attribute[1].Value);
  157. ProductIdDictionary.Add(attribute[5].Value, attribute[1].Value);
  158. PackNameDictionary.Add(attribute[1].Value, attribute[5].Value);
  159. }
  160. UnityPurchasing.Initialize(Instance, cb);
  161. }
  162. public static void PlayAD(UnityAction action)
  163. {
  164. OnPlayAD = () =>
  165. {
  166. action.Invoke();
  167. StaticsManager.GetInstance().AdFinished();
  168. };
  169. if (Advertisement.IsReady())
  170. {
  171. Manager.ReactiveFlag = true;
  172. ShowOptions showOptions = new ShowOptions();
  173. showOptions.resultCallback = PlayADCallback;
  174. Advertisement.Show(showOptions);
  175. StaticsManager.GetInstance().AdChannel(0);
  176. }
  177. else
  178. {
  179. Bubble.Show(null, Language.GetStr(LanguageLabel.IAP__LoadAd));
  180. }
  181. }
  182. public static void PlayADCallback(ShowResult showResult)
  183. {
  184. if (showResult == ShowResult.Finished)
  185. {
  186. OnPlayAD.Invoke();
  187. Manager.PlayADsAmt++;
  188. LastPlayADsTime = HttpManager.CurrentDateTime;
  189. ConfigManager.SaveStringToConfig(PlayerConfigLabel.ADPlayTime, LastPlayADsTime.ToString());
  190. }
  191. else if (showResult == ShowResult.Skipped)
  192. {
  193. Bubble.Show(null, Language.GetStr(LanguageLabel.IAP__IncompleteAd));
  194. }
  195. else if (showResult == ShowResult.Failed)
  196. {
  197. Bubble.Show(null, Language.GetStr(LanguageLabel.IAP__FailAd));
  198. }
  199. }
  200. public static void TryPurchase(string str)
  201. {
  202. if (!Inited)
  203. {
  204. Bubble.Show(null, Language.GetStr(LanguageLabel.IAP__PurchaseFail));
  205. InitIAP();
  206. return;
  207. }
  208. if (!ProductList.Contains(ProductIdDictionary[str]))
  209. {
  210. Bubble.Show(null, Language.GetStr(LanguageLabel.IAP__PurchaseUnvalid));
  211. return;
  212. }
  213. if (RechargeGiftManager.Inited)
  214. {
  215. SelectPayChannel(str);
  216. }
  217. else
  218. {
  219. HttpManager.GetThanksGiftInfo
  220. (
  221. jData =>
  222. {
  223. RechargeGiftManager.Init(jData);
  224. SelectPayChannel(str);
  225. },
  226. () => { Bubble.Show(null, Language.GetStr(LanguageLabel.UI__GetThanksGiftInfoFailed)); }
  227. );
  228. }
  229. }
  230. private static void SelectPayChannel(string str)
  231. {
  232. if (PayChannel != PayChannel.UnityIAP)
  233. {
  234. PayCard.Init();
  235. PayCard.Show(() => Purchase(str));
  236. }
  237. else
  238. {
  239. Purchase(str);
  240. }
  241. }
  242. private static void Purchase(string str)
  243. {
  244. if (PayChannel == PayChannel.AliPay)
  245. {
  246. HttpManager.GetAlipayProductID
  247. (
  248. ProductIdDictionary[str],
  249. data =>
  250. {
  251. AliplayManager.Instance.Pay(data, str);
  252. }
  253. );
  254. }
  255. else if (PayChannel == PayChannel.WXPay)
  256. {
  257. HttpManager.GetWXPayProductID
  258. (
  259. ProductIdDictionary[str],
  260. data =>
  261. {
  262. WXPayManager.Pay(data, str);
  263. }
  264. );
  265. }
  266. else if (PayChannel == PayChannel.UnityIAP)
  267. {
  268. IStoreController.InitiatePurchase(ProductIdDictionary[str]);
  269. }
  270. }
  271. public void OnInitialized(IStoreController controller, IExtensionProvider extensions) //
  272. {
  273. Inited = true;
  274. IStoreController = controller;
  275. IExtensionProvider = extensions;
  276. }
  277. public void OnInitializeFailed(InitializationFailureReason error)
  278. {
  279. Inited = false;
  280. }
  281. public void OnPurchaseFailed(Product i, PurchaseFailureReason p)
  282. {
  283. Bubble.Show(null, Language.GetStr(LanguageLabel.IAP__PurchaseFail));
  284. }
  285. public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e)
  286. {
  287. Bubble.Show(null, Language.GetStr(LanguageLabel.IAP__PurchaseSuccess));
  288. string packName = PackNameDictionary[e.purchasedProduct.definition.id];
  289. BuyProductCallbackDictionary[packName].Invoke();
  290. return PurchaseProcessingResult.Complete;
  291. }
  292. public static void Share(string imagePath = null, string content = null)
  293. {
  294. try
  295. {
  296. ShareContent shareContent = new ShareContent();
  297. if (content == null)
  298. {
  299. shareContent.SetText(Language.GetStr(LanguageLabel.Common__ShareContent));
  300. }
  301. else
  302. {
  303. shareContent.SetText(content);
  304. }
  305. shareContent.SetTitle(Language.GetStr(LanguageLabel.Common__ShareTit));
  306. shareContent.SetTitleUrl(Language.GetShareUrl());
  307. shareContent.SetSite(Language.GetShareUrl());
  308. shareContent.SetSiteUrl(Language.GetShareUrl());
  309. shareContent.SetUrl(Language.GetShareUrl());
  310. shareContent.SetShareType(ContentType.App);
  311. if (string.IsNullOrEmpty(imagePath))
  312. {
  313. shareContent.SetImageUrl(ImageUrl);
  314. }
  315. else
  316. {
  317. ImagePath = imagePath;
  318. shareContent.SetImagePath(imagePath);
  319. }
  320. ShareSdk.ShowPlatformList(null, shareContent, 100, 100);
  321. }
  322. catch (Exception e)
  323. {
  324. DebugManager.Log(e.ToString());
  325. }
  326. }
  327. public static void Authorize()
  328. {
  329. ShareSdk.Authorize(PlatformType.QQ);
  330. }
  331. public static void ShareCallback(int reqID, ResponseState state, PlatformType type, Hashtable result)
  332. {
  333. if (!string.IsNullOrEmpty(ImagePath))
  334. {
  335. ResourceManager.SetActive(CanvasLabel.P_Open, true);
  336. ResourceManager.SetActive(CanvasLabel.P_Share, true);
  337. ImagePath = null;
  338. }
  339. if (state == ResponseState.Success)
  340. {
  341. Manager.ShareAmt++;
  342. Bubble.Show(null, Language.GetStr(LanguageLabel.Common__ShareSuccess));
  343. }
  344. else if (state == ResponseState.Fail)
  345. {
  346. Bubble.Show(null, Language.GetStr(LanguageLabel.Common__ShareFail));
  347. }
  348. else if (state == ResponseState.Cancel)
  349. {
  350. Bubble.Show(null, Language.GetStr(LanguageLabel.Common__ShareFail));
  351. }
  352. }
  353. public static void AuthorizeCallback(int reqID, ResponseState state, PlatformType type, Hashtable result)
  354. {
  355. if (state == ResponseState.Success)
  356. {
  357. print("Success");
  358. }
  359. else if (state == ResponseState.Fail)
  360. {
  361. print("Fail");
  362. }
  363. else if (state == ResponseState.Cancel)
  364. {
  365. print("Cancel");
  366. }
  367. }
  368. }