ManaIAP.cs 6.1 KB

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