ManaIAP.cs 10 KB

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