重要注意事项:游戏必须在初始化 Unity Ads 之前先初始化 Unity IAP 才能正常使用内购推荐 (IAP Promo)。
本集成指南涵盖四个主要步骤:
要使用内购推荐 (IAP Promo),您需要:
1.配置项目以使用 Unity 服务。 2.在项目中启用 Unity IAP SDK (1.2+) 和 Unity Ads SDK (2.3+)。
内购推荐 (IAP Promo) 需要受支持的 Unity IAP SDK 版本 (1.17+)。要获取最新 IAP SDK,请在 Services 窗口 (Window > Services) 中启用 In-App Purchasing,或者从 Asset Store 将其导入。如果是从 Services 窗口启用,务必在系统提示时选择导入 (Import) 资源包。
请参阅有关设置 IAP 的文档以了解其他信息。
内购推荐 (IAP Promo) 需要受支持的 Unity Ads SDK 版本。Unity 建议从 Asset Store 导入最新的 Unity Ads SDK (3.0+) 以获得此 SDK。
请参阅设置 Unity Ads 以了解其他信息。
在设置所需服务后,即可在游戏中实现它们。
初始化有两个选项:无码或脚本。
Codeless IAP 能为您处理初始化。如果使用 Codeless IAP 初始化,必须在代码的其他位置调用 Unity Ads 初始化方法。
要使用 Codeless IAP,请填充__商品目录__ (Product Catalog),然后创建 IAP 监听器 (IAP Listener) 来获取该目录:
1.在 Editor 中,选择 Window > UnityIAP > IAP Catalog 以打开 IAP Catalog 窗口。该窗口会列出您以前配置的所有商品。必须在__商品目录__中至少配置一个__商品__。有关设置商品的完整过程,请参阅 Codeless IAP。
2.在 IAP Catalog 窗口中,选择 App Store Export > Cloud JSON 以导出__商品目录__的本地副本。
3.创建 IAP 监听器。选择 Window > Unity IAP > Create IAP Listener,然后将其添加到游戏的第一个场景。游戏一启动,监听器就会获取__商品目录。这样可以避免在游戏请求__推荐 (Promotions) 而__商品__没有准备好(因为无码按钮尚未出现在场景中)时发生错误。
如果不使用 Codeless IAP,必须通过脚本手动初始化 Unity IAP。请参阅以下代码示例:
using UnityEngine;
using UnityEngine.Purchasing;
public class IAPManager : MonoBehaviour, IStoreListener {
private IStoreController controller;
//The following products must be added to the Product Catalog in the Editor:
private const string coins100 = "100.gold.coins";
private const string coins500 = "500.gold.coins";
public int coin_count = 0;
void Awake () {
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) {
this.controller = controller; Debug.Log ("Initialization Successful");
}
public void OnInitializeFailed(InitializationFailureReason error) {
Debug.Log ("UnityIAP.OnInitializeFailed (" + error + ")")
}
public void OnPurchaseFailed (Product item, PurchaseFailureReason reason) {
Debug.Log("UnityIAP.OnPurchaseFailed (" + item + ", " + reason + ")");
}
public PurchaseProcessingResult ProcessPurchase (PurchaseEventArgs e) {
string purchasedItem = e.purchasedProduct.definition.id;
switch (purchasedItem) {
case coins100: Debug.Log ("Congratulations, you are richer!");
coin_count += 100;
Debug.Log ("IAPLog: Coin count: " + coin_count);
break;
case coins500: Debug.Log ("Congratulations, you are richer!");
coin_count += 500;
Debug.Log ("IAPLog: Coin count: " + coin_count);
break;
}
return PurchaseProcessingResult.Complete;
}
public void Buy(string productId) {
Debug.Log ("UnityIAP.BuyClicked (" + productId + ")");
controller.InitiatePurchase (productId);
}
}
无论使用的是 Codeless IAP 还是手动 IAP 初始化方法,都必须初始化 Unity Ads。以下代码示例显示了要调用的初始化方法:
using UnityEngine;
using UnityEngine.Monetization;
public class AdManager : MonoBehaviour {
public bool testMode = true;
private const string adPlacement = "video";
private const string promoPlacement = "promo";
#if UNITY_IOS
private string gameId = "0000000"; // Your iOS game ID here
#elif UNITY_ANDROID
private string gameId = "9999999"; // Your Android game ID here
#else
private string gameId = "0123456"; // Prevents Editor Errors
#endif
private void Awake () {
if (Monetization.isSupported && !Monetization.isInitialized) {
Monetization.Initialize (gameId, testMode);
}
}
public void ShowVideoAd () {
ShowAdPlacementContent ad = Monetization.GetPlacementContent (adPlacement) as ShowAdPlacementContent;
ad.Show ();
}
public void ShowPromo () {
PromoAdPlacementContent promo = Monetization.GetPlacementContent (promoPlacement) as PromoAdPlacementContent;
promo.Show ();
}
}
导航至 Operate 控制面板的 Monetization 部分可配置内购推荐 (IAP Promo) 优惠:
Use the Editor Play mode to check that a test ad appears when the Placement makes its request.
In order to test Promos, you must have Promo assets configured and build to a device. If you initialize in test mode, you will see a test Promo creative. To see real promotional creative assets, you must build the game to a device in production mode.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.