using System; using UnityEngine; using System.Collections; using System.Collections.Generic; using System.IO; using cn.sharesdk.unity3d; using UnityEngine.Advertisements; using UnityEngine.Events; using UnityEngine.Purchasing; using UnityEngine.UI; public class ManaIAP : MonoBehaviour, IStoreListener { #region 变量 public static bool LoadComplete; public static UnityAction AdAction; public static ManaIAP Instance; public static IStoreController IStoreController; public static IExtensionProvider IExtensionProvider; public static List ProductList = new List(); public static Dictionary ProductDic = new Dictionary(); #region Share配置 public static string ImageUrl { get { return Application.persistentDataPath + "/ICON2.png"; } set { ImageUrl_ = value; } } public static string ImageUrl_; public static string ShareUrl = "http://www.dashgame.com"; public static ShareSDK ShareSdk; #endregion #endregion public void Awake() { Instance = this; } public static void Initialize() { InitializeAd(); ConfigurationBuilder cb = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance()); cb.AddProduct("Test", ProductType.Consumable); ProductList.Add("Test"); UnityPurchasing.Initialize(Instance, cb); if (Application.isMobilePlatform || Application.platform == RuntimePlatform.IPhonePlayer) { ShareSdk = Instance.gameObject.AddComponent(); ShareSdk.Initialize(); ShareSdk.shareHandler = ShareCallback; } } public static void InitializeAd() { if (Application.platform == RuntimePlatform.IPhonePlayer) { Advertisement.Initialize("1408492"); } else if (Application.isMobilePlatform) { Advertisement.Initialize("1408493"); } else if (Application.isEditor) { Advertisement.Initialize("1408493"); } } public static void PlayAd(UnityAction action) { if (Advertisement.IsReady()) { AdAction = action; ShowOptions showOptions = new ShowOptions(); showOptions.resultCallback = AdCallback; Advertisement.Show(showOptions); } else { Bubble.Show(null, Language.GetStr("IAP", "LoadAd")); if (!Advertisement.isInitialized) { InitializeAd(); } } } public static void Purchase(string id) { if (!LoadComplete) { Bubble.Show(null, Language.GetStr("IAP", "PurchaseFail")); return; } if (!ProductList.Contains(id)) { Bubble.Show(null, Language.GetStr("IAP", "PurchaseUnvalid")); return; } IStoreController.InitiatePurchase(id); } public static void AdCallback(ShowResult showResult) { if (showResult == ShowResult.Finished) { AdAction.Invoke(); ManaData.AdAmt++; } else if (showResult == ShowResult.Skipped) { Bubble.Show(null, Language.GetStr("IAP", "IncompleteAd")); } else if (showResult == ShowResult.Failed) { Bubble.Show(null, Language.GetStr("IAP", "FailAd")); } } public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e) { Bubble.Show(null, Language.GetStr("IAP", "PurchaseSuccess")); ProductDic[e.purchasedProduct.definition.id].Invoke(); return PurchaseProcessingResult.Complete; } public void OnInitialized(IStoreController controller, IExtensionProvider extensions) { LoadComplete = true; IStoreController = controller; IExtensionProvider = extensions; ManaDebug.Log("初始化IAP成功"); } public void OnInitializeFailed(InitializationFailureReason error) { LoadComplete = false; Debug.Log("初始化IAP失败"); ManaDebug.Log("初始化IAP失败"); } public void OnPurchaseFailed(Product i, PurchaseFailureReason p) { Bubble.Show(null, Language.GetStr("IAP", "PurchaseFail")); } public static void Share() { ShareContent shareContent = new ShareContent(); shareContent.SetTitle(Language.GetStr("Common", "ShareTit")); shareContent.SetText(Language.GetStr("Common", "ShareContent")); shareContent.SetUrl(ShareUrl); shareContent.SetImagePath(ImageUrl); shareContent.SetShareType(ContentType.App); PlatformType[] platformTypes = new[] { PlatformType.QQ, PlatformType.QZone, PlatformType.WeChat, PlatformType.Facebook, PlatformType.SinaWeibo, PlatformType.GooglePlus, PlatformType.WeChatMoments, }; ShareSdk.ShowPlatformList(platformTypes, shareContent, 100, 100); } public static void Authorize() { ShareSdk.Authorize(PlatformType.QQ); } public static void ShareCallback(int reqID, ResponseState state, PlatformType type, Hashtable result) { ManaDebug.Log(state.ToString()); if (state == ResponseState.Success) { ManaData.ShareAmt++; Bubble.Show(null, Language.GetStr("Common", "ShareSuccess")); } else if (state == ResponseState.Fail) { Bubble.Show(null, Language.GetStr("Common", "ShareFail")); } else if (state == ResponseState.Cancel) { Bubble.Show(null, Language.GetStr("Common", "ShareFail")); } } public static void AuthorizeCallback(int reqID, ResponseState state, PlatformType type, Hashtable result) { if (state == ResponseState.Success) { print("Success"); } else if (state == ResponseState.Fail) { print("Fail"); } else if (state == ResponseState.Cancel) { print("Cancel"); } } }