ManaIAP.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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.Collections.Generic;
  12. public enum PayChannel
  13. {
  14. AliPay,
  15. UnityIAP,
  16. }
  17. public class ManaIAP : MonoBehaviour, IStoreListener
  18. {
  19. #region 变量
  20. public static float Timer;
  21. public static DateTime? ADPlayTime = null;
  22. public static List<Chest> ChestList = new List<Chest>();
  23. public static bool UseAlipayOnIOS = false;
  24. public static bool Complete;
  25. public static ManaIAP Instance;
  26. public static PayChannel PayChannel;
  27. public static UnityAction AdAction;
  28. public static IStoreController IStoreController;
  29. public static IExtensionProvider IExtensionProvider;
  30. public static List<string> ProductList = new List<string>();
  31. public static Dictionary<string, string> PackNameDic = new Dictionary<string, string>();
  32. public static Dictionary<string, string> ProductIdDic = new Dictionary<string, string>();
  33. public static Dictionary<string, UnityAction> ProductActionDic = new Dictionary<string, UnityAction>();
  34. #region Share配置
  35. public static string ImageUrl = "http://or5zgoeui.bkt.clouddn.com/game_icon.png";
  36. public static string ImagePath;
  37. public static ShareSDK ShareSdk;
  38. #endregion
  39. #endregion
  40. public void Awake()
  41. {
  42. Instance = this;
  43. InitializeAD();
  44. InitializeShare();
  45. }
  46. public void Update()
  47. {
  48. if (ChestList.Count > 0)
  49. return;
  50. if (!Advertisement.IsReady())
  51. {
  52. Timer += Time.deltaTime;
  53. if (Timer >= 10)
  54. {
  55. Timer = 0;
  56. InitializeAD();
  57. }
  58. }
  59. else
  60. {
  61. if (ADPlayTime == null)
  62. return;
  63. if (ManaServer.Connect && (ManaServer.Time.Subtract((DateTime)ADPlayTime).TotalSeconds > 1200))
  64. {
  65. if (ManaGarden.MiniLock && !ManaVisit.InVisit && !ManaTutorial.TutorialA)
  66. {
  67. ChestList.Add(ManaReso.GetChest());
  68. }
  69. }
  70. }
  71. }
  72. public static void Initialize()
  73. {
  74. //InitializeAD();
  75. //GameObject.Find("I_Lab").GetComponent<Text>().text = "Error Code : AD";
  76. GameObject.Find("I_Lab").GetComponent<Text>().text = "Error Code : IAP1";
  77. InitializeIAP();
  78. GameObject.Find("I_Lab").GetComponent<Text>().text = "Error Code : IAP2";
  79. ADPlayTime = DateTime.Parse(ManaData.GetPlayerString("ADPlayTime"));
  80. //InitializeShare();
  81. //GameObject.Find("I_Lab").GetComponent<Text>().text = "Error Code : Share";
  82. }
  83. public static void InitializeAD()
  84. {
  85. if (Application.platform == RuntimePlatform.IPhonePlayer)
  86. {
  87. Advertisement.Initialize("1408492", false);
  88. }
  89. else if (Application.isMobilePlatform)
  90. {
  91. Advertisement.Initialize("1408493", false);
  92. }
  93. else if (Application.isEditor)
  94. {
  95. Advertisement.Initialize("1408493", false);
  96. }
  97. }
  98. public static void InitializeShare()
  99. {
  100. if (Application.isMobilePlatform || Application.platform == RuntimePlatform.IPhonePlayer)
  101. {
  102. ShareSdk = Instance.gameObject.AddComponent<ShareSDK>();
  103. ShareSdk.Initialize();
  104. ShareSdk.shareHandler = ShareCallback;
  105. }
  106. }
  107. public static void InitializeIAP()
  108. {
  109. if (Application.isEditor)
  110. {
  111. return;
  112. }
  113. PayChannel = ManaReso.Load<TextAsset>("Setting", Folder.Config).text.ToEnum<PayChannel>();
  114. if (Application.platform == RuntimePlatform.IPhonePlayer)
  115. {
  116. if (UseAlipayOnIOS)
  117. {
  118. InitializeAlipay();
  119. }
  120. else
  121. {
  122. InitializeUnityIAP();
  123. }
  124. }
  125. else
  126. {
  127. if (PayChannel == PayChannel.AliPay)
  128. {
  129. InitializeAlipay();
  130. }
  131. else if (PayChannel == PayChannel.UnityIAP)
  132. {
  133. InitializeUnityIAP();
  134. }
  135. }
  136. }
  137. public static void InitializeAlipay()
  138. {
  139. foreach (var attribute in ManaData.GetIAPConfig())
  140. {
  141. ProductList.Add(attribute[1].Value);
  142. ProductIdDic.Add(attribute[5].Value, attribute[1].Value);
  143. PackNameDic.Add(attribute[1].Value, attribute[5].Value);
  144. }
  145. AliplayManager.Instance.Init();
  146. }
  147. public static void InitializeUnityIAP()
  148. {
  149. ConfigurationBuilder cb = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
  150. foreach (var attribute in ManaData.GetIAPConfig())
  151. {
  152. cb.AddProduct(attribute[1].Value, ProductType.Consumable);
  153. ProductList.Add(attribute[1].Value);
  154. ProductIdDic.Add(attribute[5].Value, attribute[1].Value);
  155. PackNameDic.Add(attribute[1].Value, attribute[5].Value);
  156. }
  157. UnityPurchasing.Initialize(Instance, cb);
  158. }
  159. public static void PlayAD(UnityAction action)
  160. {
  161. AdAction = () =>
  162. {
  163. action.Invoke();
  164. StaticsManager.GetInstance().AdFinished();
  165. };
  166. if (Advertisement.IsReady())
  167. {
  168. ManaCenter.ReactiveLock = true;
  169. ShowOptions showOptions = new ShowOptions();
  170. showOptions.resultCallback = CallbackAD;
  171. Advertisement.Show(showOptions);
  172. StaticsManager.GetInstance().AdChannel(0);
  173. }
  174. else
  175. {
  176. Bubble.Show(null, Language.GetStr("IAP", "LoadAd"));
  177. }
  178. }
  179. public static void CallbackAD(ShowResult showResult)
  180. {
  181. if (showResult == ShowResult.Finished)
  182. {
  183. AdAction.Invoke();
  184. ManaCenter.AdAmt++;
  185. ADPlayTime = ManaServer.Time;
  186. ManaData.SavePlayerString("ADPlayTime", ADPlayTime.ToString());
  187. }
  188. else if (showResult == ShowResult.Skipped)
  189. {
  190. Bubble.Show(null, Language.GetStr("IAP", "IncompleteAd"));
  191. }
  192. else if (showResult == ShowResult.Failed)
  193. {
  194. Bubble.Show(null, Language.GetStr("IAP", "FailAd"));
  195. }
  196. }
  197. public static void Purchase(string str)
  198. {
  199. //bool freeze = true;
  200. //if (freeze)
  201. //{
  202. // Bubble.Show(null, Language.GetStr("IAP", "TemporarilyUnavailible"));
  203. // return;
  204. //}
  205. if (!Complete)
  206. {
  207. Bubble.Show(null, Language.GetStr("IAP", "PurchaseFail"));
  208. InitializeIAP();
  209. return;
  210. }
  211. if (!ProductList.Contains(ProductIdDic[str]))
  212. {
  213. Bubble.Show(null, Language.GetStr("IAP", "PurchaseUnvalid"));
  214. return;
  215. }
  216. if (PayChannel == PayChannel.AliPay)
  217. {
  218. ManaServer.GetProductID
  219. (
  220. ProductIdDic[str],
  221. data =>
  222. {
  223. AliplayManager.Instance.Pay(data, str);
  224. }
  225. );
  226. }
  227. else if (PayChannel == PayChannel.UnityIAP)
  228. {
  229. IStoreController.InitiatePurchase(ProductIdDic[str]);
  230. }
  231. }
  232. public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
  233. {
  234. Complete = true;
  235. IStoreController = controller;
  236. IExtensionProvider = extensions;
  237. ManaDebug.Log("初始化IAP成功");
  238. }
  239. public void OnInitializeFailed(InitializationFailureReason error)
  240. {
  241. Complete = false;
  242. Debug.Log("初始化IAP失败");
  243. ManaDebug.Log("初始化IAP失败");
  244. }
  245. public void OnPurchaseFailed(Product i, PurchaseFailureReason p)
  246. {
  247. Bubble.Show(null, Language.GetStr("IAP", "PurchaseFail"));
  248. }
  249. public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e)
  250. {
  251. Bubble.Show(null, Language.GetStr("IAP", "PurchaseSuccess"));
  252. string packName = PackNameDic[e.purchasedProduct.definition.id];
  253. ProductActionDic[packName].Invoke();
  254. return PurchaseProcessingResult.Complete;
  255. }
  256. public static void Share(string imagePath = null)
  257. {
  258. ShareContent shareContent = new ShareContent();
  259. shareContent.SetText(Language.GetStr("Common", "ShareContent"));
  260. shareContent.SetTitle(Language.GetStr("Common", "ShareTit"));
  261. shareContent.SetTitleUrl(Language.GetShareUrl());
  262. shareContent.SetSite(Language.GetShareUrl());
  263. shareContent.SetSiteUrl(Language.GetShareUrl());
  264. shareContent.SetUrl(Language.GetShareUrl());
  265. shareContent.SetShareType(ContentType.App);
  266. if (string.IsNullOrEmpty(imagePath))
  267. {
  268. shareContent.SetImageUrl(ImageUrl);
  269. }
  270. else
  271. {
  272. ImagePath = imagePath;
  273. shareContent.SetImagePath(imagePath);
  274. }
  275. ShareSdk.ShowPlatformList(null, shareContent, 100, 100);
  276. }
  277. public static void Authorize()
  278. {
  279. ShareSdk.Authorize(PlatformType.QQ);
  280. }
  281. public static void ShareCallback(int reqID, ResponseState state, PlatformType type, Hashtable result)
  282. {
  283. if (!string.IsNullOrEmpty(ImagePath))
  284. {
  285. ManaReso.SetActive("P_Open", true);
  286. ManaReso.SetActive("P_Share", true);
  287. ImagePath = null;
  288. }
  289. if (state == ResponseState.Success)
  290. {
  291. ManaCenter.ShareAmt++;
  292. Bubble.Show(null, Language.GetStr("Common", "ShareSuccess"));
  293. }
  294. else if (state == ResponseState.Fail)
  295. {
  296. Bubble.Show(null, Language.GetStr("Common", "ShareFail"));
  297. }
  298. else if (state == ResponseState.Cancel)
  299. {
  300. Bubble.Show(null, Language.GetStr("Common", "ShareFail"));
  301. }
  302. }
  303. public static void AuthorizeCallback(int reqID, ResponseState state, PlatformType type, Hashtable result)
  304. {
  305. if (state == ResponseState.Success)
  306. {
  307. print("Success");
  308. }
  309. else if (state == ResponseState.Fail)
  310. {
  311. print("Fail");
  312. }
  313. else if (state == ResponseState.Cancel)
  314. {
  315. print("Cancel");
  316. }
  317. }
  318. }