Unity自带IAP插件使用(googleplay)
https://blog.csdn.net/ar__ha/article/details/64439872
Unity Services里的Unity IAP对于IOS和GooglePlay的支付用这个插件就足够了。
1.集成插件
Window-Services(Ctrl+0)在Services面板Link你的工程,启用In-App Purchase,Import/Update一下,插件就在Assets/Plugins/UnityPurchasing下。
2.设置
以GooglePlay的设置为例
Window - Unity Iap - Android - Target Google Play :选择GooglePlay,
Window - Unity Iap - Receipt Validation Obfuscator :输入GooglePlay的PublicKey,
点击Obfuscate secrets后,在Assets/Plugins/UnityPurchasing/generated下会生产GooglePlayTangle自动生成的文件,不用管他。
但是要注意到他的宏定义
#if UNITY_ANDROID || UNITY_IPHONE || UNITY_STANDALONE_OSX || UNITY_TVOS
3.DEMO
插件的demo在Assets/Plugins/UnityPurchasing/scenes/IAP Demo 这个场景里
也可以直接看cs文件,Assets/Plugins/UnityPurchasing/script/IAPDemo.cs
主要用到的是UnityEngine.Purchasing.IStoreListener 这个接口
4.初始化
- //使用这个解析IAP成功后的receipt
- private UnityEngine.Purchasing.Security.CrossPlatformValidator validator;
- private void InitUnityPurchase() {
- var module = StandardPurchasingModule.Instance();
- var builder = ConfigurationBuilder.Instance (module);
- //添加计费点
- // UnityEngine.Purchasing.ProductType
- builder.AddProduct("item1", ProductType.Consumable, new IDs
- {
- {"苹果计费点", AppleAppStore.Name },
- {"谷歌计费点", GooglePlay.Name}
- }
- );
- #if !UNITY_EDITOR
- validator = new CrossPlatformValidator(GooglePlayTangle.Data(), AppleTangle.Data(), Application.bundleIdentifier);
- #endif
- UnityPurchasing.Initialize (this, builder);
- }
- private IStoreController m_Controller;
- //UNITY IAP初始化成功
- public void OnInitialized (IStoreController controller, IExtensionProvider extensions) {
- m_Controller = controller;
- // On Apple platforms we need to handle deferred purchases caused by Apple's Ask to Buy feature.
- // On non-Apple platforms this will have no effect; OnDeferred will never be called.
- var m_AppleExtensions = extensions.GetExtension<IAppleExtensions> ();
- m_AppleExtensions.RegisterPurchaseDeferredListener(OnDeferred);
- var product = m_Controller.products.WithID("item1");
- //价格 (带货币单位的字符串)
- var priceString = product.metadata.localizedPriceString;
- //价格 (换算汇率后的价格)
- var price = product.metadata.localizedPrice;
- }
- //初始化失败(没有网络的情况下并不会调起,而是一直等到有网络连接再尝试初始化)
- public void OnInitializeFailed (InitializationFailureReason error) {
- Debug.Log("Billing failed to initialize!");
- switch (error) {
- case InitializationFailureReason.AppNotKnown:
- Debug.LogError("Is your App correctly uploaded on the relevant publisher console?");
- break;
- case InitializationFailureReason.PurchasingUnavailable:
- // Ask the user if billing is disabled in device settings.
- Debug.Log("Billing disabled!");
- break;
- case InitializationFailureReason.NoProductsAvailable:
- // Developer configuration error; check product metadata.
- Debug.Log("No products available for purchase!");
- break;
- }
- }
- public void DoIapPurchase (Action<bool, string> callback) {
- if (m_Controller != null) {
- var product = m_Controller.products.WithID ("item1");
- if (product != null && product.availableToPurchase) {
- //调起支付
- m_Controller.InitiatePurchase(product);
- }
- else {
- callback (false, "no available product");
- }
- }
- else {
- callback ( false, "m_Controller is null");
- }
- }
实现IStoreListener支付回调
- public PurchaseProcessingResult ProcessPurchase (PurchaseEventArgs e) {
- try {
- var result = validator.Validate (e.purchasedProduct.receipt);
- Debug.Log ("Receipt is valid. Contents:");
- foreach (IPurchaseReceipt productReceipt in result) {
- Debug.Log(productReceipt.productID);
- Debug.Log(productReceipt.purchaseDate);
- Debug.Log(productReceipt.transactionID);
- AppleInAppPurchaseReceipt apple = productReceipt as AppleInAppPurchaseReceipt;
- if (null != apple) {
- Debug.Log(apple.originalTransactionIdentifier);
- Debug.Log(apple.subscriptionExpirationDate);
- Debug.Log(apple.cancellationDate);
- Debug.Log(apple.quantity);
- //如果有服务器,服务器用这个receipt去苹果验证。
- var receiptJson = JSONObject.Parse(e.purchasedProduct.receipt);
- var receipt = receiptJson.GetString("Payload");
- }
- GooglePlayReceipt google = productReceipt as GooglePlayReceipt;
- if (null != google) {
- Debug.Log(google.purchaseState);
- Debug.Log(google.purchaseToken);
- }
- }
- return PurchaseProcessingResult.Complete;
- } catch (IAPSecurityException) {
- Debug.Log("Invalid receipt, not unlocking content");
- return PurchaseProcessingResult.Complete;
- }
- return PurchaseProcessingResult.Complete;
- }
失败:
- public void OnPurchaseFailed(Product i, PurchaseFailureReason p) {
- Logger.Warning("purchase failed of reason : " + p.ToString());
- }
IOS deferred:
- /// <summary>
- /// iOS Specific.
- /// This is called as part of Apple's 'Ask to buy' functionality,
- /// when a purchase is requested by a minor and referred to a parent
- /// for approval.
- ///
- /// When the purchase is approved or rejected, the normal purchase events
- /// will fire.
- /// </summary>
- /// <param name="item">Item.</param>
- private void OnDeferred(Product item)
- {
- Logger.Warning("Purchase deferred: " + item.definition.id);
- }
Unity自带IAP插件使用(googleplay)的更多相关文章
- unity自带寻路Navmesh入门教程(一)
说明:从今天开始,我阿赵打算写一些简单的教程,方便自己日后回顾,或者方便刚入门的朋友学习.水平有限请勿见怪.不过请尊重码字截图录屏的劳动,如需转载请先告诉我.谢谢! unity自从3.5版本之后,增加 ...
- 【转载】利用Unity自带的合图切割功能将合图切割成子图
虽然目前网上具有切割合图功能的工具不少,但大部分都是自动切割或者根据plist之类的合图文件切割的, 这种切割往往不可自己微调或者很难维调,导致效果不理想. 今天逛贴吧发现了一位网友写的切割合图插件很 ...
- 【Unity笔记】常用插件
记录一些常见插件,随时补充. iTween动画插件 原理:插值法,给出初始值和终点值,自动算出中间值. DoTween Tween动画 Playmaker $45 Playmaker由第三方软件商Hu ...
- 【转】unity自带寻路Navmesh入门教程(一)
http://liweizhaolili.blog.163.com/blog/static/16230744201271161310135/ 说明:从今天开始,我阿赵打算写一些简单的教程,方便自己日后 ...
- Android Studio如何导出可供Unity使用的aar插件详解
http://www.cnblogs.com/xtqqkss/p/6387271.html 前言 项目之前使用Eclipse导出的jar文件来做与Android交互,最近因为工作需要需使用Androi ...
- Unity自带网络功能——NetworkView组件、Serialize、RPC
Unity拥有大量的第三方插件,专门提供了对网络功能的支持.可是,大部分开发人员第一次接触到的还是Unity自带的网络功能,也就是大家常常说到的Unity Networking API.这些API是借 ...
- UNITY自带的PACKAGE的UTILITY 里面有一个自带的FPS COUNTER
UNITY自带的PACKAGE的UTILITY 里面有一个自带的FPS COUNTER 可用,但是脚本是保密的?
- unity自带寻路Navmesh入门教程(三)
继续介绍NavMesh寻路的功能,接下来阿赵打算讲一下以下两个例子,先看看完成的效果: 第一个例子对于喜欢DOTA的朋友应该很熟悉了,就是不同小队分不同路线进攻的寻路,红绿蓝三个队伍分别根据三条路 ...
- 【Unity3D】Unity自带组件—完成第一人称人物控制
1.导入unity自带的Character Controllers包 2.可以看到First Person Controller组件的构成 Mouse Look() : 随鼠标的移动而使所属物体发生旋 ...
随机推荐
- Order By操作
本文转载自:http://www.cnblogs.com/vingi/articles/2450482.html 适用场景:对查询出的语句进行排序,比如按时间排序等等. 说明:按指定表达式对集合排序: ...
- 引用 WCF 服务后,没有生成任何 .datasource?
如题WCF服务生成成功,在添加服务引用的时候也不报错,但是添加完成之后不能正常调用服务借口. 在重新引用服务的时候,或者是是更新引用服务的时候,点“高级”按钮,在服务引用设置对话框中,将“重新使用引用 ...
- [置顶]
if语句的陷阱
if else if是只要有满足条件的,就不再对之后的else if进行判断 #include<stdio.h> void main() { char a=0; if(a==0) { a= ...
- 开发环境入门 linux基础(部分)虚拟内存,rpm和yum安装
虚拟内存,rpm和yum安装 文本中查找 /内容 替换:扩展模式下(:)%s /替换目标/要替换的文件/ (只替换第一个)(后边加g全部替换) :set u添加行号 raid lvm逻辑卷 df - ...
- python3 中 requests 框架
原文的文件地址:http://blog.csdn.net/shanzhizi/article/details/50903748 一.安装 Requests 通过pip安装 pip install re ...
- Python类(六)-静态方法、类方法、属性方法
静态方法 通过@staticmethod来定义,静态方法在类中,但在静态方法里访问不了类和实例中的属性,但静态方法需要类来调用 # -*- coding:utf-8 -*- __author__ = ...
- NSURLConnection基本用法(苹果原生)
一.NSURLConnection的常用类 (1)NSURL:请求地址 (2)NSURLRequest/NSMutableURLRequest:封装一个请求,保存发给服务器的全部数据,包括一个NSUR ...
- NIO/AIO
1 NIO NEW IO 的简称,新的java I/O标准,jdk1.4纳入,有以下特点: (1)NIO基于block块,以块(硬盘数据块)为单位处理数据,比旧式的io基于流的效率高. (2)为各个基 ...
- 数字图像处理实验(5):Proj03-01 ~ Proj03-06 标签: 图像处理matlab 2017-04-30 10:39 184人阅读
PROJECT 03-01 : Image Enhancement Using Intensity Transformations 实验要求: Objective To manipulate a te ...
- 孕妇的孕周和体重对胎儿游离DNA在母体cfDNA占比的影响
有一篇文件,研究了孕妇的孕周和体重对 胎儿游离DNA在母体cfDNA中占比的影响 文章名称:Gestational age and maternal weight effects on fetal c ...