using cn.sharesdk.unity3d; using UnityEngine; using UnityEngine.UI; using UnityEngine.Events; using UnityEngine.Purchasing; using UnityEngine.Advertisements; using UnityEngine.Purchasing.Extension; using System; using System.IO; using System.Collections; using System.Diagnostics; using System.Collections.Generic; using Debug = UnityEngine.Debug; public enum PayChannel { AliPay, UnityIAP, } public class IAPManager : MonoBehaviour, IStoreListener { #region Config public static float ReloadADTime = 10f; public static float CreateADChestTime = 1200f; public static float CreateADChestTimer; public static DateTime? LastPlayADsTime; public static List ADChests = new List(); public static UnityAction OnPlayAD; public static bool Inited; public static IAPManager Instance; //public static bool UseAlipayOnIOS = false; public static PayChannel PayChannel; public static IStoreController IStoreController; public static IExtensionProvider IExtensionProvider; public static List ProductList = new List(); public static Dictionary PackNameDictionary = new Dictionary(); public static Dictionary ProductIdDictionary = new Dictionary(); public static Dictionary BuyProductCallbackDictionary = new Dictionary(); #region Share配置 public static string ImageUrl = "http://or5zgoeui.bkt.clouddn.com/game_icon.png"; public static string ImagePath; public static ShareSDK ShareSdk; #endregion #endregion public void Awake() { Instance = this; InitAD(); InitShareSDK(); } public void Update() { if (ADChests.Count > 0) return; CreateADChestTimer += Time.deltaTime; if (CreateADChestTimer >= ReloadADTime) { CreateADChestTimer = 0; if (!Advertisement.IsReady()) { InitAD(); } else { if (LastPlayADsTime == null) return; if (HttpManager.IsConnect && (HttpManager.CurrentDateTime.Subtract((DateTime) LastPlayADsTime).TotalSeconds > CreateADChestTime)) { if (GardenManager.InMinigameFlag && !VisitManager.InVisit && !TutorialManager.NewplayerTutorial && !SFSManager.PlazaRoomController.InPlazaRoom && !Player.InDressRoom) { ADChests.Add(ResourceManager.GetADChest()); } } } } } public static void RetrieveADChest() { if (ADChests.Count > 0) { ResourceManager.Save(ADChests[0].transform.parent); ADChests.RemoveAt(0); } } public static void Init() { GameObject.Find(CanvasLabel.U_DebugLab).GetComponent().text = "Error Code : I1"; InitIAP(); GameObject.Find(CanvasLabel.U_DebugLab).GetComponent().text = "Error Code : I2"; LastPlayADsTime = DateTime.Parse(ConfigManager.GetStringFormConfig(PlayerConfigLabel.ADPlayTime)); GameObject.Find(CanvasLabel.U_DebugLab).GetComponent().text = "Error Code : I3"; } public static void InitAD() { if (Application.platform == RuntimePlatform.IPhonePlayer) { Advertisement.Initialize("1408492", false); } else if (Application.isMobilePlatform) { Advertisement.Initialize("1408493", false); } else if (Application.isEditor) { Advertisement.Initialize("1408493", false); } } public static void InitShareSDK() { if (Application.isMobilePlatform || Application.platform == RuntimePlatform.IPhonePlayer) { ShareSdk = Instance.gameObject.AddComponent(); DebugManager.Log("0"); ShareSdk.Initialize(); ShareSdk.shareHandler = ShareCallback; } } public static void InitIAP() { if (Application.isEditor) { return; } PayChannel = ResourceManager.Load(ResourceLabel.Setting, Folder.Config).text.ToEnum(); if (Application.platform == RuntimePlatform.IPhonePlayer) { InitUnityIAP(); } else { if (PayChannel == PayChannel.AliPay) { InitAlipay(); } else if (PayChannel == PayChannel.UnityIAP) { InitUnityIAP(); } } } public static void InitAlipay() { foreach (var attribute in ConfigManager.GetIAPConfig()) { ProductList.Add(attribute[1].Value); ProductIdDictionary.Add(attribute[5].Value, attribute[1].Value); PackNameDictionary.Add(attribute[1].Value, attribute[5].Value); } AliplayManager.Instance.Init(); } public static void InitUnityIAP() { ConfigurationBuilder cb = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance()); foreach (var attribute in ConfigManager.GetIAPConfig()) { cb.AddProduct(attribute[1].Value, ProductType.Consumable); ProductList.Add(attribute[1].Value); ProductIdDictionary.Add(attribute[5].Value, attribute[1].Value); PackNameDictionary.Add(attribute[1].Value, attribute[5].Value); } UnityPurchasing.Initialize(Instance, cb); } public static void PlayAD(UnityAction action) { OnPlayAD = () => { action.Invoke(); StaticsManager.GetInstance().AdFinished(); }; if (Advertisement.IsReady()) { Manager.ReactiveFlag = true; ShowOptions showOptions = new ShowOptions(); showOptions.resultCallback = PlayADCallback; Advertisement.Show(showOptions); StaticsManager.GetInstance().AdChannel(0); } else { Bubble.Show(null, Language.GetStr(LanguageLabel.IAP__LoadAd)); } } public static void PlayADCallback(ShowResult showResult) { if (showResult == ShowResult.Finished) { OnPlayAD.Invoke(); Manager.PlayADsAmt++; LastPlayADsTime = HttpManager.CurrentDateTime; ConfigManager.SaveStringToConfig(PlayerConfigLabel.ADPlayTime, LastPlayADsTime.ToString()); } else if (showResult == ShowResult.Skipped) { Bubble.Show(null, Language.GetStr(LanguageLabel.IAP__IncompleteAd)); } else if (showResult == ShowResult.Failed) { Bubble.Show(null, Language.GetStr(LanguageLabel.IAP__FailAd)); } } public static void TryPurchase(string str) { if (!Inited) { Bubble.Show(null, Language.GetStr(LanguageLabel.IAP__PurchaseFail)); InitIAP(); return; } if (!ProductList.Contains(ProductIdDictionary[str])) { Bubble.Show(null, Language.GetStr(LanguageLabel.IAP__PurchaseUnvalid)); return; } if (RechargeGiftManager.Inited) { Purchase(str); } else { HttpManager.GetThanksGiftInfo ( jData => { RechargeGiftManager.Init(jData); Purchase(str); }, () => { Bubble.Show(null, Language.GetStr(LanguageLabel.UI__GetThanksGiftInfoFailed)); } ); } } private static void Purchase(string str) { if (PayChannel == PayChannel.AliPay) { HttpManager.GetProductID ( ProductIdDictionary[str], data => { AliplayManager.Instance.Pay(data, str); } ); } else if (PayChannel == PayChannel.UnityIAP) { IStoreController.InitiatePurchase(ProductIdDictionary[str]); } } public void OnInitialized(IStoreController controller, IExtensionProvider extensions) // { Inited = true; IStoreController = controller; IExtensionProvider = extensions; } public void OnInitializeFailed(InitializationFailureReason error) { Inited = false; } public void OnPurchaseFailed(Product i, PurchaseFailureReason p) { Bubble.Show(null, Language.GetStr(LanguageLabel.IAP__PurchaseFail)); } public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e) { Bubble.Show(null, Language.GetStr(LanguageLabel.IAP__PurchaseSuccess)); string packName = PackNameDictionary[e.purchasedProduct.definition.id]; BuyProductCallbackDictionary[packName].Invoke(); return PurchaseProcessingResult.Complete; } public static void Share(string imagePath = null, string content = null) { try { ShareContent shareContent = new ShareContent(); if (content == null) { shareContent.SetText(Language.GetStr(LanguageLabel.Common__ShareContent)); } else { shareContent.SetText(content); } shareContent.SetTitle(Language.GetStr(LanguageLabel.Common__ShareTit)); shareContent.SetTitleUrl(Language.GetShareUrl()); shareContent.SetSite(Language.GetShareUrl()); shareContent.SetSiteUrl(Language.GetShareUrl()); shareContent.SetUrl(Language.GetShareUrl()); shareContent.SetShareType(ContentType.App); if (string.IsNullOrEmpty(imagePath)) { shareContent.SetImageUrl(ImageUrl); } else { ImagePath = imagePath; shareContent.SetImagePath(imagePath); } ShareSdk.ShowPlatformList(null, shareContent, 100, 100); } catch (Exception e) { DebugManager.Log(e.ToString()); } } public static void Authorize() { ShareSdk.Authorize(PlatformType.QQ); } public static void ShareCallback(int reqID, ResponseState state, PlatformType type, Hashtable result) { if (!string.IsNullOrEmpty(ImagePath)) { ResourceManager.SetActive(CanvasLabel.P_Open, true); ResourceManager.SetActive(CanvasLabel.P_Share, true); ImagePath = null; } if (state == ResponseState.Success) { Manager.ShareAmt++; Bubble.Show(null, Language.GetStr(LanguageLabel.Common__ShareSuccess)); } else if (state == ResponseState.Fail) { Bubble.Show(null, Language.GetStr(LanguageLabel.Common__ShareFail)); } else if (state == ResponseState.Cancel) { Bubble.Show(null, Language.GetStr(LanguageLabel.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"); } } }