找回密码
 立即注册
查看: 1434|回复: 6

[简易教程] Unity 接入苹果内购 (IAP)

[复制链接]
发表于 2019-7-30 15:57 | 显示全部楼层 |阅读模式
苹果官网相关配置1. 新建要购买的项目,点击旁边的+号 开始创建要进行内购item(在此之前要确保税务信息填写完成)
AppStoreConnect官网地址



image

内购一共四种类型,要注意的是非消耗品类型只能购买一次 并且  任何类型的产品ID 如果之前在这个项目中创建过,之后删除也不能再使用
设置完成后选择存储,退回到之前界面状态若现实 准备提交 表示里面需要填的东西都填全了 如果显示的是 元数据丢失 一般是截图没有上传 或者其他数据 例如简介之类的没有填写
物品ID本人的命名规则是 bundleid.itemID 例如 com.testCompany.testApp.Gold1000 或 com.testCompany.testApp.item001
若创建完成后 item最后显示表示  元数据丢失 表示有描述、或者示意图之类的没有添加
2 创建沙盒测试账号



image




image




image

邮箱要随便输入一个不存在的邮箱  所在区域最好填中国,否则在购买界面等等弹出的是所在国家的语言  填写完成后记下邮箱和密码
再次进入我的APP,要注意的是 如果这一条没有操作 支付不会成功



image

如果没有 APP内购项目这一条 检查创建的内购内容是否显示元数据丢失 ,添加截图或描述信息 内购条目显示 准备提交时 说名修改完成,如下图。



image

继续之前的操作,点开加号 添加要加入的内购项目



image




image

到此APPstoreConnect配置完成
unity端配置
需要unity版本:5.5.x
** 打开unity后 点击 window --> services**



image




image

点击 In_App Purchaing



image

一路打开后,最后会是这样。 中间会有一个选项,是是否是为12岁以下儿童设计的 遇到直接continue就好
到这里点击import 会导入所需要的包
注意下方的红框 是谷歌商店支付相关的。直接空着不用管它 先上一个哥们的参考
当时接入是的参考帖子,写的很详细了
下面是实际操作部分,先上代码
using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.Purchasing;using System;#if UNITY_IOSpublic class MyStoreClass : MonoBehaviour, IStoreListener{    private static MyStoreClass _instance;    public static MyStoreClass Instance{get{return _instance;}}    private IStoreController m_controller;    public ChargeWindow m_chargeWindow;    private IExtensionProvider m_extensions;    private bool IsInitialized()    {        return m_controller != null && m_extensions != null;    }    void Awake()    {        if (_instance == null)            _instance = this;    }    public void InitIdData(List<PayData> dataList)    {#if UNITY_IOS        ConfigurationBuilder builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());        foreach (var v in dataList)        {            ProductType type = ProductType.Consumable;            if (v.m_glod == 0)                type = ProductType.NonConsumable;            builder.AddProduct(v.m_id, ProductType.Consumable);        }        UnityPurchasing.Initialize(this, builder);#endif    }    //初始化完成    public void OnInitialized(IStoreController controller, IExtensionProvider extensions)    {        this.m_controller = controller;        m_extensions = extensions;        RestorePurchases();        Debug.Log(string.Format("iap_test OnInitialized"));    }    public void OnInitializeFailed(InitializationFailureReason error)    {        Debug.Log(string.Format("iap_test init failed error: {0}",error));    }    public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e)    {        string order=TakingDataManage.GetOrderByItemId(e.purchasedProduct.definition.id);        TakingDataManage.OnPlayerStartCharge(order,e.purchasedProduct.definition.id,0,"CNY",0,TakingDataManage.PaymentType.applePay);        TakingDataManage.OnPlayerChargeSucess(order);        Debug.Log(string.Format("ipa_test purchase sucess id {0} ",e.purchasedProduct.definition.id));        GameManager.GetManager().GetGameModuleManager().GetModule<PayModule>().PauchaseSucess(e.purchasedProduct.definition.id);        return PurchaseProcessingResult.Complete;    }    public void OnPurchaseFailed(Product item, PurchaseFailureReason r)    {        string order=TakingDataManage.GetOrderByItemId(item.definition.id);        TakingDataManage.OnPlayerStartCharge(order,item.definition.id,0,"CNY",0,TakingDataManage.PaymentType.applePay);        GameManager.GetManager().GetGameModuleManager().GetModule<PayModule>().PauchaseFail(r);        Debug.Log(string.Format("ipa_test purchase failed id {0} ,error type is {1}", item.definition.id,r.ToString()));    }    public void StartPurchase(PayData data ,ChargeWindow c)    {        string productId = data.m_id;        if (c == null)            m_chargeWindow = c;        if (IsInitialized())        {            Product product = m_controller.products.WithID(productId);            if (product != null && product.availableToPurchase)            {                Debug.Log(string.Format("Purchasing product asychronously: '{0}'", product.definition.id));                m_controller.InitiatePurchase(product);            }            else            {                Debug.Log("BuyProductID: FAIL. Not purchasing product, either is not found or is not available for purchase");            }        }        else        {            Debug.Log("BuyProductID FAIL. Not initialized.");        }    }    public void RestorePurchases()    {        if (!IsInitialized())        {            Debug.Log("RestorePurchases FAIL. Not initialized.");            return;        }        if (Application.platform == RuntimePlatform.IPhonePlayer ||            Application.platform == RuntimePlatform.OSXPlayer)        {            Debug.Log("RestorePurchases started ...");            var apple = m_extensions.GetExtension<IAppleExtensions>();            apple.RestoreTransactions((result) =>            {                //返回一个bool值,如果成功,则会多次调用支付回调,然后根据支付回调中的参数得到商品id,最后做处理(ProcessPurchase)                Debug.Log("RestorePurchases continuing: " + result + ". If no further messages, no purchases available to restore.");            });        }        else        {            Debug.Log("RestorePurchases FAIL. Not supported on this platform. Current = " + Application.platform);        }    }}#endif代码比较简单
大概就是 在InitIdData函数里,把 购买的条目放入 builder.AddProduct(v.m_id, ProductType.Consumable);
两个参数分别是 string 和ProductType ,分别是 在appStoreConnect里设置的id和消耗品类型,之后进行实例化, OnInitialized函数。
这里在实例化成功后执行了 RestorePurchases,是因为手里的是单机项目,所以每次进入后需要确认已经购买过的unComsumble类型的道具。
在测试过程中遇到 内购完成后没有的带回调,切弹出 该订单已购买,请恢复数据(大概是这个意思,具体记不清了)
大概是因为 此物品已经购买成功了,但是苹果没有接受到app的消息 这个时候需要执行 RestorePurchases
他会把你没有解决的订单和一次性购买项目再次拉去一遍,他会执行 ProcessPurchase 这个回调函数
要注意的是 同时最好只发起一个支付的请求,在拉起支付后不要让玩家再点击购买的按钮。否则很容易导致上面的情况
还有就是在沙盒测试的时候 拉起会比较慢,有同学说是应为苹果的沙盒服务器不是很稳定,连接比较慢 耐心多等一会。
关于沙盒测试,需要首先推出手机的icloud账号,安装app拉起支付之后,在这个时候用其他账号登录,登入沙盒测试账号进行支付。



作者:Charon_ted
链接:https://www.jianshu.com/p/b9f5744d3f9f
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
 楼主| 发表于 2019-7-30 15:57 | 显示全部楼层
发表于 2020-4-26 08:51 | 显示全部楼层
很不错
发表于 2020-4-26 08:38 | 显示全部楼层
楼主是超人
发表于 2020-4-26 08:53 | 显示全部楼层
好帖就是要顶
发表于 2020-4-26 08:23 | 显示全部楼层
说的非常好
发表于 2020-4-26 08:19 | 显示全部楼层
不错不错
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-4-26 20:58 , Processed in 0.068591 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表