Version: 2018.1
内购推荐 (IAP Promo)
内购推荐 (IAP Promo) 广告位

内购推荐 (IAP Promo) 集成

概述

重要注意事项:游戏必须在初始化 Unity Ads 之前先初始化 Unity IAP 才能正常使用内购推荐 (IAP Promo)。

本集成指南涵盖四个主要步骤:

在 Unity Editor 中准备项目

设置 Unity 服务

要使用内购推荐 (IAP Promo),您需要:

1.配置项目以使用 Unity 服务。 2.在项目中启用 Unity IAP SDK (1.2+) 和 Unity Ads SDK (2.3+)。

设置 Unity IAP

IAP Promo requires a supported version of the Unity IAP SDK (1.17+). To acquire the latest IAP SDK, either enable In-App Purchasing in the Services window (Window > Services), or import it from the Asset store. If you’re enabling it from the Services window, be sure to Import the Asset package when prompted.

Enabling Unity IAP in the Editor’s Services window
Enabling Unity IAP in the Editor’s Services window

请参阅有关设置 IAP 的文档以了解其他信息。

设置 Unity Ads

内购推荐 (IAP Promo) 需要受支持的 Unity Ads SDK 版本 (2.2+)。请从 Asset Store 导入最新的 Unity Ads SDK 以获得此 SDK。这样就能为项目启用 Unity Ads。

请参阅设置 Unity Ads 以了解其他信息。

实现

在设置所需服务后,即可在游戏中实现它们。

实现 IAP

初始化有两个选项:无码或脚本。

使用 Codeless IAP

Codeless IAP 能为您处理初始化。如果使用 Codeless IAP 初始化,必须在代码的其他位置调用 Unity Ads 初始化方法。

要使用 Codeless IAP,请填充__商品目录__ (Product Catalog),然后创建 IAP 监听器 (IAP Listener) 来获取该目录:

  1. In the Editor, select Window > UnityIAP > IAP Catalog to open the IAP Catalog window. This window lists all of your previously configured Products. You must have at least one Product configured in your Product Catalog. For a complete walkthrough on setting up Products, see Codeless IAP.

  2. In the IAP Catalog window, select App Store Export > Cloud JSON to export a local copy of the Product Catalog.
    Exporting an IAP Product Catalog to JSON

  3. Create an IAP Listener. Select Window > Unity IAP > Create IAP Listener, and add it to the first scene of your game. The listener fetches your Product Catalog as soon as the game boots. This avoids errors where the game requests Promotions but a Product isn’t ready because the codeless button hasn’t appeared in the scene yet.

使用脚本

如果不使用 Codeless IAP,必须通过脚本手动初始化 Unity IAP。请参阅以下代码示例:

using System.Collections;
using System.Collections.Generic;

using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Purchasing;
using UnityEngine.UI;

public class UnityIAP : MonoBehaviour, IStoreListener
{
    private IStoreController controller;

    private const string product_coins = "100.gold.coins";
    private const string product_hat = "top_hat";
    private const string product_elite = "elite_status";
    private const string product_bundle = "gem_super_box";
    public int coin_count = 0;
    public int gems_count = 0;
    public bool hat_owned = false;
    public bool elite_member = false;

    private void Start()
    {
        Debug.Log("UnityIAP.Init()");

        StandardPurchasingModule module = StandardPurchasingModule.Instance();
        ProductCatalog catalog = ProductCatalog.LoadDefaultCatalog();
        ConfigurationBuilder builder = ConfigurationBuilder.Instance(module);
        IAPConfigurationHelper.PopulateConfigurationBuilder(ref builder, catalog);

        UnityPurchasing.Initialize(this, builder);
    }

    public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
    {
        Debug.Log("UnityIAP.OnInitialized(...)");
        this.controller = controller;

        // Include any additional initialization logic as needed
    }

    public void OnInitializeFailed(InitializationFailureReason error)
    {
        Debug.Log("UnityIAP.OnInitializeFailed(" + error + ")");

        // Include any additional initialization failure logic as needed 
    }

    public void Buy(string productId)
    {
        Debug.Log("UnityIAP.BuyClicked(" + productId + ")");
        this.controller.InitiatePurchase(productId);
    }

    public void OnPurchaseFailed(Product item, PurchaseFailureReason r)
    {
        Debug.Log("UnityIAP.OnPurchaseFailed(" + item + ", " + r + ")");
    }

    public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e)
    {
        string purchasedItem = e.purchasedProduct.definition.id;

        switch (purchasedItem)
        {
            case product_coins:
                Debug.Log("IAPLog: Congratualtions you are richer!");
                coin_count += 100;
                Debug.Log("IAPLog: Coin count: " + coin_count);
                break;

            case product_hat:
                hat_owned = true;
                Button topHatButton = GameObject.Find("buyTopHat").GetComponent<Button>();
                topHatButton.interactable = false;
                topHatButton.GetComponentInChildren<Text>().text = "That hat is dashing!";
                Debug.Log("IAPLog: Hat owned: " + hat_owned);
                break;

            case product_elite:
                elite_member = true;
                Button eliteButton = GameObject.Find("buyElite").GetComponent<Button>();
                eliteButton.interactable = false;
                eliteButton.GetComponentInChildren<Text>().text = "Welcome to Elite Status";
                Debug.Log("IAPLog: Elite member: " + elite_member);
                break;
            case product_bundle:
                gems_count += 5000;
                break;
        }

        return PurchaseProcessingResult.Complete;
    }
}

实现 Unity Ads

无论使用的是 Codeless IAP 还是手动 IAP 初始化方法,都必须初始化 Unity Ads。以下代码示例显示了要调用的初始化方法:

using System.Collections;
using System.Collections.Generic;

using UnityEngine;
using UnityEngine.Advertisements;
using UnityEngine.Events;
using UnityEngine.Purchasing;
using UnityEngine.UI;

public class AdsManager : MonoBehaviour
{
    #if UNITY_IOS
        private string gameId = "0000000"; // 在此处输入您的 iOS 游戏 ID
    #elif UNITY_ANDROID
        private string gameId = "9999999"; // 在此处输入您的 Android 游戏 ID
    #else
        private string gameId = "0123456"; // 防止 Editor 错误
    #endif

    public void Init()
    {
        Debug.Log("UnityAds.Init()");
        if (!Advertisement.isSupported || Advertisement.isInitialized)
        {
            Debug.Log("Could not initialize ads");
            return;
        }

        Debug.Log("Initializing Unity Ads with game ID: " + gameId);
        Advertisement.Initialize(gameId, false);
    }

    public void ShowAdUnit()
    {
        Debug.Log("Unity Ads Log: Ad shown");
        Advertisement.Show("testAdButton"); // 在此处输入广告的广告位 ID
    }

    public void ShowPromo()
    {
        Debug.Log("Unity Ads Log: Promo Shown");
        Advertisement.Show("testPromoButton"); // 在此处输入推荐 (Promo) 的广告位 ID
    }
}

在开发者控制面板 (Developer Dashboard) 中配置推荐 (Promotions)

导航至开发者控制面板 (Developer Dashboard) 的内购推荐 (IAP Promo) 部分可配置内购推荐 (IAP Promo) 优惠:

  • 使用广告位可控制__推荐 (Promotions)__ 在游戏中的显示时机和方式。
  • 使用商品界面可导入__商品目录__并管理每个商品的创意资源。
  • 定义推荐 (Promotions) 的参数,例如运行时间、包含的__广告位__和__商品__以及目标用户。

测试集成结果

通过实现以下示例代码来调用内购推荐 (IAP Promo) 内容:

public void ShowPromo()
{
    Advertisement.Show (placementID);
}

按 Editor 中的 Play 可检查__广告位__提出请求时是否显示测试广告。要查看真实的推荐广告素材资源,必须在生产模式下将游戏发布到设备。




内购推荐 (IAP Promo)
内购推荐 (IAP Promo) 广告位