IAPManager.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. DebugManager.Log("0");
  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. Purchase(str);
  216. }
  217. else
  218. {
  219. HttpManager.GetThanksGiftInfo
  220. (
  221. jData =>
  222. {
  223. RechargeGiftManager.Init(jData);
  224. Purchase(str);
  225. },
  226. () => { Bubble.Show(null, Language.GetStr(LanguageLabel.UI__GetThanksGiftInfoFailed)); }
  227. );
  228. }
  229. }
  230. private static void Purchase(string str)
  231. {
  232. if (PayChannel == PayChannel.AliPay)
  233. {
  234. HttpManager.GetProductID
  235. (
  236. ProductIdDictionary[str],
  237. data =>
  238. {
  239. AliplayManager.Instance.Pay(data, str);
  240. }
  241. );
  242. }
  243. else if (PayChannel == PayChannel.UnityIAP)
  244. {
  245. IStoreController.InitiatePurchase(ProductIdDictionary[str]);
  246. }
  247. }
  248. public void OnInitialized(IStoreController controller, IExtensionProvider extensions) //
  249. {
  250. Inited = true;
  251. IStoreController = controller;
  252. IExtensionProvider = extensions;
  253. }
  254. public void OnInitializeFailed(InitializationFailureReason error)
  255. {
  256. Inited = false;
  257. }
  258. public void OnPurchaseFailed(Product i, PurchaseFailureReason p)
  259. {
  260. Bubble.Show(null, Language.GetStr(LanguageLabel.IAP__PurchaseFail));
  261. }
  262. public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e)
  263. {
  264. Bubble.Show(null, Language.GetStr(LanguageLabel.IAP__PurchaseSuccess));
  265. string packName = PackNameDictionary[e.purchasedProduct.definition.id];
  266. BuyProductCallbackDictionary[packName].Invoke();
  267. return PurchaseProcessingResult.Complete;
  268. }
  269. public static void Share(string imagePath = null, string content = null)
  270. {
  271. try
  272. {
  273. ShareContent shareContent = new ShareContent();
  274. if (content == null)
  275. {
  276. shareContent.SetText(Language.GetStr(LanguageLabel.Common__ShareContent));
  277. }
  278. else
  279. {
  280. shareContent.SetText(content);
  281. }
  282. shareContent.SetTitle(Language.GetStr(LanguageLabel.Common__ShareTit));
  283. shareContent.SetTitleUrl(Language.GetShareUrl());
  284. shareContent.SetSite(Language.GetShareUrl());
  285. shareContent.SetSiteUrl(Language.GetShareUrl());
  286. shareContent.SetUrl(Language.GetShareUrl());
  287. shareContent.SetShareType(ContentType.App);
  288. if (string.IsNullOrEmpty(imagePath))
  289. {
  290. shareContent.SetImageUrl(ImageUrl);
  291. }
  292. else
  293. {
  294. ImagePath = imagePath;
  295. shareContent.SetImagePath(imagePath);
  296. }
  297. ShareSdk.ShowPlatformList(null, shareContent, 100, 100);
  298. }
  299. catch (Exception e)
  300. {
  301. DebugManager.Log(e.ToString());
  302. }
  303. }
  304. public static void Authorize()
  305. {
  306. ShareSdk.Authorize(PlatformType.QQ);
  307. }
  308. public static void ShareCallback(int reqID, ResponseState state, PlatformType type, Hashtable result)
  309. {
  310. if (!string.IsNullOrEmpty(ImagePath))
  311. {
  312. ResourceManager.SetActive(CanvasLabel.P_Open, true);
  313. ResourceManager.SetActive(CanvasLabel.P_Share, true);
  314. ImagePath = null;
  315. }
  316. if (state == ResponseState.Success)
  317. {
  318. Manager.ShareAmt++;
  319. Bubble.Show(null, Language.GetStr(LanguageLabel.Common__ShareSuccess));
  320. }
  321. else if (state == ResponseState.Fail)
  322. {
  323. Bubble.Show(null, Language.GetStr(LanguageLabel.Common__ShareFail));
  324. }
  325. else if (state == ResponseState.Cancel)
  326. {
  327. Bubble.Show(null, Language.GetStr(LanguageLabel.Common__ShareFail));
  328. }
  329. }
  330. public static void AuthorizeCallback(int reqID, ResponseState state, PlatformType type, Hashtable result)
  331. {
  332. if (state == ResponseState.Success)
  333. {
  334. print("Success");
  335. }
  336. else if (state == ResponseState.Fail)
  337. {
  338. print("Fail");
  339. }
  340. else if (state == ResponseState.Cancel)
  341. {
  342. print("Cancel");
  343. }
  344. }
  345. }