ManaIAP.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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
  23. {
  24. get
  25. {
  26. string iconPath = Application.persistentDataPath + "/ICON2.png";
  27. if (!File.Exists(iconPath))
  28. {
  29. Texture2D texture2D = ManaReso.Load<Texture2D>("ICON2", Folder.UI);
  30. File.WriteAllBytes(iconPath, texture2D.EncodeToPNG());
  31. }
  32. return Application.persistentDataPath + "/ICON2.png";
  33. }
  34. }
  35. public static string ShareUrl = "http://www.dashgame.com";
  36. public static ShareSDK ShareSdk;
  37. #endregion
  38. #endregion
  39. public void Awake()
  40. {
  41. Instance = this;
  42. }
  43. public static void Initialize()
  44. {
  45. InitializeAD();
  46. InitializeIAP();
  47. InitializeShare();
  48. }
  49. public static void InitializeAD()
  50. {
  51. if (Application.platform == RuntimePlatform.IPhonePlayer)
  52. {
  53. Advertisement.Initialize("1408492", true);
  54. }
  55. else if (Application.isMobilePlatform)
  56. {
  57. Advertisement.Initialize("1408493", true);
  58. }
  59. else if (Application.isEditor)
  60. {
  61. Advertisement.Initialize("1408493", true);
  62. }
  63. }
  64. public static void InitializeIAP()
  65. {
  66. ConfigurationBuilder cb = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
  67. cb.AddProduct("Test", ProductType.Consumable);
  68. ProductList.Add("Test");
  69. UnityPurchasing.Initialize(Instance, cb);
  70. }
  71. public static void InitializeShare()
  72. {
  73. if (Application.isMobilePlatform || Application.platform == RuntimePlatform.IPhonePlayer)
  74. {
  75. ShareSdk = Instance.gameObject.AddComponent<ShareSDK>();
  76. ShareSdk.Initialize();
  77. ShareSdk.shareHandler = ShareCallback;
  78. }
  79. }
  80. public static void PlayAD(UnityAction action)
  81. {
  82. if (Advertisement.IsReady())
  83. {
  84. AdAction = action;
  85. ShowOptions showOptions = new ShowOptions();
  86. showOptions.resultCallback = CallbackAD;
  87. Advertisement.Show(showOptions);
  88. }
  89. else
  90. {
  91. Bubble.Show(null, Language.GetStr("IAP", "LoadAd"));
  92. if (!Advertisement.isInitialized)
  93. {
  94. InitializeAD();
  95. }
  96. }
  97. }
  98. public static void CallbackAD(ShowResult showResult)
  99. {
  100. if (showResult == ShowResult.Finished)
  101. {
  102. AdAction.Invoke();
  103. ManaCenter.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 static void Purchase(string id)
  115. {
  116. bool freeze = true;
  117. if (freeze)
  118. {
  119. Bubble.Show(null, Language.GetStr("IAP", "TemporarilyUnavailible"));
  120. return;
  121. }
  122. if (!Complete)
  123. {
  124. Bubble.Show(null, Language.GetStr("IAP", "PurchaseFail"));
  125. return;
  126. }
  127. if (!ProductList.Contains(id))
  128. {
  129. Bubble.Show(null, Language.GetStr("IAP", "PurchaseUnvalid"));
  130. return;
  131. }
  132. IStoreController.InitiatePurchase(id);
  133. }
  134. public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
  135. {
  136. Complete = true;
  137. IStoreController = controller;
  138. IExtensionProvider = extensions;
  139. ManaDebug.Log("初始化IAP成功");
  140. }
  141. public void OnInitializeFailed(InitializationFailureReason error)
  142. {
  143. Complete = false;
  144. Debug.Log("初始化IAP失败");
  145. ManaDebug.Log("初始化IAP失败");
  146. }
  147. public void OnPurchaseFailed(Product i, PurchaseFailureReason p)
  148. {
  149. Bubble.Show(null, Language.GetStr("IAP", "PurchaseFail"));
  150. }
  151. public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e)
  152. {
  153. Bubble.Show(null, Language.GetStr("IAP", "PurchaseSuccess"));
  154. ProductDic[e.purchasedProduct.definition.id].Invoke();
  155. return PurchaseProcessingResult.Complete;
  156. }
  157. public static void Share()
  158. {
  159. ShareContent shareContent = new ShareContent();
  160. shareContent.SetText(Language.GetStr("Common", "ShareContent"));
  161. shareContent.SetTitle(Language.GetStr("Common", "ShareTit"));
  162. shareContent.SetUrl(ShareUrl);
  163. shareContent.SetImagePath(ImageUrl);
  164. shareContent.SetShareType(ContentType.App);
  165. ShareSdk.ShowPlatformList(null, shareContent, 100, 100);
  166. }
  167. public static void Authorize()
  168. {
  169. ShareSdk.Authorize(PlatformType.QQ);
  170. }
  171. public static void ShareCallback(int reqID, ResponseState state, PlatformType type, Hashtable result)
  172. {
  173. if (state == ResponseState.Success)
  174. {
  175. ManaCenter.ShareAmt++;
  176. Bubble.Show(null, Language.GetStr("Common", "ShareSuccess"));
  177. }
  178. else if (state == ResponseState.Fail)
  179. {
  180. Bubble.Show(null, Language.GetStr("Common", "ShareFail"));
  181. }
  182. else if (state == ResponseState.Cancel)
  183. {
  184. Bubble.Show(null, Language.GetStr("Common", "ShareFail"));
  185. }
  186. }
  187. public static void AuthorizeCallback(int reqID, ResponseState state, PlatformType type, Hashtable result)
  188. {
  189. if (state == ResponseState.Success)
  190. {
  191. print("Success");
  192. }
  193. else if (state == ResponseState.Fail)
  194. {
  195. print("Fail");
  196. }
  197. else if (state == ResponseState.Cancel)
  198. {
  199. print("Cancel");
  200. }
  201. }
  202. }