googleplay设置
进入play console后可以发布应用
点击所有应用->创建应用(这部经常报错误码,多试几次就ok可能和vpn有关)
创建一个应用成功后,这个应用就会包含上面所有选项
先在应用版本中传包,
然后商品详情中填写游戏相关说明图片,
内容分级中填写分级调查问卷,
定价和分发范围中填写游戏付费类型
应用内商品填写内购商品信息,这里注意商品id要与后面代码中的的id一致,eg:
服务和API中有个key,这个key要填写到unity service in-purchase googlePlay需要的那个key的位置
付费测试说明
新建号应用后,查看应用版本,能看到可以上传4个版本,内部测试渠道,封闭测试渠道,开放式渠道,正式版渠道
每个里面都可以管理添加测试账号,添加了测试账号后,测试计费时会提示当前是测试版本不会真正收取费用相关的提示
unity配置
Services->
IN-APP PURCHASING 中设置好googleplay中得到的的keyid
代码
在商店启动前初始化这个类
登录后复制
using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.Purchasing; public class Purchaser : MonoBehaviour, IStoreListener { private static IStoreController m_StoreController; private static IExtensionProvider m_StoreExtensionProvider; public static string kProductIDSubscription = "subscription"; private static string kProductNameAppleSubscription = "com.unity3d.subscription.new"; private static string kProductNameGooglePlaySubscription = "com.unity3d.subscription.original"; public string[] kPurchaserIdList = { "com.diffcolor.gaincoins1", "com.diffcolor.gaincoins2", "com.diffcolor.gaincoins3", "com.diffcolor.gaincoins4", }; private static Purchaser Ins; public static Purchaser GetIns () { return Ins; } void Awake () { Ins = this; } void Start () { DontDestroyOnLoad (gameObject); if (m_StoreController == null) { Debug.Log ("[n]----Purchaser.Start Init"); InitializePurchasing (); } } #region init public void InitializePurchasing () { if (IsInitialized ()) { Debug.Log ("[n]----Purchaser.InitializePurchasing Already.Init"); return; } Debug.Log ("[n]----Purchaser.InitializePurchasing Begain Init"); var builder = ConfigurationBuilder.Instance (StandardPurchasingModule.Instance ()); builder.AddProduct (kProductIDSubscription, ProductType.NonConsumable, new IDs () { { kProductNameAppleSubscription, AppleAppStore.Name }, { kProductNameGooglePlaySubscription, GooglePlay.Name }, }); //初始化各分类内购 ID for (int x = 0; x < kPurchaserIdList.Length - 1; x++) { String purchaseID = kPurchaserIdList [x]; builder.AddProduct (purchaseID, ProductType.Consumable); } //初始化广告内购 ID builder.AddProduct (kPurchaserIdList [kPurchaserIdList.Length - 1], ProductType.NonConsumable); UnityPurchasing.Initialize (this, builder); } private bool IsInitialized () { //bool b = m_StoreController != null && m_StoreExtensionProvider != null; bool b0 = m_StoreController != null; bool b1 = m_StoreExtensionProvider != null; bool b = b0 && b1; Debug.Log ("[n]----Purchaser.IsInitialized b0:" + b0 + " b1:" + b1); return b; } public void OnInitialized (IStoreController controller, IExtensionProvider extensions) { Debug.Log ("[n]----Purchaser.OnInitialized init CB success "); m_StoreController = controller; m_StoreExtensionProvider = extensions; } public void OnInitializeFailed (InitializationFailureReason error) { Debug.Log ("[ne]----Purchaser.OnInitializeFailed init CB faile errer:" + error); } #endregion #region buy public string getPrice (string id) { string tstr = null; Product product = m_StoreController.products.WithID (id); if (product != null && product.availableToPurchase) { tstr = m_StoreController.products.WithID (id).metadata.localizedPriceString.ToString (); } Debug.Log ("[n]----Purchaser.getPrice id:" + id + " content:" + tstr); return tstr; } public void BuyConsumable (string id) { BuyProductID (id); } void BuyProductID (string productId) { if (IsInitialized ()) { Debug.Log ("[n]----Purchaser.BuyProductID productId:" + productId); Product product = m_StoreController.products.WithID (productId); if (product != null && product.availableToPurchase) { Debug.Log ("[n]----Purchaser.BuyProductID 启动计费 productId:" + product.definition.id); //Debug.Log(string.Format("Purchasing product asychronously: '{0}'", product.definition.id)); m_StoreController.InitiatePurchase (product); } else { if (product == null) Debug.Log ("[ne]----Purchaser.BuyProductID product==null productId:" + productId); if (!product.availableToPurchase) Debug.Log ("[ne]----Purchaser.BuyProductID availableToPurchase==false productId:" + productId); onPurchaseFailed ("Not Found product."); } } else { Debug.Log ("[ne]----Purchaser.BuyProductID fail,Not Init productId:" + productId); onPurchaseFailed ("Not initialized."); } } #endregion #region restore public void RestorePurchases () { if (!IsInitialized ()) { Debug.Log ("[n]----Purchaser.RestorePurchases fail,Not initialized"); return; } if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.OSXPlayer) { Debug.Log ("[n]----Purchaser.RestorePurchases restoreStart!"); var apple = m_StoreExtensionProvider.GetExtension<IAppleExtensions> (); apple.RestoreTransactions ((result) => { if (result) { Debug.Log ("[n]----Purchaser.RestorePurchases Success!"); GameManager.instance.onRestorePurchasesSuccess (); } else { Debug.Log ("[ne]----Purchaser.RestorePurchases Fail!"); GameManager.instance.onRestorePurchasesFailed (); } }); } else { Debug.Log ("[ne]----Purchaser.RestorePurchases platform error current=" + Application.platform); GameManager.instance.onRestorePurchasesFailed (); } } #endregion #region cb //success; public PurchaseProcessingResult ProcessPurchase (PurchaseEventArgs args) { Product product = args.purchasedProduct; Dictionary<string, object> parameters = new Dictionary<string, object> (); //parameters[AppEventParameterName.ContentID] = product.definition.id; float price = (float)product.metadata.localizedPrice; string currency = product.metadata.isoCurrencyCode; Debug.Log ("[n]----Purchaser.ProcessPurchase PayCB===>productID:" + product.definition.id + " price:" + price + " currentcy:" + currency); if (String.Equals (args.purchasedProduct.definition.id, kPurchaserIdList [0], StringComparison.Ordinal)) { GameManager.instance.purchansedCallback (450); AnalyticsUtils.analyPurchase (1.99f, price, currency, product.definition.id, parameters); } else if (String.Equals (args.purchasedProduct.definition.id, kPurchaserIdList [1], StringComparison.Ordinal)) { GameManager.instance.purchansedCallback (2000); AnalyticsUtils.analyPurchase (7.99f, price, currency, product.definition.id, parameters); } else if (String.Equals (args.purchasedProduct.definition.id, kPurchaserIdList [2], StringComparison.Ordinal)) { GameManager.instance.purchansedCallback (5600); AnalyticsUtils.analyPurchase (20.99f, price, currency, product.definition.id, parameters); } else if (String.Equals (args.purchasedProduct.definition.id, kPurchaserIdList [3], StringComparison.Ordinal)) { GameManager.instance.purchansedCallback (15200); AnalyticsUtils.analyPurchase (53.99f, price, currency, product.definition.id, parameters); } else if (String.Equals (args.purchasedProduct.definition.id, kPurchaserIdList [4], StringComparison.Ordinal)) { GameManager.instance.onPurchaseNoAdsSuccess (); AnalyticsUtils.analyPurchase (2.99f, price, currency, product.definition.id, parameters); } Debug.Log ("[n]----Purchaser.ProcessPurchase befor data report!!!"); //添加购买成功统计 Dictionary<string, object> dict = new Dictionary<string, object> (); dict.Add ("Level", "Level_" + GameData.instance.cLevel); dict.Add ("productId", product.definition.id); AnalyticsUtils.analyticsEvent ("Purchase Success", dict); Debug.Log ("[n]----Purchaser.ProcessPurchase after data report!!!"); return PurchaseProcessingResult.Complete; } void onPurchaseFailed (string failureReason) { Debug.Log ("[ne]----Purchaser.onPurchaseFailed befor data report!!! errorMsg:" + failureReason); GameManager.instance.onPurchaseFailed (); Dictionary<string, object> dict = new Dictionary<string, object> (); dict.Add ("FailureReason", failureReason); AnalyticsUtils.analyticsEvent ("Purchase Fail", dict); Debug.Log ("[ne]----Purchaser.onPurchaseFailed after data report!!!"); } #endregion #region unuse public void OnPurchaseFailed (Product product, PurchaseFailureReason failureReason) { Debug.Log (string.Format ("[ne]----OnPurchaseFailed: FAIL. Product: '{0}', PurchaseFailureReason: {1}", product.definition.storeSpecificId, failureReason)); onPurchaseFailed (string.Format ("Product: '{0}', FailureReason: {1}", product.definition.storeSpecificId, failureReason)); } #endregion }1.
免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删