Unity IAP 提供了一个简单的 Webview 界面来通过广告位显示应用内购推荐资源。要控制这些资源的渲染过程,请使用原生推荐 (Native Promo) 来实现自定义推荐显示。
要使用原生推荐,必须从 Asset Store 下载并安装最新的 SDK (3.0+)。
按照相关说明设置 IAP 广告位、商品和推荐,但请注意在配置 IAP 商品时不应该上传广告素材资源。
SDK 提供了一个原生推荐适配器接口,其中包含处理推荐资源交互的方法。在自定义脚本中使用这些方法可以在推荐开始、完成和启动购买流程时通知 SDK。
每个开发人员的原生推荐实现方式有很大差异。以下抽象示例代码说明了一种实现。
using UnityEngine.Monetization;
public class NativePromoDisplay : MonoBehaviour {
PlacementContent placementContent = Monetization.GetPlacementContent (placementId);
PromoAdPlacementContent promoContent = placementContent as PromoAdPlacementContent;
INativePromoAdapter adapter = Monetization.CreateNativePromoAdapter (promoContent);
void ShowPromo () {
LogPromoInfo ();
// 使用 promoContent 的相关 Product ID(例如 adapter.metadata.premiumProduct.productID)来确定要展示的资源
// 调用 adapter.OnShown () 来告知 SDK 已启动推荐,然后执行这些资源的自定义显示
// 调用 adapter.OnClicked () 来告知 SDK 该玩家已点击购买按钮,并启动购买流程
// 调用 adapter.OnClosed () 来告知 SDK 已结束推荐
}
void LogPromoInfo () {
Debug.LogFormat ("Product ID: \t{0}", adapter.metadata.premiumProduct.productId);
Debug.LogFormat ("Localized Title: \t{0}", adapter.metadata.premiumProduct.localizedTitle);
Debug.LogFormat ("Localized Description: \t{0}", adapter.metadata.premiumProduct.localizedDescription);
Debug.LogFormat ("ISO Currency Code: \t{0}", adapter.metadata.premiumProduct.isoCurrencyCode);
Debug.LogFormat ("Localized Price: \t{0}", adapter.metadata.premiumProduct.localizedPrice);
Debug.LogFormat ("Localized Price String: \t{0}", adapter.metadata.premiumProduct.localizedPriceString);
}
}
扩展 ShowAdPlacementContent
类的功能以支持应用内购推荐 (IAP Promo) 内容。
Unity IAP 商品对象。
属性 | 获取方法 | 描述 |
---|---|---|
string productId |
GetProductId () |
商品的内部引用 ID。 |
string localizedTitle |
GetlocalizedTitle () |
面向消费者的商品名称,用于应用商店 UI。 |
string localizedPriceString |
GetLocalizedPriceString () |
面向消费者的价格字符串,包括货币符号,用于应用商店 UI。 |
double localizedPrice |
GetLocalizedPrice () |
内部系统的商品价格值。 |
string isoCurrencyCode |
GetIsoCurrencyCode () |
商品本地化货币的 ISO 代码。 |
string localizedDescription |
GetLocalizedDescription () |
面向消费者的商品描述,用于应用商店 UI。 |
string productType |
GetProductType () |
Unity 支持“消耗品”(Consumable)、“非消耗品”(Non-consumable) 和“订阅”(Subscription) 商品类型。 |
如需了解商品属性的更多详细信息,请参阅有关定义商品的文档。
通过此函数传递 PromoAdPlacementContent
对象以创建新的 INativePromoAdapter
。例如:
INativePromoAdapter adapter = Monetization.CreateNativePromoAdapter (promoContent);
此接口提供用于处理用户与推荐资源交互的访问方法。使用这些方法可以传入自定义资源并定义预期行为。
此属性包含通过适配器传递的 PromoAdPlacementContent
对象的信息。
public struct PromoMetadata {
public Product premiumProduct;
}
public PromoMetadata metadata { get; };
例如:
string cc = nativePromoAdapter.metadata.premiumProduct.isoCurrencyCode;
显示推荐时调用此函数。它应包含显示推荐资源的自定义方法。
void OnShown ();
玩家放弃推荐优惠时调用此函数。
void OnClosed ();
玩家点击按钮购买商品时调用此函数。它应该启动购买流程。
void OnClicked ();
每个开发人员的原生推荐实现方式有很大差异。以下抽象示例代码说明了一种实现。
@interface ViewController: UIViewController <USRVUnityPurchasingDelegate>
-(void) showPromo: (UMONPromoAdPlacementContent *) placementContent {
self.nativePromoAdapter = [[UMONNativePromoAdapter alloc] initWithPromo: placementContent];
UMONPromoMetaData *metaData = placementContent.metadata;
UPURProduct *product = metaData.premiumProduct;
NSString *price = (product == nil || product.localizedPriceString == nil) ? @"$0.99": product.localizedPriceString;
self.nativePromoView.hidden = NO;
NSString *title = [NSString stringWithFormat: @"Buy for only %@", price];
[self.purchaseButton setTitle: title forState: UIControlStateNormal];
[self.nativePromoAdapter promoDidShow];
}
// 如果玩家点击了购买按钮:
(IBAction) purchaseButtonTapped: (id) sender {
[self.nativePromoAdapter promoDidClick];
[self.nativePromoAdapter promoDidClose];
self.nativePromoView.hidden = YES;
}
// 如果玩家关闭了推荐资源:
-(IBAction) promoCloseButtonTapped: (id) sender {
self.nativePromoView.hidden = YES;
[self.nativePromoAdapter promoDidClose];
}
- (void) loadProducts: (UnityPurchasingLoadProductsCompletionHandler) completionHandler {
// 获取商品列表(请参阅购买集成文档)
}
- (void) purchaseProduct: (NSString *) productId
// 插入购买商品成功或失败的逻辑(请参阅购买集成文档)
}
请阅读 iOS 购买集成文档,了解有关此处引用的 USRVUnityPurchasingDelegate
类的更多详细信息。
扩展 UMONShowAdPlacementContent
类的功能以支持应用内购推荐 (IAP Promo) 内容。
Unity IAP 商品对象。
属性 | 描述 |
---|---|
@property (strong, nonatomic, readonly) NSString *productId |
商品的内部引用 ID。 |
@property (strong, nonatomic, readonly) NSString *localizedTitle () |
面向消费者的商品名称,用于应用商店 UI。 |
@property (strong, nonatomic, readonly) NSString *localizedPriceString |
面向消费者的价格字符串,包括货币符号,用于应用商店 UI。 |
@property (nonatomic, readonly) double *localizedPrice |
内部系统的商品价格值。 |
@property (strong, nonatomic, readonly) NSString *isoCurrencyCode |
商品本地化货币的 ISO 代码。 |
@property (strong, nonatomic, readonly) NSString *localizedDescription |
面向消费者的商品描述,用于应用商店 UI。 |
@property (strong, nonatomic, readonly) NSString *productType |
Unity 支持“消耗品”(Consumable)、“非消耗品”(Non-consumable) 和“订阅”(Subscription) 商品类型。 |
如需了解商品属性的更多详细信息,请参阅有关定义商品的文档。
通过此函数传递 UMONPromoAdPlacementContent
对象以创建新的 UMONNativePromoAdapter
。
-(instancetype) initWithPromo: (UMONPromoAdPlacementContent *) promo;
此委托提供用于处理用户与推荐资源交互的访问方法。使用这些方法可以传入自定义资源并定义预期行为。
此属性包含通过适配器传递的 UMONPromoAdPlacementContent
对象的信息。
@interface UMONPromoAdPlacementContent: UMONShowAdPlacementContent
-(instancetype) initWithPlacementId: (NSString *) placementId withParams: (NSDictionary *) params;
@property (nonatomic, strong, readonly) UMONPromoMetaData *metadata;
@end
显示推荐时调用此函数。它应包含显示推荐资源的自定义方法。
-(void) promoDidShow;
-(void) promoDidShow: (UMONNativePromoShowType) showType;
可以传递 UMONNativePromoShowType
枚举值来引用推荐资源的预览类型。kNativePromoShowTypeFull
表示完整推荐视图,而 kNativePromoShowTypePreview
表示可以扩展来显示完整推荐的最小化视图。
玩家放弃推荐优惠时调用此函数。
-(void) promoDidClose;
promoDidClick
玩家点击按钮购买商品时调用此函数。它应该启动购买流程。
-(void) promoDidClick;
每个开发人员的原生推荐实现方式有很大差异。以下抽象示例代码说明了一种实现。
UnityPurchasing.setAdapter (new UnityPurchasingAdapter ());
private class UnityPurchasingAdapter implements IPurchasingAdapter {
@Override
public void retrieveProducts (IRetrieveProductsListener listener) {
// 获取商品列表(请参阅购买集成文档)
}
@Override
public void onPurchase (String productID, ITransactionListener listener, Map<String, Object> extras) {
// 插入购买商品成功或失败的逻辑(请参阅购买集成文档)
}
private void showPromo (final PromoAdPlacementContent placementContent) {
final NativePromoAdapter nativePromoAdapter = new NativePromoAdapter (placementContent);
PromoMetadata metadata = placementContent.getMetadata ();
Product product = metadata.getPremiumProduct ();
String price = product == null ? "$0.99": product.getLocalizedPriceString ();
final View root = getLayoutInflater ().inflate (R.layout.unitymonetization_native_promo, (ViewGroup) findViewById (R.id.unityads_example_layout_root));
Button buyButton = root.findViewById(R.id.native_promo_buy_button);
Button closeButton = root.findViewById (R.id.native_promo_close_button);
buyButton.setText ("Buy now for only " + price + "!");
nativePromoAdapter.onShown();
buyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v) {
//执行购买,然后调用
nativePromoAdapter.onClosed ();
((ViewGroup)root).removeView (findViewById (R.id.native_promo_root));
}
});
closeButton.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick(View v) {
nativePromoAdapter.onClosed ();
((ViewGroup)root).removeView (findViewById (R.id.native_promo_root));
}
});
}
请阅读 Android 购买集成文档,了解有关此处引用的 UnityPurchasingAdapter
类的更多详细信息。
扩展 ShowAdPlacementContent
类的功能以支持应用内购推荐 (IAP Promo) 内容。
Unity IAP 商品对象。
属性 | 获取方法 | 描述 |
---|---|---|
String productId |
getProductId () |
商品的内部引用 ID。 |
String localizedTitle |
getlocalizedTitle () |
面向消费者的商品名称,用于应用商店 UI。 |
String localizedPriceString |
getLocalizedPriceString () |
面向消费者的价格字符串,包括货币符号,用于应用商店 UI。 |
Double localizedPrice |
getLocalizedPrice () |
内部系统的商品价格值。 |
String isoCurrencyCode |
getIsoCurrencyCode () |
商品本地化货币的 ISO 代码。 |
String localizedDescription |
getLocalizedDescription () |
面向消费者的商品描述,用于应用商店 UI。 |
String productType |
getProductType () |
Unity 支持“消耗品”(Consumable)、“非消耗品”(Non-consumable) 和“订阅”(Subscription) 商品类型。 |
如需了解商品属性的更多详细信息,请参阅有关定义商品的文档。
此委托提供用于处理用户与推荐资源交互的访问方法。使用这些方法可以传入自定义资源并定义预期行为。通过 NativePromoAdapter
函数传递 PromoAdPlacementContent
对象以创建新的适配器。例如:
final NativePromoAdapter nativePromoAdapter = new NativePromoAdapter (placementContent);
此属性包含通过适配器传递的 PromoAdPlacementContent
对象的信息。
public PromoMetadata getMetadata ();
显示推荐时调用此函数。它应包含显示推荐资源的自定义方法。
public void onShown (NativePromoShowType type);
可以传递 NativePromoShowType
枚举值来引用推荐资源的预览类型。FULL
表示完整推荐视图,而 PREVIEW
表示可以扩展来显示完整推荐的最小化视图。
玩家放弃推荐优惠时调用此函数。
public void onClicked
玩家点击按钮购买商品时调用此函数。它应该启动购买流程。
public void promoClicked;
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.