Unity|Unity 3D(加入IOS内购(In-App Purchasing))
编写 内购类,在需要的的地方调用相应方法。
using UnityEngine;
using UnityEngine.Purchasing;
public class InAppPurchasing : MonoBehaviour,IStoreListener
{
//IAP组件相关的对象,m_Controller里存储着商品信息
private static IStoreController m_Controller;
//存储商品信息
private staticIAppleExtensions m_AppleExtensions;
private static bool PurchaseAvailable = true;
//IAP可用状态
private static bool InternetAvailable;
//是否初始化成功public static InAppPurchasing instance;
void Awake()
{
instance = this;
}
void OnEnable()
{
//如果没有连击网络,关闭IAP功能
if (Application.internetReachability == NetworkReachability.NotReachable)
{
PurchaseAvailable = false;
}
else
{
PurchaseAvailable = true;
//如果没有初始化成功
if (InternetAvailable == false)
{
InitUnityPurchase();
//初始化
}
}
}
// Use this for initialization
void Start()
{}
// Update is called once per frame
void Update()
{}public void InitUnityPurchase()//初始化方法
{
var module = StandardPurchasingModule.Instance();
//标准采购模块
var builder = ConfigurationBuilder.Instance(module);
//配置模式
//添加多个ID到builder里,ID命名方式建议字母+数字结尾,比如ShuiJing_1,ShuiJing_2,
//注意ProductType的类型,Consumable是可以无限购买(比如水晶),NonConsumable是只能购买一次(比如关卡),Subscription是每月订阅(比如VIP)
for (int i = 0;
i < 商品数量;
i++)
{
builder.AddProduct("你的商品ID_" + i.ToString(), ProductType.Consumable);
}
//开始初始化
UnityPurchasing.Initialize(this, builder);
}
public void OnInitialized(IStoreController controller, IExtensionProvider extensions)//初始化成功回调
{
m_Controller = controller;
m_AppleExtensions = extensions.GetExtension();
m_AppleExtensions.RegisterPurchaseDeferredListener(OnDeferred);
//登记 购买延迟 监听器
InternetAvailable = true;
//初始化成功
Debug.Log("IAP初始化成功");
}
public void OnInitializeFailed(InitializationFailureReason error)//初始化失败回调
{
InternetAvailable = false;
if (error == InitializationFailureReason.PurchasingUnavailable)
{
print("手机设置了禁止APP内购");
}
Debug.Log("IAP初始化失败");
}
public void OnPurchaseFailed(Product item, PurchaseFailureReason r)//购买失败回调
{
print("购买失败");
}public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e)//购买成功回调
{
return 0;
//complete 购买完成
return 1;
//pending 未购买
}private void OnDeferred(Product item)//购买延迟提示
{
print("网速慢");
}public void OnPurchaseClicked(int ButtonId)//发起购买函数,在你的商品按钮上拖这个方法进入
{
m_Controller.InitiatePurchase("你的商品ID_" + ButtonId.ToString());
}
}
参考自Unity3d百度贴吧
【Unity|Unity 3D(加入IOS内购(In-App Purchasing))】
推荐阅读
- 2020-04-07vue中Axios的封装和API接口的管理
- iOS中的Block
- 感恩遇见长投-让我重新审视自己
- 记录iOS生成分享图片的一些问题,根据UIView生成固定尺寸的分享图片
- 2019-08-29|2019-08-29 iOS13适配那点事
- Hacking|Hacking with iOS: SwiftUI Edition - SnowSeeker 项目(一)
- iOS面试题--基础
- Unity和Android通信系列文章2——扩展UnityPlayerActivity
- 心蓝六月复盘
- 接口|axios接口报错-参数类型错误解决