ManaIAP.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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 && !SFSManager.GardenSmartFox.PlazaRoomManager.InPlazaRoom && !Player.InDressRoom)
  67. {
  68. ChestList.Add(ManaReso.GetADChest());
  69. }
  70. }
  71. }
  72. }
  73. }
  74. public static void RetrieveADChest()
  75. {
  76. if (ChestList.Count > 0)
  77. {
  78. ManaReso.Save(ChestList[0].transform.parent);
  79. ChestList.RemoveAt(0);
  80. }
  81. }
  82. public static void Initialize()
  83. {
  84. //InitializeAD();
  85. //GameObject.Find("I_Lab").GetComponent<Text>().text = "Error Code : AD";
  86. GameObject.Find("U_DebugLab").GetComponent<Text>().text = "Error Code : I1";
  87. InitializeIAP();
  88. GameObject.Find("U_DebugLab").GetComponent<Text>().text = "Error Code : I2";
  89. ADPlayTime = DateTime.Parse(ManaData.GetPlayerString("ADPlayTime"));
  90. GameObject.Find("U_DebugLab").GetComponent<Text>().text = "Error Code : I3";
  91. //InitializeShare();
  92. //GameObject.Find("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 = ManaReso.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 ManaData.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 ManaData.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. ManaCenter.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("IAP", "LoadAd"));
  188. }
  189. }
  190. public static void CallbackAD(ShowResult showResult)
  191. {
  192. if (showResult == ShowResult.Finished)
  193. {
  194. AdAction.Invoke();
  195. ManaCenter.AdAmt++;
  196. ADPlayTime = ManaServer.Time;
  197. ManaData.SavePlayerString("ADPlayTime", ADPlayTime.ToString());
  198. }
  199. else if (showResult == ShowResult.Skipped)
  200. {
  201. Bubble.Show(null, Language.GetStr("IAP", "IncompleteAd"));
  202. }
  203. else if (showResult == ShowResult.Failed)
  204. {
  205. Bubble.Show(null, Language.GetStr("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("IAP", "TemporarilyUnavailible"));
  214. // return;
  215. //}
  216. if (!Complete)
  217. {
  218. Bubble.Show(null, Language.GetStr("IAP", "PurchaseFail"));
  219. InitializeIAP();
  220. return;
  221. }
  222. if (!ProductList.Contains(ProductIdDic[str]))
  223. {
  224. Bubble.Show(null, Language.GetStr("IAP", "PurchaseUnvalid"));
  225. return;
  226. }
  227. if (PayChannel == PayChannel.AliPay)
  228. {
  229. ManaServer.GetProductID
  230. (
  231. ProductIdDic[str],
  232. data =>
  233. {
  234. AliplayManager.Instance.Pay(data, str);
  235. }
  236. );
  237. }
  238. else if (PayChannel == PayChannel.UnityIAP)
  239. {
  240. IStoreController.InitiatePurchase(ProductIdDic[str]);
  241. }
  242. }
  243. public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
  244. {
  245. Complete = true;
  246. IStoreController = controller;
  247. IExtensionProvider = extensions;
  248. //ManaDebug.Log("初始化IAP成功");
  249. }
  250. public void OnInitializeFailed(InitializationFailureReason error)
  251. {
  252. Complete = false;
  253. //Debug.Log("初始化IAP失败");
  254. //ManaDebug.Log("初始化IAP失败");
  255. }
  256. public void OnPurchaseFailed(Product i, PurchaseFailureReason p)
  257. {
  258. Bubble.Show(null, Language.GetStr("IAP", "PurchaseFail"));
  259. }
  260. public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e)
  261. {
  262. Bubble.Show(null, Language.GetStr("IAP", "PurchaseSuccess"));
  263. string packName = PackNameDic[e.purchasedProduct.definition.id];
  264. ProductActionDic[packName].Invoke();
  265. return PurchaseProcessingResult.Complete;
  266. }
  267. public static void Share(string imagePath = null)
  268. {
  269. ShareContent shareContent = new ShareContent();
  270. shareContent.SetText(Language.GetStr("Common", "ShareContent"));
  271. shareContent.SetTitle(Language.GetStr("Common", "ShareTit"));
  272. shareContent.SetTitleUrl(Language.GetShareUrl());
  273. shareContent.SetSite(Language.GetShareUrl());
  274. shareContent.SetSiteUrl(Language.GetShareUrl());
  275. shareContent.SetUrl(Language.GetShareUrl());
  276. shareContent.SetShareType(ContentType.App);
  277. if (string.IsNullOrEmpty(imagePath))
  278. {
  279. shareContent.SetImageUrl(ImageUrl);
  280. }
  281. else
  282. {
  283. ImagePath = imagePath;
  284. shareContent.SetImagePath(imagePath);
  285. }
  286. ShareSdk.ShowPlatformList(null, shareContent, 100, 100);
  287. }
  288. public static void Authorize()
  289. {
  290. ShareSdk.Authorize(PlatformType.QQ);
  291. }
  292. public static void ShareCallback(int reqID, ResponseState state, PlatformType type, Hashtable result)
  293. {
  294. if (!string.IsNullOrEmpty(ImagePath))
  295. {
  296. ManaReso.SetActive("P_Open", true);
  297. ManaReso.SetActive("P_Share", true);
  298. ImagePath = null;
  299. }
  300. if (state == ResponseState.Success)
  301. {
  302. ManaCenter.ShareAmt++;
  303. Bubble.Show(null, Language.GetStr("Common", "ShareSuccess"));
  304. }
  305. else if (state == ResponseState.Fail)
  306. {
  307. Bubble.Show(null, Language.GetStr("Common", "ShareFail"));
  308. }
  309. else if (state == ResponseState.Cancel)
  310. {
  311. Bubble.Show(null, Language.GetStr("Common", "ShareFail"));
  312. }
  313. }
  314. public static void AuthorizeCallback(int reqID, ResponseState state, PlatformType type, Hashtable result)
  315. {
  316. if (state == ResponseState.Success)
  317. {
  318. print("Success");
  319. }
  320. else if (state == ResponseState.Fail)
  321. {
  322. print("Fail");
  323. }
  324. else if (state == ResponseState.Cancel)
  325. {
  326. print("Cancel");
  327. }
  328. }
  329. }