IAPManager.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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 Timer;
  23. public static DateTime? ADPlayTime = null;
  24. public static List<Chest> ChestList = new List<Chest>();
  25. public static bool UseAlipayOnIOS = false;
  26. public static bool Complete;
  27. public static IAPManager Instance;
  28. public static PayChannel PayChannel;
  29. public static UnityAction AdAction;
  30. public static IStoreController IStoreController;
  31. public static IExtensionProvider IExtensionProvider;
  32. public static List<string> ProductList = new List<string>();
  33. public static Dictionary<string, string> PackNameDic = new Dictionary<string, string>();
  34. public static Dictionary<string, string> ProductIdDic = new Dictionary<string, string>();
  35. public static Dictionary<string, UnityAction> ProductActionDic = new Dictionary<string, UnityAction>();
  36. #region Share配置
  37. public static string ImageUrl = "http://or5zgoeui.bkt.clouddn.com/game_icon.png";
  38. public static string ImagePath;
  39. public static ShareSDK ShareSdk;
  40. #endregion
  41. #endregion
  42. public void Awake()
  43. {
  44. Instance = this;
  45. InitializeAD();
  46. InitializeShare();
  47. }
  48. private float ReloadADsTime = 10f;
  49. private float ReloadADChestTime = 1200f;
  50. public void Update()
  51. {
  52. if (ChestList.Count > 0)
  53. return;
  54. Timer += Time.deltaTime;
  55. if (Timer >= ReloadADsTime)
  56. {
  57. Timer = 0;
  58. if (!Advertisement.IsReady())
  59. {
  60. InitializeAD();
  61. }
  62. else
  63. {
  64. if (ADPlayTime == null)
  65. return;
  66. if (HttpManager.Connect && (HttpManager.Time.Subtract((DateTime)ADPlayTime).TotalSeconds > ReloadADChestTime))
  67. {
  68. if (GardenManager.MiniLock && !VisitManager.InVisit && !TutorialManager.NewplayerTutorial && !SFSManager.PlazaRoomController.InPlazaRoom && !Player.InDressRoom)
  69. {
  70. ChestList.Add(ResourceManager.GetADChest());
  71. }
  72. }
  73. }
  74. }
  75. }
  76. public static void RetrieveADChest()
  77. {
  78. if (ChestList.Count > 0)
  79. {
  80. ResourceManager.Save(ChestList[0].transform.parent);
  81. ChestList.RemoveAt(0);
  82. }
  83. }
  84. public static void Initialize()
  85. {
  86. GameObject.Find(ObjectLabel.U_DebugLab).GetComponent<Text>().text = "Error Code : I1";
  87. InitializeIAP();
  88. GameObject.Find(ObjectLabel.U_DebugLab).GetComponent<Text>().text = "Error Code : I2";
  89. ADPlayTime = DateTime.Parse(ConfigManager.GetStringFormConfig(PlayerConfigLabel.ADPlayTime));
  90. GameObject.Find(ObjectLabel.U_DebugLab).GetComponent<Text>().text = "Error Code : I3";
  91. }
  92. public static void InitializeAD()
  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 InitializeShare()
  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 InitializeIAP()
  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. InitializeAlipay();
  128. }
  129. else
  130. {
  131. InitializeUnityIAP();
  132. }
  133. }
  134. else
  135. {
  136. if (PayChannel == PayChannel.AliPay)
  137. {
  138. InitializeAlipay();
  139. }
  140. else if (PayChannel == PayChannel.UnityIAP)
  141. {
  142. InitializeUnityIAP();
  143. }
  144. }
  145. }
  146. public static void InitializeAlipay()
  147. {
  148. foreach (var attribute in ConfigManager.GetIAPConfig())
  149. {
  150. ProductList.Add(attribute[1].Value);
  151. ProductIdDic.Add(attribute[5].Value, attribute[1].Value);
  152. PackNameDic.Add(attribute[1].Value, attribute[5].Value);
  153. }
  154. AliplayManager.Instance.Init();
  155. }
  156. public static void InitializeUnityIAP()
  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. ProductIdDic.Add(attribute[5].Value, attribute[1].Value);
  164. PackNameDic.Add(attribute[1].Value, attribute[5].Value);
  165. }
  166. UnityPurchasing.Initialize(Instance, cb);
  167. }
  168. public static void PlayAD(UnityAction action)
  169. {
  170. AdAction = () =>
  171. {
  172. action.Invoke();
  173. StaticsManager.GetInstance().AdFinished();
  174. };
  175. if (Advertisement.IsReady())
  176. {
  177. Manager.ReactiveLock = true;
  178. ShowOptions showOptions = new ShowOptions();
  179. showOptions.resultCallback = CallbackAD;
  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 CallbackAD(ShowResult showResult)
  189. {
  190. if (showResult == ShowResult.Finished)
  191. {
  192. AdAction.Invoke();
  193. Manager.AdAmt++;
  194. ADPlayTime = HttpManager.Time;
  195. ConfigManager.SaveStringToConfig(PlayerConfigLabel.ADPlayTime, ADPlayTime.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 Purchase(string str)
  207. {
  208. if (!Complete)
  209. {
  210. Bubble.Show(null, Language.GetStr(LanguageLabel.IAP__PurchaseFail));
  211. InitializeIAP();
  212. return;
  213. }
  214. if (!ProductList.Contains(ProductIdDic[str]))
  215. {
  216. Bubble.Show(null, Language.GetStr(LanguageLabel.IAP__PurchaseUnvalid));
  217. return;
  218. }
  219. HttpManager.GetThanksGiftInfo
  220. (
  221. jData =>
  222. {
  223. ThanksGift.Init(jData);
  224. PurchaseCore(str);
  225. },
  226. () => { Bubble.Show(null, Language.GetStr(LanguageLabel.UI__GetThanksGiftInfoFailed)); }
  227. );
  228. }
  229. private static void PurchaseCore(string str)
  230. {
  231. if (PayChannel == PayChannel.AliPay)
  232. {
  233. HttpManager.GetProductID
  234. (
  235. ProductIdDic[str],
  236. data =>
  237. {
  238. AliplayManager.Instance.Pay(data, str);
  239. }
  240. );
  241. }
  242. else if (PayChannel == PayChannel.UnityIAP)
  243. {
  244. IStoreController.InitiatePurchase(ProductIdDic[str]);
  245. }
  246. }
  247. public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
  248. {
  249. Complete = true;
  250. IStoreController = controller;
  251. IExtensionProvider = extensions;
  252. }
  253. public void OnInitializeFailed(InitializationFailureReason error)
  254. {
  255. Complete = 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 = PackNameDic[e.purchasedProduct.definition.id];
  265. ProductActionDic[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(ObjectLabel.P_Open, true);
  298. ResourceManager.SetActive(ObjectLabel.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. }