https://blog.csdn.net/ar__ha/article/details/64439872

Unity Services里的Unity IAP对于IOS和GooglePlay的支付用这个插件就足够了。

Unity官方文档

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.初始化

  1.  
    //使用这个解析IAP成功后的receipt
  2.  
    private UnityEngine.Purchasing.Security.CrossPlatformValidator validator;
  3.  
     
  4.  
     
  5.  
    private void InitUnityPurchase() {
  6.  
    var module = StandardPurchasingModule.Instance();
  7.  
    var builder = ConfigurationBuilder.Instance (module);
  8.  
     
  9.  
    //添加计费点
  10.  
    // UnityEngine.Purchasing.ProductType
  11.  
    builder.AddProduct("item1", ProductType.Consumable, new IDs
  12.  
    {
  13.  
    {"苹果计费点", AppleAppStore.Name },
  14.  
    {"谷歌计费点", GooglePlay.Name}
  15.  
    }
  16.  
    );
  17.  
    #if !UNITY_EDITOR
  18.  
    validator = new CrossPlatformValidator(GooglePlayTangle.Data(), AppleTangle.Data(), Application.bundleIdentifier);
  19.  
    #endif
  20.  
    UnityPurchasing.Initialize (this, builder);
  21.  
    }
 
实现IStoreListener 接口初始化回调
 
成功:
  1.  
    private IStoreController m_Controller;
  2.  
     
  3.  
     
  4.  
    //UNITY IAP初始化成功
  5.  
    public void OnInitialized (IStoreController controller, IExtensionProvider extensions) {
  6.  
    m_Controller = controller;
  7.  
     
  8.  
    // On Apple platforms we need to handle deferred purchases caused by Apple's Ask to Buy feature.
  9.  
    // On non-Apple platforms this will have no effect; OnDeferred will never be called.
  10.  
    var m_AppleExtensions = extensions.GetExtension<IAppleExtensions> ();
  11.  
    m_AppleExtensions.RegisterPurchaseDeferredListener(OnDeferred);
  12.  
     
  13.  
    var product = m_Controller.products.WithID("item1");
  14.  
    //价格 (带货币单位的字符串)
  15.  
    var priceString = product.metadata.localizedPriceString;
  16.  
    //价格 (换算汇率后的价格)
  17.  
    var price = product.metadata.localizedPrice;
  18.  
    }
失败:
  1.  
    //初始化失败(没有网络的情况下并不会调起,而是一直等到有网络连接再尝试初始化)
  2.  
    public void OnInitializeFailed (InitializationFailureReason error) {
  3.  
     
  4.  
    Debug.Log("Billing failed to initialize!");
  5.  
    switch (error) {
  6.  
    case InitializationFailureReason.AppNotKnown:
  7.  
    Debug.LogError("Is your App correctly uploaded on the relevant publisher console?");
  8.  
    break;
  9.  
    case InitializationFailureReason.PurchasingUnavailable:
  10.  
    // Ask the user if billing is disabled in device settings.
  11.  
    Debug.Log("Billing disabled!");
  12.  
    break;
  13.  
    case InitializationFailureReason.NoProductsAvailable:
  14.  
    // Developer configuration error; check product metadata.
  15.  
    Debug.Log("No products available for purchase!");
  16.  
    break;
  17.  
    }
  18.  
    }
 
5.发起支付
  1.  
    public void DoIapPurchase (Action<bool, string> callback) {
  2.  
    if (m_Controller != null) {
  3.  
    var product = m_Controller.products.WithID ("item1");
  4.  
    if (product != null && product.availableToPurchase) {
  5.  
    //调起支付
  6.  
    m_Controller.InitiatePurchase(product);
  7.  
    }
  8.  
    else {
  9.  
    callback (false, "no available product");
  10.  
    }
  11.  
    }
  12.  
    else {
  13.  
    callback ( false, "m_Controller is null");
  14.  
    }
  15.  
    }
 

实现IStoreListener支付回调

 
成功:
  1.  
    public PurchaseProcessingResult ProcessPurchase (PurchaseEventArgs e) {
  2.  
    try {
  3.  
    var result = validator.Validate (e.purchasedProduct.receipt);
  4.  
    Debug.Log ("Receipt is valid. Contents:");
  5.  
    foreach (IPurchaseReceipt productReceipt in result) {
  6.  
    Debug.Log(productReceipt.productID);
  7.  
    Debug.Log(productReceipt.purchaseDate);
  8.  
    Debug.Log(productReceipt.transactionID);
  9.  
     
  10.  
    AppleInAppPurchaseReceipt apple = productReceipt as AppleInAppPurchaseReceipt;
  11.  
    if (null != apple) {
  12.  
    Debug.Log(apple.originalTransactionIdentifier);
  13.  
    Debug.Log(apple.subscriptionExpirationDate);
  14.  
    Debug.Log(apple.cancellationDate);
  15.  
    Debug.Log(apple.quantity);
  16.  
     
  17.  
     
  18.  
    //如果有服务器,服务器用这个receipt去苹果验证。
  19.  
    var receiptJson = JSONObject.Parse(e.purchasedProduct.receipt);
  20.  
    var receipt = receiptJson.GetString("Payload");
  21.  
    }
  22.  
     
  23.  
    GooglePlayReceipt google = productReceipt as GooglePlayReceipt;
  24.  
    if (null != google) {
  25.  
    Debug.Log(google.purchaseState);
  26.  
    Debug.Log(google.purchaseToken);
  27.  
    }
  28.  
    }
  29.  
    return PurchaseProcessingResult.Complete;
  30.  
    } catch (IAPSecurityException) {
  31.  
    Debug.Log("Invalid receipt, not unlocking content");
  32.  
    return PurchaseProcessingResult.Complete;
  33.  
    }
  34.  
    return PurchaseProcessingResult.Complete;
  35.  
    }

失败:

  1.  
    public void OnPurchaseFailed(Product i, PurchaseFailureReason p) {
  2.  
    Logger.Warning("purchase failed of reason : " + p.ToString());
  3.  
    }

IOS deferred:

  1.  
    /// <summary>
  2.  
    /// iOS Specific.
  3.  
    /// This is called as part of Apple's 'Ask to buy' functionality,
  4.  
    /// when a purchase is requested by a minor and referred to a parent
  5.  
    /// for approval.
  6.  
    ///
  7.  
    /// When the purchase is approved or rejected, the normal purchase events
  8.  
    /// will fire.
  9.  
    /// </summary>
  10.  
    /// <param name="item">Item.</param>
  11.  
    private void OnDeferred(Product item)
  12.  
    {
  13.  
    Logger.Warning("Purchase deferred: " + item.definition.id);
  14.  
    }

Unity自带IAP插件使用(googleplay)的更多相关文章

  1. unity自带寻路Navmesh入门教程(一)

    说明:从今天开始,我阿赵打算写一些简单的教程,方便自己日后回顾,或者方便刚入门的朋友学习.水平有限请勿见怪.不过请尊重码字截图录屏的劳动,如需转载请先告诉我.谢谢! unity自从3.5版本之后,增加 ...

  2. 【转载】利用Unity自带的合图切割功能将合图切割成子图

    虽然目前网上具有切割合图功能的工具不少,但大部分都是自动切割或者根据plist之类的合图文件切割的, 这种切割往往不可自己微调或者很难维调,导致效果不理想. 今天逛贴吧发现了一位网友写的切割合图插件很 ...

  3. 【Unity笔记】常用插件

    记录一些常见插件,随时补充. iTween动画插件 原理:插值法,给出初始值和终点值,自动算出中间值. DoTween Tween动画 Playmaker $45 Playmaker由第三方软件商Hu ...

  4. 【转】unity自带寻路Navmesh入门教程(一)

    http://liweizhaolili.blog.163.com/blog/static/16230744201271161310135/ 说明:从今天开始,我阿赵打算写一些简单的教程,方便自己日后 ...

  5. Android Studio如何导出可供Unity使用的aar插件详解

    http://www.cnblogs.com/xtqqkss/p/6387271.html 前言 项目之前使用Eclipse导出的jar文件来做与Android交互,最近因为工作需要需使用Androi ...

  6. Unity自带网络功能——NetworkView组件、Serialize、RPC

    Unity拥有大量的第三方插件,专门提供了对网络功能的支持.可是,大部分开发人员第一次接触到的还是Unity自带的网络功能,也就是大家常常说到的Unity Networking API.这些API是借 ...

  7. UNITY自带的PACKAGE的UTILITY 里面有一个自带的FPS COUNTER

    UNITY自带的PACKAGE的UTILITY 里面有一个自带的FPS COUNTER 可用,但是脚本是保密的?

  8. unity自带寻路Navmesh入门教程(三)

    继续介绍NavMesh寻路的功能,接下来阿赵打算讲一下以下两个例子,先看看完成的效果:   第一个例子对于喜欢DOTA的朋友应该很熟悉了,就是不同小队分不同路线进攻的寻路,红绿蓝三个队伍分别根据三条路 ...

  9. 【Unity3D】Unity自带组件—完成第一人称人物控制

    1.导入unity自带的Character Controllers包 2.可以看到First Person Controller组件的构成 Mouse Look() : 随鼠标的移动而使所属物体发生旋 ...

随机推荐

  1. Angular5学习笔记 - 配置NG-ZORRO(八)

    一.在项目中集成组件 $ cd PROJECT_NAME $ npm install ng-zorro-antd --save 二.在项目中导入组件 直接用下面的代码替换 /src/app/app.m ...

  2. C# Chat曲线图,在发布之后出现错误 Invalid temp directory in chart handler configuration c:\TempImageFiles\

    First error message: Invalid temp directory in chart handler configuration c:\TempImageFiles\ Soluti ...

  3. HP 防止cciss设备被DM映射

    http://h10025.www1.hp.com/ewfrf/wc/document?cc=cn&lc=zh-hans&dlc=zh-hans&docname=c034933 ...

  4. c# pictureBox 循环播放图片

    c# 1.遍历目录 查找图片 2.在 pictureBox 循环播放 public void PlayThread()//CMD_UpdateBtnStatus cmd { Int32 framera ...

  5. MySQL 数据库 练习题

    一.表关系 请创建如下表,并创建相关约束 二.操作表 1.自行创建测试数据 2.查询“生物”课程比“物理”课程成绩高的所有学生的学号: 3.查询平均成绩大于60分的同学的学号和平均成绩: 4.查询所有 ...

  6. webapi中使用token验证(JWT验证)

    本文介绍如何在webapi中使用JWT验证 准备 安装JWT安装包 System.IdentityModel.Tokens.Jwt 你的前端api登录请求的方法,参考 axios.get(" ...

  7. Centos 7.2 安装稳定版 nginx

    1. 创建适用于RHEL/CentOS系统的安装源文件,位置为: /etc/yum.repos.d/nginx.repo , 并写入以下内容: [nginx] name=nginx repo base ...

  8. 通过id查询出图片

    第一步,model中需要如下的做法 [UIHint("Picture")] //加上之后会默认显示上传图片的模式 public int PictrueId { get; set; ...

  9. eclipse和myeclipse的下载地址

    官方下载地址: Eclipse 标准版 x86 http://mirror.hust.edu.cn/eclipse//technology/epp/downloads/release/luna/R/e ...

  10. DAY8-python之网络编程

    一.客户端/服务器架构 1.硬件C/S架构(打印机) 2.软件C/S架构 互联网中处处是C/S架构 如黄色网站是服务端,你的浏览器是客户端(B/S架构也是C/S架构的一种) 腾讯作为服务端为你提供视频 ...