IAPManager.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. InitUnityIAP();
  126. }
  127. else
  128. {
  129. if (PayChannel == PayChannel.AliPay)
  130. {
  131. InitAlipay();
  132. }
  133. else if (PayChannel == PayChannel.UnityIAP)
  134. {
  135. InitUnityIAP();
  136. }
  137. }
  138. }
  139. public static void InitAlipay()
  140. {
  141. foreach (var attribute in ConfigManager.GetIAPConfig())
  142. {
  143. ProductList.Add(attribute[1].Value);
  144. ProductIdDictionary.Add(attribute[5].Value, attribute[1].Value);
  145. PackNameDictionary.Add(attribute[1].Value, attribute[5].Value);
  146. }
  147. AliplayManager.Instance.Init();
  148. }
  149. public static void InitUnityIAP()
  150. {
  151. ConfigurationBuilder cb = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
  152. foreach (var attribute in ConfigManager.GetIAPConfig())
  153. {
  154. cb.AddProduct(attribute[1].Value, ProductType.Consumable);
  155. ProductList.Add(attribute[1].Value);
  156. ProductIdDictionary.Add(attribute[5].Value, attribute[1].Value);
  157. PackNameDictionary.Add(attribute[1].Value, attribute[5].Value);
  158. }
  159. UnityPurchasing.Initialize(Instance, cb);
  160. }
  161. public static void PlayAD(UnityAction action)
  162. {
  163. OnPlayAD = () =>
  164. {
  165. action.Invoke();
  166. StaticsManager.GetInstance().AdFinished();
  167. };
  168. if (Advertisement.IsReady())
  169. {
  170. Manager.ReactiveFlag = true;
  171. ShowOptions showOptions = new ShowOptions();
  172. showOptions.resultCallback = PlayADCallback;
  173. Advertisement.Show(showOptions);
  174. StaticsManager.GetInstance().AdChannel(0);
  175. }
  176. else
  177. {
  178. Bubble.Show(null, Language.GetStr(LanguageLabel.IAP__LoadAd));
  179. }
  180. }
  181. public static void PlayADCallback(ShowResult showResult)
  182. {
  183. if (showResult == ShowResult.Finished)
  184. {
  185. OnPlayAD.Invoke();
  186. Manager.PlayADsAmt++;
  187. LastPlayADsTime = HttpManager.CurrentDateTime;
  188. ConfigManager.SaveStringToConfig(PlayerConfigLabel.ADPlayTime, LastPlayADsTime.ToString());
  189. }
  190. else if (showResult == ShowResult.Skipped)
  191. {
  192. Bubble.Show(null, Language.GetStr(LanguageLabel.IAP__IncompleteAd));
  193. }
  194. else if (showResult == ShowResult.Failed)
  195. {
  196. Bubble.Show(null, Language.GetStr(LanguageLabel.IAP__FailAd));
  197. }
  198. }
  199. public static void TryPurchase(string str)
  200. {
  201. if (!Inited)
  202. {
  203. Bubble.Show(null, Language.GetStr(LanguageLabel.IAP__PurchaseFail));
  204. InitIAP();
  205. return;
  206. }
  207. if (!ProductList.Contains(ProductIdDictionary[str]))
  208. {
  209. Bubble.Show(null, Language.GetStr(LanguageLabel.IAP__PurchaseUnvalid));
  210. return;
  211. }
  212. if (RechargeGiftManager.Inited)
  213. {
  214. Purchase(str);
  215. }
  216. else
  217. {
  218. HttpManager.GetThanksGiftInfo
  219. (
  220. jData =>
  221. {
  222. RechargeGiftManager.Init(jData);
  223. Purchase(str);
  224. },
  225. () => { Bubble.Show(null, Language.GetStr(LanguageLabel.UI__GetThanksGiftInfoFailed)); }
  226. );
  227. }
  228. }
  229. private static void Purchase(string str)
  230. {
  231. if (PayChannel == PayChannel.AliPay)
  232. {
  233. HttpManager.GetProductID
  234. (
  235. ProductIdDictionary[str],
  236. data =>
  237. {
  238. AliplayManager.Instance.Pay(data, str);
  239. }
  240. );
  241. }
  242. else if (PayChannel == PayChannel.UnityIAP)
  243. {
  244. IStoreController.InitiatePurchase(ProductIdDictionary[str]);
  245. }
  246. }
  247. public void OnInitialized(IStoreController controller, IExtensionProvider extensions)//
  248. {
  249. Inited = true;
  250. IStoreController = controller;
  251. IExtensionProvider = extensions;
  252. }
  253. public void OnInitializeFailed(InitializationFailureReason error)
  254. {
  255. Inited = false;
  256. }
  257. public void OnPurchaseFailed(Product i, PurchaseFailureReason p)
  258. {
  259. Bubble.Show(null, Language.GetStr(LanguageLabel.IAP__PurchaseFail));
  260. }
  261. public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e)
  262. {
  263. Bubble.Show(null, Language.GetStr(LanguageLabel.IAP__PurchaseSuccess));
  264. string packName = PackNameDictionary[e.purchasedProduct.definition.id];
  265. BuyProductCallbackDictionary[packName].Invoke();
  266. return PurchaseProcessingResult.Complete;
  267. }
  268. public static void Share(string imagePath = null)
  269. {
  270. ShareContent shareContent = new ShareContent();
  271. shareContent.SetText(Language.GetStr(LanguageLabel.Common__ShareContent));
  272. shareContent.SetTitle(Language.GetStr(LanguageLabel.Common__ShareTit));
  273. shareContent.SetTitleUrl(Language.GetShareUrl());
  274. shareContent.SetSite(Language.GetShareUrl());
  275. shareContent.SetSiteUrl(Language.GetShareUrl());
  276. shareContent.SetUrl(Language.GetShareUrl());
  277. shareContent.SetShareType(ContentType.App);
  278. if (string.IsNullOrEmpty(imagePath))
  279. {
  280. shareContent.SetImageUrl(ImageUrl);
  281. }
  282. else
  283. {
  284. ImagePath = imagePath;
  285. shareContent.SetImagePath(imagePath);
  286. }
  287. ShareSdk.ShowPlatformList(null, shareContent, 100, 100);
  288. }
  289. public static void Authorize()
  290. {
  291. ShareSdk.Authorize(PlatformType.QQ);
  292. }
  293. public static void ShareCallback(int reqID, ResponseState state, PlatformType type, Hashtable result)
  294. {
  295. if (!string.IsNullOrEmpty(ImagePath))
  296. {
  297. ResourceManager.SetActive(CanvasLabel.P_Open, true);
  298. ResourceManager.SetActive(CanvasLabel.P_Share, true);
  299. ImagePath = null;
  300. }
  301. if (state == ResponseState.Success)
  302. {
  303. Manager.ShareAmt++;
  304. Bubble.Show(null, Language.GetStr(LanguageLabel.Common__ShareSuccess));
  305. }
  306. else if (state == ResponseState.Fail)
  307. {
  308. Bubble.Show(null, Language.GetStr(LanguageLabel.Common__ShareFail));
  309. }
  310. else if (state == ResponseState.Cancel)
  311. {
  312. Bubble.Show(null, Language.GetStr(LanguageLabel.Common__ShareFail));
  313. }
  314. }
  315. public static void AuthorizeCallback(int reqID, ResponseState state, PlatformType type, Hashtable result)
  316. {
  317. if (state == ResponseState.Success)
  318. {
  319. print("Success");
  320. }
  321. else if (state == ResponseState.Fail)
  322. {
  323. print("Fail");
  324. }
  325. else if (state == ResponseState.Cancel)
  326. {
  327. print("Cancel");
  328. }
  329. }
  330. }