ManaIAP.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. set { ImageUrl_ = value; }
  29. }
  30. public static string ImageUrl_;
  31. public static string ShareUrl = "http://www.dashgame.com";
  32. public static ShareSDK ShareSdk;
  33. #endregion
  34. #endregion
  35. public void Awake()
  36. {
  37. Instance = this;
  38. }
  39. public static void Initialize()
  40. {
  41. InitializeAd();
  42. ConfigurationBuilder cb = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
  43. cb.AddProduct("Test", ProductType.Consumable);
  44. ProductList.Add("Test");
  45. UnityPurchasing.Initialize(Instance, cb);
  46. if (Application.isMobilePlatform || Application.platform == RuntimePlatform.IPhonePlayer)
  47. {
  48. ShareSdk = Instance.gameObject.AddComponent<ShareSDK>();
  49. ShareSdk.Initialize();
  50. ShareSdk.shareHandler = ShareCallback;
  51. }
  52. }
  53. public static void InitializeAd()
  54. {
  55. if (Application.platform == RuntimePlatform.IPhonePlayer)
  56. {
  57. Advertisement.Initialize("1408492");
  58. }
  59. else if (Application.isMobilePlatform)
  60. {
  61. Advertisement.Initialize("1408493");
  62. }
  63. else if (Application.isEditor)
  64. {
  65. Advertisement.Initialize("1408493");
  66. }
  67. }
  68. public static void PlayAd(UnityAction action)
  69. {
  70. if (Advertisement.IsReady())
  71. {
  72. AdAction = action;
  73. ShowOptions showOptions = new ShowOptions();
  74. showOptions.resultCallback = AdCallback;
  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 Purchase(string id)
  87. {
  88. if (!LoadComplete)
  89. {
  90. Bubble.Show(null, Language.GetStr("IAP", "PurchaseFail"));
  91. return;
  92. }
  93. if (!ProductList.Contains(id))
  94. {
  95. Bubble.Show(null, Language.GetStr("IAP", "PurchaseUnvalid"));
  96. return;
  97. }
  98. IStoreController.InitiatePurchase(id);
  99. }
  100. public static void AdCallback(ShowResult showResult)
  101. {
  102. if (showResult == ShowResult.Finished)
  103. {
  104. AdAction.Invoke();
  105. ManaData.AdAmt++;
  106. }
  107. else if (showResult == ShowResult.Skipped)
  108. {
  109. Bubble.Show(null, Language.GetStr("IAP", "IncompleteAd"));
  110. }
  111. else if (showResult == ShowResult.Failed)
  112. {
  113. Bubble.Show(null, Language.GetStr("IAP", "FailAd"));
  114. }
  115. }
  116. public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e)
  117. {
  118. Bubble.Show(null, Language.GetStr("IAP", "PurchaseSuccess"));
  119. ProductDic[e.purchasedProduct.definition.id].Invoke();
  120. return PurchaseProcessingResult.Complete;
  121. }
  122. public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
  123. {
  124. LoadComplete = true;
  125. IStoreController = controller;
  126. IExtensionProvider = extensions;
  127. ManaDebug.Log("初始化IAP成功");
  128. }
  129. public void OnInitializeFailed(InitializationFailureReason error)
  130. {
  131. LoadComplete = 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 static void Share()
  140. {
  141. ShareContent shareContent = new ShareContent();
  142. shareContent.SetTitle(Language.GetStr("Common", "ShareTit"));
  143. shareContent.SetText(Language.GetStr("Common", "ShareContent"));
  144. shareContent.SetUrl(ShareUrl);
  145. shareContent.SetImagePath(ImageUrl);
  146. shareContent.SetShareType(ContentType.App);
  147. PlatformType[] platformTypes = new[]
  148. {
  149. PlatformType.QQ,
  150. PlatformType.QZone,
  151. PlatformType.WeChat,
  152. PlatformType.Facebook,
  153. PlatformType.SinaWeibo,
  154. PlatformType.GooglePlus,
  155. PlatformType.WeChatMoments,
  156. };
  157. ShareSdk.ShowPlatformList(platformTypes, shareContent, 100, 100);
  158. }
  159. public static void Authorize()
  160. {
  161. ShareSdk.Authorize(PlatformType.QQ);
  162. }
  163. public static void ShareCallback(int reqID, ResponseState state, PlatformType type, Hashtable result)
  164. {
  165. ManaDebug.Log(state.ToString());
  166. if (state == ResponseState.Success)
  167. {
  168. ManaData.ShareAmt++;
  169. Bubble.Show(null, Language.GetStr("Common", "ShareSuccess"));
  170. }
  171. else if (state == ResponseState.Fail)
  172. {
  173. Bubble.Show(null, Language.GetStr("Common", "ShareFail"));
  174. }
  175. else if (state == ResponseState.Cancel)
  176. {
  177. Bubble.Show(null, Language.GetStr("Common", "ShareFail"));
  178. }
  179. }
  180. public static void AuthorizeCallback(int reqID, ResponseState state, PlatformType type, Hashtable result)
  181. {
  182. if (state == ResponseState.Success)
  183. {
  184. print("Success");
  185. }
  186. else if (state == ResponseState.Fail)
  187. {
  188. print("Fail");
  189. }
  190. else if (state == ResponseState.Cancel)
  191. {
  192. print("Cancel");
  193. }
  194. }
  195. }