using cn.sharesdk.unity3d; using UnityEngine; using UnityEngine.UI; using UnityEngine.Events; using UnityEngine.Purchasing; using UnityEngine.Advertisements; using System; using System.IO; using System.Collections; using System.Collections.Generic; public class ManaIAP : MonoBehaviour, IStoreListener { #region 变量 public static bool Complete; public static ManaIAP Instance; public static UnityAction AdAction; 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 { string iconPath = Application.persistentDataPath + "/ICON2.png"; if (!File.Exists(iconPath)) { Texture2D texture2D = ManaReso.Load("ICON2", Folder.UI); File.WriteAllBytes(iconPath, texture2D.EncodeToPNG()); } return Application.persistentDataPath + "/ICON2.png"; } } public static string ShareUrl = "http://www.dashgame.com"; public static ShareSDK ShareSdk; #endregion #endregion public void Awake() { Instance = this; } public static void Initialize() { InitializeAD(); InitializeIAP(); InitializeShare(); } public static void InitializeAD() { if (Application.platform == RuntimePlatform.IPhonePlayer) { Advertisement.Initialize("1408492", true); } else if (Application.isMobilePlatform) { Advertisement.Initialize("1408493", true); } else if (Application.isEditor) { Advertisement.Initialize("1408493", true); } } public static void InitializeIAP() { ConfigurationBuilder cb = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance()); cb.AddProduct("Test", ProductType.Consumable); ProductList.Add("Test"); UnityPurchasing.Initialize(Instance, cb); } public static void InitializeShare() { if (Application.isMobilePlatform || Application.platform == RuntimePlatform.IPhonePlayer) { ShareSdk = Instance.gameObject.AddComponent(); ShareSdk.Initialize(); ShareSdk.shareHandler = ShareCallback; } } public static void PlayAD(UnityAction action) { if (Advertisement.IsReady()) { AdAction = action; ShowOptions showOptions = new ShowOptions(); showOptions.resultCallback = CallbackAD; Advertisement.Show(showOptions); } else { Bubble.Show(null, Language.GetStr("IAP", "LoadAd")); if (!Advertisement.isInitialized) { InitializeAD(); } } } public static void CallbackAD(ShowResult showResult) { if (showResult == ShowResult.Finished) { AdAction.Invoke(); ManaCenter.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 static void Purchase(string id) { bool freeze = true; if (freeze) { Bubble.Show(null, Language.GetStr("IAP", "TemporarilyUnavailible")); return; } if (!Complete) { Bubble.Show(null, Language.GetStr("IAP", "PurchaseFail")); return; } if (!ProductList.Contains(id)) { Bubble.Show(null, Language.GetStr("IAP", "PurchaseUnvalid")); return; } IStoreController.InitiatePurchase(id); } 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("IAP", "PurchaseFail")); } public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e) { Bubble.Show(null, Language.GetStr("IAP", "PurchaseSuccess")); ProductDic[e.purchasedProduct.definition.id].Invoke(); return PurchaseProcessingResult.Complete; } public static void Share() { ShareContent shareContent = new ShareContent(); shareContent.SetText(Language.GetStr("Common", "ShareContent")); shareContent.SetTitle(Language.GetStr("Common", "ShareTit")); shareContent.SetUrl(ShareUrl); shareContent.SetImagePath(ImageUrl); shareContent.SetShareType(ContentType.App); 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 (state == ResponseState.Success) { ManaCenter.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"); } } }