IAPManager.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. public void Update()
  49. {
  50. if (ChestList.Count > 0)
  51. return;
  52. Timer += Time.deltaTime;
  53. if (Timer >= 10)
  54. {
  55. Timer = 0;
  56. if (!Advertisement.IsReady())
  57. {
  58. InitializeAD();
  59. }
  60. else
  61. {
  62. if (ADPlayTime == null)
  63. return;
  64. if (HttpManager.Connect && (HttpManager.Time.Subtract((DateTime)ADPlayTime).TotalSeconds > 1200))
  65. {
  66. if (GardenManager.MiniLock && !VisitManager.InVisit && !TutorialManager.NewplayerTutorial && !SFSManager.PlazaRoomController.InPlazaRoom && !Player.InDressRoom)
  67. {
  68. ChestList.Add(ResourceManager.GetADChest());
  69. }
  70. }
  71. }
  72. }
  73. }
  74. public static void RetrieveADChest()
  75. {
  76. if (ChestList.Count > 0)
  77. {
  78. ResourceManager.Save(ChestList[0].transform.parent);
  79. ChestList.RemoveAt(0);
  80. }
  81. }
  82. public static void Initialize()
  83. {
  84. //InitializeAD();
  85. //GameObject.Find(PrefabLabel.I_Lab).GetComponent<Text>().text = "Error Code : AD";
  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.GetPlayerString("ADPlayTime"));
  90. GameObject.Find(ObjectLabel.U_DebugLab).GetComponent<Text>().text = "Error Code : I3";
  91. //InitializeShare();
  92. //GameObject.Find(PrefabLabel.I_Lab).GetComponent<Text>().text = "Error Code : Share";
  93. }
  94. public static void InitializeAD()
  95. {
  96. if (Application.platform == RuntimePlatform.IPhonePlayer)
  97. {
  98. Advertisement.Initialize("1408492", false);
  99. }
  100. else if (Application.isMobilePlatform)
  101. {
  102. Advertisement.Initialize("1408493", false);
  103. }
  104. else if (Application.isEditor)
  105. {
  106. Advertisement.Initialize("1408493", false);
  107. }
  108. }
  109. public static void InitializeShare()
  110. {
  111. if (Application.isMobilePlatform || Application.platform == RuntimePlatform.IPhonePlayer)
  112. {
  113. ShareSdk = Instance.gameObject.AddComponent<ShareSDK>();
  114. ShareSdk.Initialize();
  115. ShareSdk.shareHandler = ShareCallback;
  116. }
  117. }
  118. public static void InitializeIAP()
  119. {
  120. if (Application.isEditor)
  121. {
  122. return;
  123. }
  124. PayChannel = ResourceManager.Load<TextAsset>("Setting", Folder.Config).text.ToEnum<PayChannel>();
  125. if (Application.platform == RuntimePlatform.IPhonePlayer)
  126. {
  127. if (UseAlipayOnIOS)
  128. {
  129. InitializeAlipay();
  130. }
  131. else
  132. {
  133. InitializeUnityIAP();
  134. }
  135. }
  136. else
  137. {
  138. if (PayChannel == PayChannel.AliPay)
  139. {
  140. InitializeAlipay();
  141. }
  142. else if (PayChannel == PayChannel.UnityIAP)
  143. {
  144. InitializeUnityIAP();
  145. }
  146. }
  147. }
  148. public static void InitializeAlipay()
  149. {
  150. foreach (var attribute in ConfigManager.GetIAPConfig())
  151. {
  152. ProductList.Add(attribute[1].Value);
  153. ProductIdDic.Add(attribute[5].Value, attribute[1].Value);
  154. PackNameDic.Add(attribute[1].Value, attribute[5].Value);
  155. }
  156. AliplayManager.Instance.Init();
  157. }
  158. public static void InitializeUnityIAP()
  159. {
  160. ConfigurationBuilder cb = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
  161. foreach (var attribute in ConfigManager.GetIAPConfig())
  162. {
  163. cb.AddProduct(attribute[1].Value, ProductType.Consumable);
  164. ProductList.Add(attribute[1].Value);
  165. ProductIdDic.Add(attribute[5].Value, attribute[1].Value);
  166. PackNameDic.Add(attribute[1].Value, attribute[5].Value);
  167. }
  168. UnityPurchasing.Initialize(Instance, cb);
  169. }
  170. public static void PlayAD(UnityAction action)
  171. {
  172. AdAction = () =>
  173. {
  174. action.Invoke();
  175. StaticsManager.GetInstance().AdFinished();
  176. };
  177. if (Advertisement.IsReady())
  178. {
  179. Manager.ReactiveLock = true;
  180. ShowOptions showOptions = new ShowOptions();
  181. showOptions.resultCallback = CallbackAD;
  182. Advertisement.Show(showOptions);
  183. StaticsManager.GetInstance().AdChannel(0);
  184. }
  185. else
  186. {
  187. Bubble.Show(null, Language.GetStr(LanguageLabel.IAP__LoadAd));
  188. }
  189. }
  190. public static void CallbackAD(ShowResult showResult)
  191. {
  192. if (showResult == ShowResult.Finished)
  193. {
  194. AdAction.Invoke();
  195. Manager.AdAmt++;
  196. ADPlayTime = HttpManager.Time;
  197. ConfigManager.SavePlayerString(PlayerConfigLabel.ADPlayTime, ADPlayTime.ToString());
  198. }
  199. else if (showResult == ShowResult.Skipped)
  200. {
  201. Bubble.Show(null, Language.GetStr(LanguageLabel.IAP__IncompleteAd));
  202. }
  203. else if (showResult == ShowResult.Failed)
  204. {
  205. Bubble.Show(null, Language.GetStr(LanguageLabel.IAP__FailAd));
  206. }
  207. }
  208. public static void Purchase(string str)
  209. {
  210. //bool freeze = true;
  211. //if (freeze)
  212. //{
  213. // Bubble.Show(null, Language.GetStr(LanguageLabel.IAP__TemporarilyUnavailible));
  214. // return;
  215. //}
  216. if (!Complete)
  217. {
  218. Bubble.Show(null, Language.GetStr(LanguageLabel.IAP__PurchaseFail));
  219. InitializeIAP();
  220. return;
  221. }
  222. if (!ProductList.Contains(ProductIdDic[str]))
  223. {
  224. Bubble.Show(null, Language.GetStr(LanguageLabel.IAP__PurchaseUnvalid));
  225. return;
  226. }
  227. HttpManager.GetThanksGiftInfo
  228. (
  229. jData =>
  230. {
  231. ThanksGift.Init(jData);
  232. PurchaseCore(str);
  233. },
  234. () => { Bubble.Show(null, Language.GetStr(LanguageLabel.UI__GetThanksGiftInfoFailed)); }
  235. );
  236. }
  237. private static void PurchaseCore(string str)
  238. {
  239. if (PayChannel == PayChannel.AliPay)
  240. {
  241. HttpManager.GetProductID
  242. (
  243. ProductIdDic[str],
  244. data =>
  245. {
  246. AliplayManager.Instance.Pay(data, str);
  247. }
  248. );
  249. }
  250. else if (PayChannel == PayChannel.UnityIAP)
  251. {
  252. IStoreController.InitiatePurchase(ProductIdDic[str]);
  253. }
  254. }
  255. public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
  256. {
  257. Complete = true;
  258. IStoreController = controller;
  259. IExtensionProvider = extensions;
  260. //ManaDebug.Log("初始化IAP成功");
  261. }
  262. public void OnInitializeFailed(InitializationFailureReason error)
  263. {
  264. Complete = false;
  265. //Debug.Log("初始化IAP失败");
  266. //ManaDebug.Log("初始化IAP失败");
  267. }
  268. public void OnPurchaseFailed(Product i, PurchaseFailureReason p)
  269. {
  270. Bubble.Show(null, Language.GetStr(LanguageLabel.IAP__PurchaseFail));
  271. }
  272. public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e)
  273. {
  274. Bubble.Show(null, Language.GetStr(LanguageLabel.IAP__PurchaseSuccess));
  275. string packName = PackNameDic[e.purchasedProduct.definition.id];
  276. ProductActionDic[packName].Invoke();
  277. return PurchaseProcessingResult.Complete;
  278. }
  279. public static void Share(string imagePath = null)
  280. {
  281. ShareContent shareContent = new ShareContent();
  282. shareContent.SetText(Language.GetStr(LanguageLabel.Common__ShareContent));
  283. shareContent.SetTitle(Language.GetStr(LanguageLabel.Common__ShareTit));
  284. shareContent.SetTitleUrl(Language.GetShareUrl());
  285. shareContent.SetSite(Language.GetShareUrl());
  286. shareContent.SetSiteUrl(Language.GetShareUrl());
  287. shareContent.SetUrl(Language.GetShareUrl());
  288. shareContent.SetShareType(ContentType.App);
  289. if (string.IsNullOrEmpty(imagePath))
  290. {
  291. shareContent.SetImageUrl(ImageUrl);
  292. }
  293. else
  294. {
  295. ImagePath = imagePath;
  296. shareContent.SetImagePath(imagePath);
  297. }
  298. ShareSdk.ShowPlatformList(null, shareContent, 100, 100);
  299. }
  300. public static void Authorize()
  301. {
  302. ShareSdk.Authorize(PlatformType.QQ);
  303. }
  304. public static void ShareCallback(int reqID, ResponseState state, PlatformType type, Hashtable result)
  305. {
  306. if (!string.IsNullOrEmpty(ImagePath))
  307. {
  308. ResourceManager.SetActive(ObjectLabel.P_Open, true);
  309. ResourceManager.SetActive(ObjectLabel.P_Share, true);
  310. ImagePath = null;
  311. }
  312. if (state == ResponseState.Success)
  313. {
  314. Manager.ShareAmt++;
  315. Bubble.Show(null, Language.GetStr(LanguageLabel.Common__ShareSuccess));
  316. }
  317. else if (state == ResponseState.Fail)
  318. {
  319. Bubble.Show(null, Language.GetStr(LanguageLabel.Common__ShareFail));
  320. }
  321. else if (state == ResponseState.Cancel)
  322. {
  323. Bubble.Show(null, Language.GetStr(LanguageLabel.Common__ShareFail));
  324. }
  325. }
  326. public static void AuthorizeCallback(int reqID, ResponseState state, PlatformType type, Hashtable result)
  327. {
  328. if (state == ResponseState.Success)
  329. {
  330. print("Success");
  331. }
  332. else if (state == ResponseState.Fail)
  333. {
  334. print("Fail");
  335. }
  336. else if (state == ResponseState.Cancel)
  337. {
  338. print("Cancel");
  339. }
  340. }
  341. }