ManaIAP.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 System;
  8. using System.IO;
  9. using System.Collections;
  10. using System.Collections.Generic;
  11. public class ManaIAP : MonoBehaviour, IStoreListener
  12. {
  13. #region 变量
  14. public static bool Complete;
  15. public static ManaIAP Instance;
  16. public static UnityAction AdAction;
  17. public static IStoreController IStoreController;
  18. public static IExtensionProvider IExtensionProvider;
  19. public static List<string> ProductList = new List<string>();
  20. public static Dictionary<string, UnityAction> ProductDic = new Dictionary<string, UnityAction>();
  21. #region Share配置
  22. public static string ImageUrl = "http://or5zgoeui.bkt.clouddn.com/game_icon.png";
  23. public static ShareSDK ShareSdk;
  24. #endregion
  25. #endregion
  26. public void Awake()
  27. {
  28. Instance = this;
  29. }
  30. public static void Initialize()
  31. {
  32. InitializeAD();
  33. InitializeIAP();
  34. InitializeShare();
  35. }
  36. public static void InitializeAD()
  37. {
  38. if (Application.platform == RuntimePlatform.IPhonePlayer)
  39. {
  40. Advertisement.Initialize("1408492", false);
  41. }
  42. else if (Application.isMobilePlatform)
  43. {
  44. Advertisement.Initialize("1408493", false);
  45. }
  46. else if (Application.isEditor)
  47. {
  48. Advertisement.Initialize("1408493", false);
  49. }
  50. }
  51. public static void InitializeIAP()
  52. {
  53. ConfigurationBuilder cb = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
  54. cb.AddProduct("Test", ProductType.Consumable);
  55. ProductList.Add("Test");
  56. UnityPurchasing.Initialize(Instance, cb);
  57. }
  58. public static void InitializeShare()
  59. {
  60. if (Application.isMobilePlatform || Application.platform == RuntimePlatform.IPhonePlayer)
  61. {
  62. ShareSdk = Instance.gameObject.AddComponent<ShareSDK>();
  63. ShareSdk.Initialize();
  64. ShareSdk.shareHandler = ShareCallback;
  65. }
  66. }
  67. public static void PlayAD(UnityAction action)
  68. {
  69. if (Advertisement.IsReady())
  70. {
  71. ManaCenter.ReactiveLock = true;
  72. AdAction = action;
  73. ShowOptions showOptions = new ShowOptions();
  74. showOptions.resultCallback = CallbackAD;
  75. Advertisement.Show(showOptions);
  76. }
  77. else
  78. {
  79. Bubble.Show(null, Language.GetStr("IAP", "LoadAd"));
  80. if (!Advertisement.isInitialized)
  81. {
  82. InitializeAD();
  83. }
  84. }
  85. }
  86. public static void CallbackAD(ShowResult showResult)
  87. {
  88. if (showResult == ShowResult.Finished)
  89. {
  90. AdAction.Invoke();
  91. ManaCenter.AdAmt++;
  92. }
  93. else if (showResult == ShowResult.Skipped)
  94. {
  95. Bubble.Show(null, Language.GetStr("IAP", "IncompleteAd"));
  96. }
  97. else if (showResult == ShowResult.Failed)
  98. {
  99. Bubble.Show(null, Language.GetStr("IAP", "FailAd"));
  100. }
  101. }
  102. public static void Purchase(string id)
  103. {
  104. bool freeze = true;
  105. if (freeze)
  106. {
  107. Bubble.Show(null, Language.GetStr("IAP", "TemporarilyUnavailible"));
  108. return;
  109. }
  110. if (!Complete)
  111. {
  112. Bubble.Show(null, Language.GetStr("IAP", "PurchaseFail"));
  113. return;
  114. }
  115. if (!ProductList.Contains(id))
  116. {
  117. Bubble.Show(null, Language.GetStr("IAP", "PurchaseUnvalid"));
  118. return;
  119. }
  120. IStoreController.InitiatePurchase(id);
  121. }
  122. public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
  123. {
  124. Complete = true;
  125. IStoreController = controller;
  126. IExtensionProvider = extensions;
  127. ManaDebug.Log("初始化IAP成功");
  128. }
  129. public void OnInitializeFailed(InitializationFailureReason error)
  130. {
  131. Complete = false;
  132. Debug.Log("初始化IAP失败");
  133. ManaDebug.Log("初始化IAP失败");
  134. }
  135. public void OnPurchaseFailed(Product i, PurchaseFailureReason p)
  136. {
  137. Bubble.Show(null, Language.GetStr("IAP", "PurchaseFail"));
  138. }
  139. public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e)
  140. {
  141. Bubble.Show(null, Language.GetStr("IAP", "PurchaseSuccess"));
  142. ProductDic[e.purchasedProduct.definition.id].Invoke();
  143. return PurchaseProcessingResult.Complete;
  144. }
  145. public static void Share()
  146. {
  147. ShareContent shareContent = new ShareContent();
  148. shareContent.SetText(Language.GetStr("Common", "ShareContent"));
  149. shareContent.SetTitle(Language.GetStr("Common", "ShareTit"));
  150. shareContent.SetUrl(Language.GetShareUrl());
  151. shareContent.SetImageUrl(ImageUrl);
  152. shareContent.SetShareType(ContentType.App);
  153. ShareSdk.ShowPlatformList(null, shareContent, 100, 100);
  154. }
  155. public static void Authorize()
  156. {
  157. ShareSdk.Authorize(PlatformType.QQ);
  158. }
  159. public static void ShareCallback(int reqID, ResponseState state, PlatformType type, Hashtable result)
  160. {
  161. if (state == ResponseState.Success)
  162. {
  163. ManaCenter.ShareAmt++;
  164. Bubble.Show(null, Language.GetStr("Common", "ShareSuccess"));
  165. }
  166. else if (state == ResponseState.Fail)
  167. {
  168. Bubble.Show(null, Language.GetStr("Common", "ShareFail"));
  169. }
  170. else if (state == ResponseState.Cancel)
  171. {
  172. Bubble.Show(null, Language.GetStr("Common", "ShareFail"));
  173. }
  174. }
  175. public static void AuthorizeCallback(int reqID, ResponseState state, PlatformType type, Hashtable result)
  176. {
  177. if (state == ResponseState.Success)
  178. {
  179. print("Success");
  180. }
  181. else if (state == ResponseState.Fail)
  182. {
  183. print("Fail");
  184. }
  185. else if (state == ResponseState.Cancel)
  186. {
  187. print("Cancel");
  188. }
  189. }
  190. }