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 Timer; public static DateTime? ADPlayTime = null; public static List ChestList = new List(); public static bool UseAlipayOnIOS = false; public static bool Complete; public static IAPManager Instance; public static PayChannel PayChannel; public static UnityAction AdAction; public static IStoreController IStoreController; public static IExtensionProvider IExtensionProvider; public static List ProductList = new List(); public static Dictionary PackNameDic = new Dictionary(); public static Dictionary ProductIdDic = new Dictionary(); public static Dictionary ProductActionDic = 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; InitializeAD(); InitializeShare(); } public void Update() { if (ChestList.Count > 0) return; Timer += Time.deltaTime; if (Timer >= 10) { Timer = 0; if (!Advertisement.IsReady()) { InitializeAD(); } else { if (ADPlayTime == null) return; if (HttpManager.Connect && (HttpManager.Time.Subtract((DateTime)ADPlayTime).TotalSeconds > 1200)) { if (GardenManager.MiniLock && !VisitManager.InVisit && !TutorialManager.NewplayerTutorial && !SFSManager.PlazaRoomController.InPlazaRoom && !Player.InDressRoom) { ChestList.Add(ResourceManager.GetADChest()); } } } } } public static void RetrieveADChest() { if (ChestList.Count > 0) { ResourceManager.Save(ChestList[0].transform.parent); ChestList.RemoveAt(0); } } public static void Initialize() { //InitializeAD(); //GameObject.Find(PrefabLabel.I_Lab).GetComponent().text = "Error Code : AD"; GameObject.Find(ObjectLabel.U_DebugLab).GetComponent().text = "Error Code : I1"; InitializeIAP(); GameObject.Find(ObjectLabel.U_DebugLab).GetComponent().text = "Error Code : I2"; ADPlayTime = DateTime.Parse(ConfigManager.GetPlayerString("ADPlayTime")); GameObject.Find(ObjectLabel.U_DebugLab).GetComponent().text = "Error Code : I3"; //InitializeShare(); //GameObject.Find(PrefabLabel.I_Lab).GetComponent().text = "Error Code : Share"; } public static void InitializeAD() { 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 InitializeShare() { if (Application.isMobilePlatform || Application.platform == RuntimePlatform.IPhonePlayer) { ShareSdk = Instance.gameObject.AddComponent(); ShareSdk.Initialize(); ShareSdk.shareHandler = ShareCallback; } } public static void InitializeIAP() { if (Application.isEditor) { return; } PayChannel = ResourceManager.Load("Setting", Folder.Config).text.ToEnum(); if (Application.platform == RuntimePlatform.IPhonePlayer) { if (UseAlipayOnIOS) { InitializeAlipay(); } else { InitializeUnityIAP(); } } else { if (PayChannel == PayChannel.AliPay) { InitializeAlipay(); } else if (PayChannel == PayChannel.UnityIAP) { InitializeUnityIAP(); } } } public static void InitializeAlipay() { foreach (var attribute in ConfigManager.GetIAPConfig()) { ProductList.Add(attribute[1].Value); ProductIdDic.Add(attribute[5].Value, attribute[1].Value); PackNameDic.Add(attribute[1].Value, attribute[5].Value); } AliplayManager.Instance.Init(); } public static void InitializeUnityIAP() { ConfigurationBuilder cb = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance()); foreach (var attribute in ConfigManager.GetIAPConfig()) { cb.AddProduct(attribute[1].Value, ProductType.Consumable); ProductList.Add(attribute[1].Value); ProductIdDic.Add(attribute[5].Value, attribute[1].Value); PackNameDic.Add(attribute[1].Value, attribute[5].Value); } UnityPurchasing.Initialize(Instance, cb); } public static void PlayAD(UnityAction action) { AdAction = () => { action.Invoke(); StaticsManager.GetInstance().AdFinished(); }; if (Advertisement.IsReady()) { Manager.ReactiveLock = true; ShowOptions showOptions = new ShowOptions(); showOptions.resultCallback = CallbackAD; Advertisement.Show(showOptions); StaticsManager.GetInstance().AdChannel(0); } else { Bubble.Show(null, Language.GetStr(LanguageLabel.IAP__LoadAd)); } } public static void CallbackAD(ShowResult showResult) { if (showResult == ShowResult.Finished) { AdAction.Invoke(); Manager.AdAmt++; ADPlayTime = HttpManager.Time; ConfigManager.SavePlayerString(PlayerConfigLabel.ADPlayTime, ADPlayTime.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 Purchase(string str) { //bool freeze = true; //if (freeze) //{ // Bubble.Show(null, Language.GetStr(LanguageLabel.IAP__TemporarilyUnavailible)); // return; //} if (!Complete) { Bubble.Show(null, Language.GetStr(LanguageLabel.IAP__PurchaseFail)); InitializeIAP(); return; } if (!ProductList.Contains(ProductIdDic[str])) { Bubble.Show(null, Language.GetStr(LanguageLabel.IAP__PurchaseUnvalid)); return; } HttpManager.GetThanksGiftInfo ( jData => { ThanksGift.Init(jData); PurchaseCore(str); }, () => { Bubble.Show(null, Language.GetStr(LanguageLabel.UI__GetThanksGiftInfoFailed)); } ); } private static void PurchaseCore(string str) { if (PayChannel == PayChannel.AliPay) { HttpManager.GetProductID ( ProductIdDic[str], data => { AliplayManager.Instance.Pay(data, str); } ); } else if (PayChannel == PayChannel.UnityIAP) { IStoreController.InitiatePurchase(ProductIdDic[str]); } } public void OnInitialized(IStoreController controller, IExtensionProvider extensions) { Complete = true; IStoreController = controller; IExtensionProvider = extensions; //ManaDebug.Log("初始化IAP成功"); } public void OnInitializeFailed(InitializationFailureReason error) { Complete = false; //Debug.Log("初始化IAP失败"); //ManaDebug.Log("初始化IAP失败"); } 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 = PackNameDic[e.purchasedProduct.definition.id]; ProductActionDic[packName].Invoke(); return PurchaseProcessingResult.Complete; } public static void Share(string imagePath = null) { ShareContent shareContent = new ShareContent(); shareContent.SetText(Language.GetStr(LanguageLabel.Common__ShareContent)); 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); } 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(ObjectLabel.P_Open, true); ResourceManager.SetActive(ObjectLabel.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"); } } }