IAPManager.cs 11 KB

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