ManaIAP.cs 11 KB

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