欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

在Unity中加入IOS内购(In-App Purchasing)

程序员文章站 2022-12-03 14:45:22
在Unity中加入IOS内购(In-App Purchasing),编写 内购类,在需要的的地方调用相应方法。 using UnityEngine; using Unity...

在Unity中加入IOS内购(In-App Purchasing),编写 内购类,在需要的的地方调用相应方法。

using UnityEngine;
using UnityEngine.Purchasing;

public class InAppPurchasing : MonoBehaviour,IStoreListener
{
    //IAP组件相关的对象,m_Controller里存储着商品信息
    private static IStoreController m_Controller;//存储商品信息
    private static  IAppleExtensions 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());
    }
}