123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420 |
- 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<Chest> ADChests = new List<Chest>();
- 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<string> ProductList = new List<string>();
- public static Dictionary<string, string> PackNameDictionary = new Dictionary<string, string>();
- public static Dictionary<string, string> ProductIdDictionary = new Dictionary<string, string>();
- public static Dictionary<string, UnityAction> BuyProductCallbackDictionary = new Dictionary<string, UnityAction>();
- #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>().text = "Error Code : I1";
- InitIAP();
- GameObject.Find(CanvasLabel.U_DebugLab).GetComponent<Text>().text = "Error Code : I2";
- LastPlayADsTime = DateTime.Parse(ConfigManager.GetStringFormConfig(PlayerConfigLabel.ADPlayTime));
- GameObject.Find(CanvasLabel.U_DebugLab).GetComponent<Text>().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<ShareSDK>();
- DebugManager.Log("0");
- ShareSdk.Initialize();
- ShareSdk.shareHandler = ShareCallback;
- }
- }
- public static void InitIAP()
- {
- if (Application.isEditor)
- {
- return;
- }
- PayChannel = ResourceManager.Load<TextAsset>(ResourceLabel.Setting, Folder.Config).text.ToEnum<PayChannel>();
- 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");
- }
- }
- }
|