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

代码

在商店启动前初始化这个类

  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Purchasing;
  5.  
  6. public class Purchaser : MonoBehaviour, IStoreListener
  7. {
  8. private static IStoreController m_StoreController;
  9.  
  10. private static IExtensionProvider m_StoreExtensionProvider;
  11.  
  12. public static string kProductIDSubscription = "subscription";
  13.  
  14. private static string kProductNameAppleSubscription = "com.unity3d.subscription.new";
  15.  
  16. private static string kProductNameGooglePlaySubscription = "com.unity3d.subscription.original";
  17.  
  18. public string[] kPurchaserIdList = {
  19. "com.diffcolor.gaincoins1",
  20. "com.diffcolor.gaincoins2",
  21. "com.diffcolor.gaincoins3",
  22. "com.diffcolor.gaincoins4",
  23. };
  24.  
  25. private static Purchaser Ins;
  26.  
  27. public static Purchaser GetIns ()
  28. {
  29. return Ins;
  30. }
  31.  
  32. void Awake ()
  33. {
  34. Ins = this;
  35. }
  36.  
  37. void Start ()
  38. {
  39. DontDestroyOnLoad (gameObject);
  40. if (m_StoreController == null) {
  41. Debug.Log ("[n]----Purchaser.Start Init");
  42. InitializePurchasing ();
  43. }
  44. }
  45.  
  46. #region init
  47.  
  48. public void InitializePurchasing ()
  49. {
  50. if (IsInitialized ()) {
  51. Debug.Log ("[n]----Purchaser.InitializePurchasing Already.Init");
  52. return;
  53. }
  54.  
  55. Debug.Log ("[n]----Purchaser.InitializePurchasing Begain Init");
  56.  
  57. var builder = ConfigurationBuilder.Instance (StandardPurchasingModule.Instance ());
  58. builder.AddProduct (kProductIDSubscription, ProductType.NonConsumable, new IDs () {
  59. { kProductNameAppleSubscription, AppleAppStore.Name },
  60. { kProductNameGooglePlaySubscription, GooglePlay.Name },
  61. });
  62. //初始化各分类内购 ID
  63. for (int x = 0; x < kPurchaserIdList.Length - 1; x++) {
  64. String purchaseID = kPurchaserIdList [x];
  65. builder.AddProduct (purchaseID, ProductType.Consumable);
  66. }
  67. //初始化广告内购 ID
  68. builder.AddProduct (kPurchaserIdList [kPurchaserIdList.Length - 1], ProductType.NonConsumable);
  69. UnityPurchasing.Initialize (this, builder);
  70. }
  71.  
  72. private bool IsInitialized ()
  73. {
  74. //bool b = m_StoreController != null && m_StoreExtensionProvider != null;
  75. bool b0 = m_StoreController != null;
  76. bool b1 = m_StoreExtensionProvider != null;
  77. bool b = b0 && b1;
  78. Debug.Log ("[n]----Purchaser.IsInitialized b0:" + b0 + " b1:" + b1);
  79. return b;
  80. }
  81.  
  82. public void OnInitialized (IStoreController controller, IExtensionProvider extensions)
  83. {
  84. Debug.Log ("[n]----Purchaser.OnInitialized init CB success ");
  85. m_StoreController = controller;
  86. m_StoreExtensionProvider = extensions;
  87. }
  88.  
  89. public void OnInitializeFailed (InitializationFailureReason error)
  90. {
  91. Debug.Log ("[ne]----Purchaser.OnInitializeFailed init CB faile errer:" + error);
  92.  
  93. }
  94.  
  95. #endregion
  96.  
  97. #region buy
  98.  
  99. public string getPrice (string id)
  100. {
  101. string tstr = null;
  102.  
  103. Product product = m_StoreController.products.WithID (id);
  104.  
  105. if (product != null && product.availableToPurchase) {
  106. tstr = m_StoreController.products.WithID (id).metadata.localizedPriceString.ToString ();
  107. }
  108. Debug.Log ("[n]----Purchaser.getPrice id:" + id + " content:" + tstr);
  109. return tstr;
  110. }
  111.  
  112. public void BuyConsumable (string id)
  113. {
  114. BuyProductID (id);
  115. }
  116.  
  117. void BuyProductID (string productId)
  118. {
  119. if (IsInitialized ()) {
  120. Debug.Log ("[n]----Purchaser.BuyProductID productId:" + productId);
  121. Product product = m_StoreController.products.WithID (productId);
  122. if (product != null && product.availableToPurchase) {
  123. Debug.Log ("[n]----Purchaser.BuyProductID 启动计费 productId:" + product.definition.id);
  124. //Debug.Log(string.Format("Purchasing product asychronously: '{0}'", product.definition.id));
  125. m_StoreController.InitiatePurchase (product);
  126. } else {
  127. if (product == null)
  128. Debug.Log ("[ne]----Purchaser.BuyProductID product==null productId:" + productId);
  129. if (!product.availableToPurchase)
  130. Debug.Log ("[ne]----Purchaser.BuyProductID availableToPurchase==false productId:" + productId);
  131. onPurchaseFailed ("Not Found product.");
  132. }
  133. } else {
  134. Debug.Log ("[ne]----Purchaser.BuyProductID fail,Not Init productId:" + productId);
  135. onPurchaseFailed ("Not initialized.");
  136.  
  137. }
  138.  
  139. }
  140.  
  141. #endregion
  142.  
  143. #region restore
  144.  
  145. public void RestorePurchases ()
  146. {
  147.  
  148. if (!IsInitialized ()) {
  149. Debug.Log ("[n]----Purchaser.RestorePurchases fail,Not initialized");
  150. return;
  151. }
  152.  
  153. if (Application.platform == RuntimePlatform.IPhonePlayer ||
  154. Application.platform == RuntimePlatform.OSXPlayer) {
  155.  
  156. Debug.Log ("[n]----Purchaser.RestorePurchases restoreStart!");
  157. var apple = m_StoreExtensionProvider.GetExtension<IAppleExtensions> ();
  158. apple.RestoreTransactions ((result) => {
  159. if (result) {
  160. Debug.Log ("[n]----Purchaser.RestorePurchases Success!");
  161. GameManager.instance.onRestorePurchasesSuccess ();
  162. } else {
  163. Debug.Log ("[ne]----Purchaser.RestorePurchases Fail!");
  164. GameManager.instance.onRestorePurchasesFailed ();
  165. }
  166. });
  167. } else {
  168. Debug.Log ("[ne]----Purchaser.RestorePurchases platform error current=" + Application.platform);
  169. GameManager.instance.onRestorePurchasesFailed ();
  170. }
  171. }
  172.  
  173. #endregion
  174.  
  175. #region cb
  176.  
  177. //success;
  178. public PurchaseProcessingResult ProcessPurchase (PurchaseEventArgs args)
  179. {
  180.  
  181. Product product = args.purchasedProduct;
  182. Dictionary<string, object> parameters = new Dictionary<string, object> ();
  183. //parameters[AppEventParameterName.ContentID] = product.definition.id;
  184.  
  185. float price = (float)product.metadata.localizedPrice;
  186. string currency = product.metadata.isoCurrencyCode;
  187.  
  188. Debug.Log ("[n]----Purchaser.ProcessPurchase PayCB===>productID:"
  189. + product.definition.id +
  190. " price:" + price
  191. + " currentcy:" + currency);
  192.  
  193. if (String.Equals (args.purchasedProduct.definition.id, kPurchaserIdList [0], StringComparison.Ordinal)) {
  194. GameManager.instance.purchansedCallback (450);
  195. AnalyticsUtils.analyPurchase (1.99f, price, currency, product.definition.id, parameters);
  196. } else if (String.Equals (args.purchasedProduct.definition.id, kPurchaserIdList [1], StringComparison.Ordinal)) {
  197. GameManager.instance.purchansedCallback (2000);
  198. AnalyticsUtils.analyPurchase (7.99f, price, currency, product.definition.id, parameters);
  199. } else if (String.Equals (args.purchasedProduct.definition.id, kPurchaserIdList [2], StringComparison.Ordinal)) {
  200. GameManager.instance.purchansedCallback (5600);
  201. AnalyticsUtils.analyPurchase (20.99f, price, currency, product.definition.id, parameters);
  202. } else if (String.Equals (args.purchasedProduct.definition.id, kPurchaserIdList [3], StringComparison.Ordinal)) {
  203. GameManager.instance.purchansedCallback (15200);
  204. AnalyticsUtils.analyPurchase (53.99f, price, currency, product.definition.id, parameters);
  205. } else if (String.Equals (args.purchasedProduct.definition.id, kPurchaserIdList [4], StringComparison.Ordinal)) {
  206. GameManager.instance.onPurchaseNoAdsSuccess ();
  207. AnalyticsUtils.analyPurchase (2.99f, price, currency, product.definition.id, parameters);
  208. }
  209.  
  210. Debug.Log ("[n]----Purchaser.ProcessPurchase befor data report!!!");
  211.  
  212. //添加购买成功统计
  213. Dictionary<string, object> dict = new Dictionary<string, object> ();
  214. dict.Add ("Level", "Level_" + GameData.instance.cLevel);
  215. dict.Add ("productId", product.definition.id);
  216. AnalyticsUtils.analyticsEvent ("Purchase Success", dict);
  217.  
  218. Debug.Log ("[n]----Purchaser.ProcessPurchase after data report!!!");
  219.  
  220. return PurchaseProcessingResult.Complete;
  221.  
  222. }
  223.  
  224. void onPurchaseFailed (string failureReason)
  225. {
  226. Debug.Log ("[ne]----Purchaser.onPurchaseFailed befor data report!!! errorMsg:" + failureReason);
  227.  
  228. GameManager.instance.onPurchaseFailed ();
  229. Dictionary<string, object> dict = new Dictionary<string, object> ();
  230. dict.Add ("FailureReason", failureReason);
  231. AnalyticsUtils.analyticsEvent ("Purchase Fail", dict);
  232.  
  233. Debug.Log ("[ne]----Purchaser.onPurchaseFailed after data report!!!");
  234. }
  235.  
  236. #endregion
  237.  
  238. #region unuse
  239.  
  240. public void OnPurchaseFailed (Product product, PurchaseFailureReason failureReason)
  241. {
  242. Debug.Log (string.Format ("[ne]----OnPurchaseFailed: FAIL. Product: '{0}', PurchaseFailureReason: {1}", product.definition.storeSpecificId, failureReason));
  243. onPurchaseFailed (string.Format ("Product: '{0}', FailureReason: {1}", product.definition.storeSpecificId, failureReason));
  244. }
  245.  
  246. #endregion
  247. }

  

unity googleplay随手记的更多相关文章

  1. [Unity优化] Unity CPU性能优化

    前段时间本人转战unity手游,由于作者(Chwen)之前参与端游开发,有些端游的经验可以直接移植到手游,比如项目框架架构.代码设计.部分性能分析,而对于移动终端而言,CPU.内存.显卡甚至电池等硬件 ...

  2. Unity随手记

    过年11天假期,带娃带了7天,吃吃喝喝.也看了点书,<射雕英雄传>(书)看了一半,还有就是在看<unity官方案例精讲>这本. 随手记一些自觉有价值或者有意思的点. 1. 对脚 ...

  3. Unity自带IAP插件使用(googleplay)

    https://blog.csdn.net/ar__ha/article/details/64439872 Unity Services里的Unity IAP对于IOS和GooglePlay的支付用这 ...

  4. Unity AssetBundle爬坑手记

    这篇文章从AssetBundle的打包,使用,管理以及内存占用各个方面进行了比较全面的分析,对AssetBundle使用过程中的一些坑进行填补指引以及喷!   AssetBundle是Unity推荐的 ...

  5. (转)Unity AssetBundle爬坑手记

    转自:http://www.cnblogs.com/ybgame/p/3973177.html 这篇文章从AssetBundle的打包,使用,管理以及内存占用各个方面进行了比较全面的分析,对Asset ...

  6. HoloLens开发手记 - Unity之Tracking loss

    当HoloLens设备不能识别到自己在世界中的位置时,应用就会发生tracking loss.默认情况下,Unity会暂停Update更新循环并显示一张闪屏图片给用户.当设备重新能追踪到位置时,闪屏图 ...

  7. HoloLens开发手记 - Unity之Recommended settings 推荐设置

    Unity提供了大量的设置选项来满足全平台的配置,对于HoloLens,Unity可以通过切换一些特定的设置来启用HoloLens特定的行为. Holographic splash screen 闪屏 ...

  8. HoloLens开发手记 - Unity development overview 使用Unity开发概述

    Unity Technical Preview for HoloLens最新发行版为:Beta 24,发布于 09/07/2016 开始使用Unity开发HoloLens应用之前,确保你已经安装好了必 ...

  9. HoloLens开发手记 - Unity之摄像头篇

    当你穿戴好HoloLens后,你就会处在全息应用世界的中心.当你的项目开启了"Virtual Reality Support"选项并选中了"Windows Hologra ...

随机推荐

  1. Pager分页

    分页组件: /// <summary> /// 分页组件 /// </summary> public class PagerHelper { /// <summary&g ...

  2. 机器学习:PCA(人脸识别中的应用——特征脸)

    一.思维理解 X:原始数据集: Wk:原始数据集 X 的前 K 个主成分: Xk:n 维的原始数据降维到 k 维后的数据集: 将原始数据集降维,就是将数据集中的每一个样本降维:X(i) . WkT = ...

  3. HashSet中是如何判断元素是否重复的

    HashSet不能添加重复的元素,当调用add(Object)方法时候, 首先会调用Object的hashCode方法判hashCode是否已经存在,如不存在则直接插入元素: 如果已存在则调用Obje ...

  4. 第十四届华中科技大学程序设计竞赛决赛同步赛 A - Beauty of Trees

    A - Beauty of Trees 题意: 链接:https://www.nowcoder.com/acm/contest/119/A来源:牛客网 Beauty of Trees 时间限制:C/C ...

  5. Python 下载图片的几种方法

    import osos.makedirs('./image/', exist_ok=True)IMAGE_URL = "http://image.nationalgeographic.com ...

  6. DVWA平台v1.9-Brute Force

    Low: 随便输一下用户名,密码,test 点击Login 显示用户名或密码错误 在owasp-zap查看数据包 点击,就会转到这 右键,点击Fuzz 点击Remove删除默认的 选定参数变量值,点击 ...

  7. react-router4.x 组件和api介绍

    react-router实用4.2.0 react-router非常复杂整体,比vue-router强大很多,好好研究,对你自身能力提高很有帮助 安装 cnpm install react-route ...

  8. Linux 对mysql远程授权连接操作 和 查看mysql数据库和表 基本命令

    Linux 对mysql远程连接的授权操作 首先linux连接mysql数据库 授权: grant all on *.* to ' with grant option; //允许账户root从任何主机 ...

  9. js闭包(三)

    场景一:采用函数引用方式的setTimeout调用 闭包的一个通常的用法是为一个在某一函数执行前先执行的函数提供参数.例如,在web环境中,一个函数作为setTimeout函数调用的第一个参数,是一种 ...

  10. 框架之Struts2

    相比较hibernate简单了许多 案例:使用Struts2框架完成登录功能 需求分析 1. 使用Struts2完成登录的功能 技术分析之Struts2框架的概述 1. 什么是Struts2的框架 * ...