美文网首页
使用Unity3d内置IAP插件 实现IOS和Google Pl

使用Unity3d内置IAP插件 实现IOS和Google Pl

作者: _ArayA_ | 来源:发表于2019-10-31 10:47 被阅读0次

    现在越来越多的游戏需要全球化运营.其中支付这块,最重要的就是IOS和Google Play是必须要支持的.

    一般要实现的需求如下:

    • 支持Google Play和IOS道具支付
    • 根据本地货币转换汇率
    • 支持游戏内订阅服务
    • 内购重置功能

    下面我来用U3D内置IAP逐条实现上述功能.

    • 支持Google Play和IOS道具支付
      基础功能
        /**
         *  Description: 根据ID购买商品
         *  productID: 商品ID
         */
        public void BuyProductID(string productID)
        {
            if (m_PurchaseInProgress)
            {
                Debug.Log("Please wait, purchase in progress");
                return;
            }
    
            if (m_Controller == null)
            {
                Debug.LogError("Purchasing is not initialized");
                return;
            }
    
            if (m_Controller.products.WithID(productID) == null)
            {
                Debug.LogError("No product has id " + productID);
                return;
            }
    
            // For platforms needing Login, games utilizing a connected backend
            // game server may wish to login.
            // Standalone games may not need to login.
            if (m_IsLoggedIn == false)
            {
                Debug.LogWarning("Purchase notifications will not be forwarded server-to-server. Login incomplete.");
            }
            // Don't need to draw our UI whilst a purchase is in progress.
            // This is not a requirement for IAP Applications but makes the demo
            // scene tidier whilst the fake purchase dialog is showing.
            m_PurchaseInProgress = true;
    
            //Sample code how to add accountId in developerPayload to pass it to getBuyIntentExtraParams
            //Dictionary<string, string> payload_dictionary = new Dictionary<string, string>();
            //payload_dictionary["accountId"] = "Faked account id";
            //payload_dictionary["developerPayload"] = "Faked developer payload";
            //m_Controller.InitiatePurchase(m_Controller.products.WithID(productID), MiniJson.JsonEncode(payload_dictionary));
            m_Controller.InitiatePurchase(m_Controller.products.WithID(productID), "developerPayload");
    

    购买时直接调用即可

     IAPManager.THIS.BuyProductID("test.pay4");
    
    • 根据本地货币转换汇率
        /**
         *  Description: 获取商品信息
         *  key:商品id
         */
        public Product GetProduct(string key)
        {
            if (m_Controller == null)
            {
                Debug.LogError("Purchasing is not initialized");
                return null;
            }
            if (productDic == null)
            {
                productDic = new Dictionary<string, Product>();
                var products = m_Controller.products.all;
                foreach (var product in products)
                {
                    productDic[product.definition.id] = product;
                }
            }
            return productDic[key];
        }
    

    其中Product.metadata.localizedPriceString 就是本地货币单位.

    • 支持游戏内订阅服务

    订阅商品与一般商品购买上没有区别,正常购买就好了。
    注意要设置好商品类型,开发者账号后台建立订阅型商品。
    订阅型商品分两种,自动续订和非自动续订的,一般都是用自动续订(You know why!)。
    促销,如1个月的订阅期,可以设置3天的免费试用期,用户购买后,免费期退订,是不收费的。但是如果过了试用期,或者忘记退订了,嘿嘿~


    image.png
    • 内购重置功能
        /**
         *  Description: 恢复购买
         */
        public void RestoreButtonClick()
        {
            if (m_IsGooglePlayStoreSelected)
            {
                m_GooglePlayStoreExtensions.RestoreTransactions(OnTransactionsRestored);
            }
            else
            {
                m_AppleExtensions.RestoreTransactions(OnTransactionsRestored);
            }
        }
    

    非消耗品的恢复.

    最后附带上整理出来的源码地址:

    链接: https://pan.baidu.com/s/1ZRj5ciLRUarO_AD99yEz_g 提取码: vurc

    相关文章

      网友评论

          本文标题:使用Unity3d内置IAP插件 实现IOS和Google Pl

          本文链接:https://www.haomeiwen.com/subject/zazgvctx.html